@echo off REM version 1 :: ---------------------------------------------------------------------------- :: Usage: inventory.cmd :: ---------------------------------------------------------------------------- :: 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 (.co.uk -> uk) :: ---------------------------------------------------------------------------- set path_local=..\cache set dns_tld=local :: -- 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 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 ^ :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