Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- :This script polls a list of computers and uses WMI to extract the products table.
- :If a target is successfully pinged it requests the computer name from WMI and
- :fails if the target name given doesn't match WMI. It extracts the products table
- :into a text file named after the target with no extension, putting the target name
- :at the top of the file. This data can later be searched with FINDSTR or imported
- :into a SQL table with a separate script. Using the "repeat" variable, this script
- :can be configured to repeat ad infinitum. It will not re-poll targets that have
- :had their products table successfully extracted unless the "refresh" bit is set.
- :It also logs WMI errors, although the format of the output file needs work to be useful.
- :-------------------------------------------------------
- :This section contains editable variables.
- :-------------------------------------------------------
- :Turning this flag on (1) will make the script loop indefinitely using the number
- :of threads specified in %threads%.
- set repeat=0
- set threads=30
- :Setting this flag will poll all machines in the list whether or not they
- :have been successfully polled in the past.
- set refresh=0
- :This is the list of targets. It should have a column header or a
- :blank line at the top.
- set sourcelist=computerlist.txt
- :Uncomment this line to generate and use a list of all computer objects (including disabled accounts) in the domain.
- REM dsquery * "dc=webtrends,dc=corp" -filter "(objectclass=computer)" -attr cn -l -limit 0 > computerlist.txt
- :-------------------------------------------------------
- :start
- @echo off
- setlocal enabledelayedexpansion
- cls
- if not exist .\output mkdir output
- if "%1" GTR "" goto :begin
- if exist results.txt del results.txt
- if exist inst* del inst*
- set instance=INST%random%
- set scriptname=WMIScrapeMultix.bat
- set counter=-1
- for /f "tokens=*" %%i in (%sourcelist%) do set /a counter +=1
- if %repeat% equ 0 (
- Echo The file has %counter% entries. How many threads?
- set /p threads=Threads:
- )
- set /a batch=%counter% / %threads%
- set modulus=%counter%
- set /a modulus %%=%batch%
- set threadcount=0
- set OS=0
- :ThreadParse
- set counter=0
- set skip=0
- set /a skip =%threadcount% * %batch%
- set /a threadcount += 1
- if %skip% equ 0 set skip=1
- if %threadcount% EQU %threads% set /a batch +=%modulus%
- for /f "tokens=* skip=%skip%" %%i in (%sourcelist%) do (
- if !counter! LEQ !batch! (
- echo %%i >> %instance%.%threadcount%
- set /a counter +=1
- )
- )
- if %threadcount% LSS %threads% goto :threadparse
- for /L %%i in (1,1,%threads%) do start /min %scriptname% %instance% %%i
- call :waiting
- for /f "usebackq" %%i in (`dir /b *.results`) do (
- type %%i >> results.txt
- del %%i
- )
- for /f "usebackq" %%i in (`dir /b *.WMIErrors`) do (
- type %%i >> WMIErrors.txt
- del %%i
- )
- if %repeat% EQU 1 (
- cls
- Echo If you want to stop this nonsense, hit CTRL-C now. You have five seconds.
- >nul ping -n 1 -w 6000 1.0.0.1
- goto :start
- )
- goto :eof
- :waiting
- cls
- echo Processing.
- >nul ping -n 1 -w 1000 1.0.0.1
- cls
- echo Processing..
- >nul ping -n 1 -w 1000 1.0.0.1
- cls
- echo Processing...
- >nul ping -n 1 -w 1000 1.0.0.1
- cls
- echo Processing....
- >nul ping -n 1 -w 1000 1.0.0.1
- if exist %instance%.? goto :waiting
- if exist %instance%.?? goto :waiting
- goto :eof
- :begin
- @echo off
- cls
- setlocal enabledelayedexpansion
- set thread=%2
- set infile=%1
- set myfile=%infile%.%thread%
- set mytemp=%infile%.%thread%.tmp
- set myname=%infile%.%thread%.name
- set myresults=%infile%.%thread%.results
- set size=0
- set counter=0
- set total=0
- for /f "tokens=*" %%i in (%myfile%) do set /a total +=1
- for /f "tokens=1" %%i in (%myfile%) do (
- set /a counter +=1
- echo %%i - !counter! of %total%
- set target=%%i
- set target=!target: =!
- if %refresh% equ 1 (
- ping %%i -n 4 -w 1000 > %mytemp%
- > nul findstr "Reply from" %mytemp%
- if !errorlevel! EQU 0 (
- call :work %%i
- ) ELSE (
- echo FailedPing,None,%%i >> %myresults%
- )
- ) ELSE (
- :echo NotRefreshing!
- If not exist .\output\!target! (
- ping %%i -n 4 -w 1000 > %mytemp%
- > nul findstr "Reply from" %mytemp%
- if !errorlevel! EQU 0 (
- call :work %%i
- ) ELSE (
- echo FailedPing,None,%%i >> %myresults%
- )
- )
- )
- )
- )
- if exist %mytemp% del %mytemp%
- if exist %myname% del %myname%
- if exist %myname%.txt del %myname%.txt
- if exist %myfile% del %myfile%
- exit
- goto :eof
- :work
- set size=0
- set target=%1
- set target=%target: =%
- set name=0
- set os=0
- if exist %mytemp% del %mytemp%
- if exist %myname% del %myname%
- if exist %myname%.txt del %myname%.txt
- call :nameget
- call :osget
- if !name! LEQ 2 (
- echo FailedWMIName,None,%target% >> %myresults%
- goto :eof
- )
- if /I "!name!" NEQ "%target%" (
- echo DNSMismatch,"!Name!",%Target%,!os! >> %myresults%
- goto :eof
- )
- echo !name! > %myname%
- REM wmic /node:"%target%" systemenclosure get serialnumber >> %mytemp% 2>>%myfile%.WMIErrors
- wmic /node:"%target%" product get name >> %mytemp% 2>>%myfile%.WMIErrors
- call :filecheck %mytemp%
- if !size! GTR 6 (
- type %mytemp% >> %myname%
- >nul copy %myname% .\output\!name!
- echo Success,!name!,%target%,!os! >> %myresults%
- ) else (
- echo FailedWMIProducts,!name!,%target%,!os! >> %myresults%
- )
- goto :eof
- :nameget
- 2>>%myfile%.WMIErrors wmic /node:"%target%" /failfast:on ComputerSystem get name |findstr /v "Name" > %myname%.txt
- for /f "tokens=1" %%i in (%myname%.txt) do (
- set name=%%i
- set name=!name: =!
- del %myname%.txt
- exit /b 9993
- )
- goto :eof
- :osget
- 2>>%myfile%.WMIErrors wmic /node:"%target%" /failfast:on OS get name |findstr /v "Name" > %myname%.txt
- for /f "tokens=*" %%i in (%myname%.txt) do (
- set OS=%%i
- del %myname%.txt
- exit /b 9993
- )
- goto :eof
- :filecheck
- set size=%~z1
- goto :eof
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement