Computer Hope

Microsoft => Microsoft DOS => Topic started by: MadFly on August 07, 2014, 12:50:49 PM

Title: Fix WMI remotely with list of hostnames
Post by: MadFly on August 07, 2014, 12:50:49 PM
I am trying to create a script that will scan through a list of hostnames, do an NSLOOKUP to confirm if hostname is bad or good. if good then copy the WMIfix to remote pc, and go run it on each machine's cmd.

so far it seems to be working fine. it seems as if it is actually fixing the WMI of remote machines. but i determined that the WMIFIX bombs out at line 15. and then PSEXEC gives error code/exit of 255. Which means "errors exceeding 154 files". And then it does not run any further.

I would also like to be able to echo something in the main script to state that the WMIFIX has been run on remote pc.

this is the main script... fallouts.bat

Code: [Select]
@echo off
@cls

:: **************************************************
::
:: Just grabs the machine names from a list and then
:: calls another subroutine, passing the name to the
:: routine.
::
:: **************************************************

:getName

  for /f %%a in (list.txt) do call :doIt %%a

  goto end

:: **************************************************
::
:: The %1 is the %%a from the previous routine. In this
:: case you get the machine name. It is being set
:: as a variable for ease of use in the rest of the
:: script.
::
:: So now you copy the file out to the system and
:: and verify it is there. The IF statement defines
:: a variable to be used for logging and to determine
:: whether or not to waste time running PSEXEC against
:: a machine where the file failed to copy.
::
:: So now we say if the var strFil = "ok", go ahead
:: and run PSEXEC. If not, then go log what you have
:: so far.
::
:: I would include some kind of error checking after
:: running REGSVR32 to verify the file was registered
:: and then log that as well.
::
:: **************************************************

:doIt

  set strSvr=%1
 
PING %1 -n 1| FIND /i "TTL" > nul && goto Success
PING %1 -n 1| FIND /i "timed" > nul && goto Timedout
PING %1 -n 1 -w 400 | FIND /i "TTL" > nul || goto ErrorMsg
goto :EOF

:Success
cls
echo Ping command was successful
echo Now we are setting the IP and HostName variable

for /F "tokens=3" %%a in ('ping %1 ^| find /i "TTL"') do set Address=%%a
for /F "tokens=2" %%a in ('ping -a %Address::=% ^| find /i "pinging"') do set HostName=%%a

set IPAddress=%Address::=%
cls
echo.
echo %1
echo %IPAddress%
echo %Hostname%
echo.
echo above is just to confirm that hostname,IP and FQDN is set
echo.
pause
cls
echo now we do a NSLOOKUP on the IPAddress collected from PING.
for /f "tokens=2" %%a in ('nslookup %IPAddress% ^| find /i "Name: " ') do set "nsNAME=%%a"
echo.
pause
cls
echo now we confirm that original hostname = FQDN
echo using NSLOOKUP details from previous commands
echo.
pause
cls
if "%nsname%"=="%Hostname%" (
set hnstatus="HOSTNAME is GOOD fix will be run"
) else (
set hnstatus="HOSTNAME is BAD we cannot do anything"
)
echo %hnstatus%
echo.
echo Hostname status above = GOOD or bad
echo if bad, then hostname resolves to different IP.
echo.
pause
cls

echo %strSvr%
echo just checking if we still have a machine name as a variable.

if "%nsname%"=="%Hostname%" (
echo f | xcopy /f /Y "c:\Temp\CM2014.2\ONLYWMI.bat" "\\%strSvr%\c$\Temp\CM2014.2\ONLYWMI.bat"
psexec \\%strSvr% c:\Temp\CM2014.2\ONLYWMI.bat
) else (
echo Hostname is bad cannot do anything
set hnstatusbad="Hostname is bad cannot do anything"
)
 
  goto logIt

:: **************************************************
::
:: LOGS ARE IMPORTANT!!
:: Get in the habit of logging the results of your
:: scripts. Verify the important pieces so you know
:: what has been completed and what you have to chase
:: down.
::
:: **************************************************

:Timedout
Echo %1, Request timed out.
Echo %1, Request timed out. >> fallouts_log.csv
goto :EOF

:ErrorMsg
Echo %1, Ping request could not find host.
Echo %1, Ping request could not find host. >> fallouts_log.csv
goto :EOF

:logIt

  echo.%strSvr%,%hnstatus%,%hnstatusbad%>>fallouts_log.csv
pause
:end

I got the original code somewhere on the internet, and removed most of its stuff (except the comments) added the ping parts and NSLOOKUP parts.

here is the ONLYWMI.bat file.

Code: [Select]
%windir%\system32\wbem\winmgmt /clearadap
%windir%\system32\wbem\winmgmt /kill
%windir%\system32\wbem\winmgmt /unregserver
%windir%\system32\wbem\winmgmt /reserver
%windir%\system32\wbem\winmgmt /resyncperf
net stop winmgmt /y
if exist %windir%\system32\wbem\repository.old rmdir /s /q %windir%\system32\wbem\repository.old
rename %windir%\system32\wbem\repository %windir%\system32\wbem\repository.old
regsvr32 /s %systemroot%\system32\scecli.dll
regsvr32 /s %systemroot%\system32\userenv.dll
mofcomp %windir%\system32\wbem\cimwin32.mof
mofcomp %windir%\system32\wbem\cimwin32.mfl
mofcomp %windir%\system32\wbem\rsop.mof
mofcomp %windir%\system32\wbem\rsop.mfl
for /f %s in ('dir /b /s %windir%\system32\wbem\*.dll') do regsvr32 /s %s
for /f %s in ('dir /b /s %windir%\system32\wbem\*.mof') do mofcomp %s
for /f %s in ('dir /b %windir%\system32\wbem\*.mfl') do mofcomp %s
net start winmgmt
%windir%\system32\wbem\wmiprvse /regserver

then list.txt just contains a list of hostnames.

Code: [Select]
computer1
computer2
computer3

I don't understand why it bombs out around line 15, which ends in .dll') do regsvr32 /s %s, and the last error before that is the syntax of the command is incorrect. and then Done!

any suggestions?
Title: Re: Fix WMI remotely with list of hostnames
Post by: Lemonilla on August 07, 2014, 04:04:07 PM
%s in a for loop should be escaped to %%s.