Outils pour utilisateurs

Outils du site


windows:script_quick_inventory

Ceci est une ancienne révision du document !


Quick Inventory

Les fichiers

qi.bat
@echo off
REM variables definition (can be edited)
set QI_MAX_THREAD=8
set QI_BASE_DIR=D:\Quick_Inventory
REM variables definition
set QI_CPT=0
set QI_CPT_OFF=0
set QI_CPT_ON=0
set QI_CPT_TIMEOUT=0
set QI_CPT_ONLINE=0
set QI_THREAD_NBR=1
set /a QI_THREAD_HIGHER=%QI_MAX_THREAD%-1
set QI_CACHE=%QI_BASE_DIR%\cache
set QI_BIN=%QI_BASE_DIR%\bin

REM delete cache folder if requested
if "%1"=="--delete-cache" rmdir /s /q %QI_CACHE%

REM create working directory
if not exist %QI_CACHE%\. mkdir %QI_CACHE%
if not exist %QI_BIN%\. mkdir %QI_BIN%
REM clean cache
del /q %QI_CACHE%\*.ping >nul 2>&1
del /q %QI_CACHE%\*.list >nul 2>&1
del /q %QI_CACHE%\*.tmp >nul 2>&1
del /q %QI_CACHE%\workstations.* >nul 2>&1
for /f "tokens=3,4" %%T IN ('dir /-c %QI_CACHE%\*.details') DO (
	if "%%T"=="0" del /q %QI_CACHE%\%%U >nul 2>&1
)
 
SETLOCAL ENABLEDELAYEDEXPANSION
REM find the number of workstations, split workstations into multiple lists
for /f "skip=1" %%L IN (list.csv) DO (
	set /a QI_CPT+=1
	set /a QI_THREAD_NBR=!QI_CPT! %% %QI_MAX_THREAD%
	echo %%L>> %QI_CACHE%\ping.thread!QI_THREAD_NBR!.list
)
 
echo %QI_CPT% workstations found
echo ---
REM ping all workstations
echo ping all workstations
for /l %%D IN (0,1,%QI_THREAD_HIGHER%) DO start %QI_BIN%\ping.thread.cmd %%D
REM wait for ping end
echo wait for %QI_MAX_THREAD% term signals ('waitfor /si thread' to trigger manually)
for /l %%E IN (0,1,%QI_THREAD_HIGHER%) DO waitfor thread
echo all signals received
echo ---

REM list offline workstations
for /f %%C IN ('findstr /c:"(100%% loss)" /c:"Destination host unreachable" /c:"could not find host" /M %QI_CACHE%\*.ping') DO echo %%~nC>> %QI_CACHE%\workstations.offline
REM find the number of online/offline workstations
for /f %%L IN (%QI_CACHE%\workstations.offline) DO set /a QI_CPT_OFF+=1
set /a QI_CPT_ON=%QI_CPT%-%QI_CPT_OFF%
REM list online workstations
for /f "skip=1" %%G IN (list.csv) DO (
	for /f "tokens=1 delims=" %%H IN ('findstr /x %%G %QI_CACHE%\workstations.offline') DO set var=%%H
	if not "!var!"=="%%G" echo %%G>> %QI_CACHE%\workstations.online
)

REM split online workstations into multiple lists
for /f %%L IN (%QI_CACHE%\workstations.online) DO (
	if not exist %QI_CACHE%\%%L.details (
		set /a QI_CPT_ONLINE+=1
		set /a QI_THREAD_NBR=!QI_CPT_ONLINE! %% %QI_MAX_THREAD%
		set workstation=%%L
		set machine_type=!workstation:~5,3!
		for %%R IN (wdx WDX wlx WLX) DO (
			if "!machine_type!"=="%%R" echo %%L>> %QI_CACHE%\details.thread!QI_THREAD_NBR!.list
		)
	)
)
 
echo %QI_CPT_ON% online / %QI_CPT_OFF% offline
echo %QI_CPT_ON%> %QI_CACHE%\online_count.tmp
echo %QI_CPT_OFF%> %QI_CACHE%\offline_count.tmp
echo ---
REM get details from online workstations
echo get details from online workstations
for /l %%D IN (0,1,%QI_THREAD_HIGHER%) DO start %QI_BIN%\details.thread.cmd %%D
REM wait for details end
echo wait for %QI_MAX_THREAD% term signals ('waitfor /si thread' to trigger manually)
for /l %%E IN (0,1,%QI_THREAD_HIGHER%) DO waitfor thread
echo all signals received
echo ---

REM clean cache from empty details
for /f "tokens=3,4" %%T IN ('dir /-c %QI_CACHE%\*.details') DO (
	if "%%T"=="0" del /q %QI_CACHE%\%%U >nul 2>&1
)

REM building html result
echo building html result
call %QI_BIN%\html_generator.bat
start firefox "%QI_BASE_DIR%\qi.html"
ENDLOCAL
:eof
list.csv
Hostname
workstation1
workstation2
ping.thread.cmd
@echo off
for /f %%A IN (%QI_CACHE%\ping.thread%1.list) DO call:knock %%A
waitfor /si thread
exit
:knock
echo %1
ping -n 2 -w 5000 %1 > %QI_CACHE%\%1.ping
:eof
details.thread.cmd
@echo off
for /f %%K IN (%QI_CACHE%\details.thread%1.list) DO (
	systeminfo /s \\%%K> %QI_CACHE%\%%K.details.tmp
	findstr /v /r "KB[0-9][0-9]*$" %QI_CACHE%\%%K.details.tmp> %QI_CACHE%\%%K.details.1.tmp
	findstr /v /c:"OS Manufacturer" /c:"OS Configuration" /c:"OS Build Type" /c:"Virtual Memory" %QI_CACHE%\%%K.details.1.tmp> %QI_CACHE%\%%K.details.2.tmp
	findstr /v /l "Registered Directory Locale" %QI_CACHE%\%%K.details.2.tmp> %QI_CACHE%\%%K.details
	del /q %QI_CACHE%\%%K.details.*.tmp
)
waitfor /si thread
exit
:eof
inventory.cmd
@echo off
REM version 1
:: ----------------------------------------------------------------------------
:: Usage: inventory.cmd <computer_name>
:: ----------------------------------------------------------------------------
:: Description: It creates a batch file filled with WMIC commands and calls it.
::   The results located in %path_local% folder are compute. Temporary files
::   are deleted.
:: ----------------------------------------------------------------------------

:: -- User customizable variables ---------------------------------------------
:: dns_tld       : top level domain (.bav.fr.corp -> corp)
:: ----------------------------------------------------------------------------
set path_local=..\cache
set dns_tld=corp

:: -- Other variables ---------------------------------------------------------
if "%1"=="" goto syntax

REM extract codes from workstation name
set workstation=%1
set country=%workstation:~0,2%
set site=%workstation:~2,3%
set machine_type=%workstation:~5,3%
set batch_file=%path_local%\%workstation%.cmd

REM machine_type=WLX or WDX
set verif_type=
if "%machine_type%"=="vmw" set machine_type=WDX
if "%machine_type%"=="VMW" set machine_type=WDX
for %%A IN (wdx WDX wlx WLX wlt WLT) DO if "%machine_type%"=="%%A" set verif_type=TRUE
if not "%verif_type%"=="TRUE" (
	echo machine_type %machine_type% not supported.
	goto fin
)

:inventaire
REM Create the batch file to run with admin rights
REM echo @echo off> %batch_file%
echo echo inventory of %workstation% in progress...>> %batch_file%
echo set OU7=>> %batch_file%
echo wmic /NODE:%workstation% os get Name^> %path_local%\%workstation%.os.tmp>> %batch_file%
echo type %path_local%\%workstation%.os.tmp ^| findstr /i /c:"Windows 7" ^>nul>> %batch_file%
echo if #%%ERRORLEVEL%%==#0 set OU7=7 >> %batch_file%
echo call:trim OU7 "%%OU7%%">> %batch_file%
echo REM request for dsget command>> %batch_file%
echo set request="CN=%workstation%,OU=%machine_type%%%OU7%%,OU=%site%,DC=%country%,DC=%dns_tld%">> %batch_file%
echo dsget computer %%request%% -desc^> %path_local%\%workstation%.ad-user.tmp>> %batch_file%
echo REM user_login>> %batch_file%
echo wmic /NODE:%workstation% computersystem get UserName ^> %path_local%\%workstation%.username.tmp>> %batch_file%
echo for /f "skip=1 delims=" %%%%A in ('type %path_local%\%workstation%.username.tmp') do (>> %batch_file%
echo 	for /f "tokens=1,2 delims=\" %%%%B in ("%%%%A") do (>> %batch_file%
echo 		set user_domain=%%%%B>> %batch_file%
echo 		set user_login=%%%%C>> %batch_file%
echo 	)>> %batch_file%
echo )>> %batch_file%
echo call:trim user_login "%%user_login%%">> %batch_file%
echo REM other wmic commands>> %batch_file%
echo wmic useraccount where "Name='%%user_login%%' and Domain='%%user_domain%%'" get FullName^> %path_local%\%workstation%.fullname.tmp>> %batch_file%
echo wmic /NODE:%workstation% csproduct get IdentifyingNumber,Name^> %path_local%\%workstation%.serial.tmp>> %batch_file%
echo wmic /NODE:%workstation% computersystem get SystemType^> %path_local%\%workstation%.type.tmp>> %batch_file%
echo wmic /NODE:%workstation% nic get MACAddress,Name ^| findstr /r "[0-9A-F]**:[0-9A-F]**:[0-9A-F]**:[0-9A-F]**:[0-9A-F]**:[0-9A-F]**"^> %path_local%\%workstation%.nic.tmp>> %batch_file%
echo waitfor /si inventaire>> %batch_file%
echo goto:eof>> %batch_file%
echo :trim>> %batch_file%
echo REM usage: call:trim var_name var_value>> %batch_file%
echo set "str=%%~2##">> %batch_file%
echo set "str=%%str:        ##=##%%">> %batch_file%
echo set "str=%%str:    ##=##%%">> %batch_file%
echo set "str=%%str:  ##=##%%">> %batch_file%
echo set "str=%%str: ##=##%%">> %batch_file%
echo set "%%~1=%%str:##=%%">> %batch_file%
echo set str=>> %batch_file%
echo goto:eof>> %batch_file%
echo :eof>> %batch_file%
start cmd.exe /C "%batch_file%"
waitfor inventaire

REM user_login
for /f "skip=1 delims=" %%A IN ('type %path_local%\%workstation%.username.tmp') DO (
	for /f "tokens=1,2 delims=\" %%B in ("%%A") do (
		set user_domain=%%B
		set user_login=%%C
	)
)
call:trim user_login "%user_login%"

REM User FullName
for /f "skip=1 delims=" %%A in ('type %path_local%\%workstation%.fullname.tmp') do set user_fullname=%%A
set user_fullname=%user_fullname:(external)=%
call:trim user_fullname "%user_fullname%"

REM ad_user
for /f "tokens=1,* delims=:" %%A IN ('findstr /n /r . %path_local%\%workstation%.ad-user.tmp') DO if #%%A==#2 set ad_user=%%B
:: left trim
for /f "tokens=* delims= " %%A IN ("%ad_user%") DO set ad_user=%%A

REM serial_num
for /f "skip=1 tokens=1* delims= " %%A in ('type %path_local%\%workstation%.serial.tmp') do set serial_num=%%A && set model_name=%%B
call:trim serial_num "%serial_num%"

REM model_name
call:trim model_name "%model_name%"

REM OS
for /f "skip=1 delims=" %%A in ('type %path_local%\%workstation%.os.tmp') do for /f "delims=|" %%B in ("%%A") do set os_name=%%B

REM arch_type
for /f "skip=1 delims=" %%A in ('type %path_local%\%workstation%.type.tmp') do set arch_type=%%A
set arch_type=%arch_type:~0,3%

REM check if the user connected is the ad_user
echo %ad_user% | findstr /i /c:"%user_fullname%" > NUL
if #%ERRORLEVEL%==#0 set user_change=false

REM write to file
set details=..\cache\%workstation%.details
echo workstation : %workstation%> %details%
echo model type  : %model_name%>> %details%
echo serial num. : %serial_num%>> %details%
echo OS name     : %os_name% ^(%arch_type%^)>> %details%
type %path_local%\%workstation%.nic.tmp>> %details%
if "%user_change%"=="false" echo user        : %user_fullname% ^(%user_login%^)>> %details%
if not "%user_change%"=="false" (
	echo user        : %user_fullname% ^(%user_login%^)>> %details%
	echo AD assign.  : %ad_user%>> %details%
)
REM write to screen
echo.
echo workstation : %workstation%
echo model type  : %model_name%
echo serial num. : %serial_num%
echo OS name     : %os_name% ^(%arch_type%^)
type %path_local%\%workstation%.nic.tmp
if "%user_change%"=="false" echo user        : %user_fullname% ^(%user_login%^)
if not "%user_change%"=="false" (
	echo user        : %user_fullname% ^(%user_login%^)
	echo AD assign.  : %ad_user%
)
goto fin

:syntax
echo Usage: %0 ^<computer_name^>

:fin
del /q %path_local%\%workstation%.*.tmp
goto:eof

:trim
REM usage: call:trim var_name var_value
set "str=%~2##"
set "str=%str:        ##=##%"
set "str=%str:    ##=##%"
set "str=%str:  ##=##%"
set "str=%str: ##=##%"
set "%~1=%str:##=%"
set str=
goto:eof
template.html
<?xml version="1.0" encoding="iso-8859-1"?>
<//www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="fr">
<head>
<title>Quick Inventory</title>
<link rel="stylesheet" type="text/css" href="bin/normal.css" media="screen" />
<script type="text/javascript">
function switch_visibility(style_type) {
	var liste = new Array();
	liste = document.getElementsByClassName(style_type);
	var tagLI = document.getElementById(style_type);
	for (var i = 0; i < liste.length; i++) {
		if (liste[i].style.display == 'none') {
			liste[i].style.display = 'inline-block';
		} else {
			liste[i].style.display = 'none';
		}
	}
	if (tagLI.className == 'select') {
		tagLI.className = '';
	} else {
	tagLI.className = 'select';
	}
}
function show_details(workstation) {
	var divDESC = document.getElementById('desc');
	var obj = document.createElement('object');
	obj.data = 'cache/'+ workstation +'.details';
	obj.type = 'text/plain';
	obj.id = 'details';
	divDESC.innerHTML = '';
	divDESC.appendChild(obj);
}
</script>
</head>
<body>
<div id="fixed">
	<div id="desc">
		<!-- Dynamic Content -->
	</div>
	<div id="menu">
		<ul id="menu_items">
			<li><a href="javascript:switch_visibility('online');" id="online" class="select">online</a></li>
			<li><a href="javascript:switch_visibility('offline');" id="offline" class="select">offline</a></li>
		</ul>
	</div>
</div>
<div id="list">
<ul>
###INSERT###
</ul>
</div>
</body>
</html>
normal.css
/* ---------------------------------------------------------------- */
/* --- GENERAL                                                ----- */
/* ---------------------------------------------------------------- */
body {
margin:0;
padding:0;
font-size:1em;
font-family:Calibri, sans-serif;
background-color:rgb(240,240,240);
}
 
div {
margin:0;
padding:0;
}
 
a {
text-decoration:none;
}
 
 
/* ---------------------------------------------------------------- */
/* --- Barre de description                                   ----- */
/* ---------------------------------------------------------------- */
#fixed{
position:fixed;
top:0;
left:1%;
width:98%;
height:200px;
padding:0 5px;
}
 
#desc {
height:160px;
border-bottom: 1px solid #c7c6c0;
background-color:rgb(255,255,255);
}
 
#details {
width: 100%;
}
 
 
/* ---------------------------------------------------------------- */
/* --- Menu central                                           ----- */
/* ---------------------------------------------------------------- */
#menu {
height:40px;
background-color:rgb(240,240,240);
border-bottom: 1px solid #c7c6c0;
}
 
#menu ul {
margin:0;
padding:0;
list-style-type:none;
}
 
#menu li {
display:inline-block;
}
 
#menu a {
display:block;
font-size:1.2em;
margin:3px 4px;
padding:4px 18px;
color:#31363e;
}
 
#menu a:hover {
background-color:#cccccc;
color:#202429;
border-radius:5px;
-moz-border-radius:5px;
-webkit-border-radius:5px;
}
 
#menu a.select {
color:#ffffff;
background-color:#5a6b7f;
border-radius:5px;
-moz-border-radius:5px;
-webkit-border-radius:5px;
}
 
#menu a.select:hover {
background-color:#2b3a4b;
}
 
 
/* ---------------------------------------------------------------- */
/* --- Liste des machines                                     ----- */
/* ---------------------------------------------------------------- */
#list {
padding-top:210px;
}
 
#list ul {
list-style-type:none;
}
 
#list li {
display:inline-block;
min-width:64px;
padding:64px 4px 4px 4px;
text-align:center;
}
 
#list li.online {
background:url('../img/computer_online.png') no-repeat top center;
}
 
#list li.offline {
background:url('../img/computer_offline.png') no-repeat top center;
}
 
#list a {
display:block;
color:#2a69a3;
text-align:center;
}
 
#list a:hover {
color:#1f4c75;
background-color:#d5d4cf;
border-radius:5px;
-moz-border-radius:5px;
-webkit-border-radius:5px;
}
 
.name {
font-variant:small-caps;
}
html_generator.bat
@echo off
set templateHTML=%QI_BIN%\template.html
set outputFile=%QI_BASE_DIR%\qi.html
set pattern=###INSERT###
 
for /f "tokens=1 delims=:" %%X IN ('findstr /n "%pattern%" %templateHTML%') DO set line=%%X
set /a line_before=%line% - 1
powershell -command "& {get-content %templateHTML% -totalcount %line_before%}"> %QI_CACHE%\begin.tmp
more +%line% %templateHTML%> %QI_CACHE%\end.tmp
 
SETLOCAL ENABLEDELAYEDEXPANSION
for /f "skip=1" %%A IN (list.csv) DO (
	set v=%%A
	for /f "delims=" %%K IN ('findstr /i /m %%A %QI_CACHE%\workstations.*') DO (set state=%%~xK)
	echo 	^<li class="!state:~1!"^>^<a href="javascript:show_details^('%%A'^);"^>^<span class="name"^>!v:~5!^</span^>^</a^>^</li^>>> %QI_CACHE%\middle.tmp
)
ENDLOCAL
del /q %outputFile% >nul 2>&1
copy /a %QI_CACHE%\begin.tmp + %QI_CACHE%\middle.tmp + %QI_CACHE%\end.tmp %outputFile%
windows/script_quick_inventory.1503149872.txt.gz · Dernière modification: 2017/08/19 15:37 de pascal