Reputation: 11
I'm trying to write a script to check if the remote workstations have 64bit or 32bit operating system. The script simple should check if the directory "Program Files (x86)" exists and then echo the results to the output.txt file. For some reason or another the script doesn't even create the output.txt file. Any suggestions?
@echo off
SET output=D:\output.txt
IF EXIST "%output%" DEL "%output%"
FOR /f %%a IN (computers.txt) DO (
CALL :ping %%a
)
GOTO :EOF
:ping
SET pingtest=false
ping -n 1 %1 | find "approximate round trip time" >NUL || SET pingtest=true
set directorytest=true
set directory="\\%1%\c$\Program Files (x86)\"
IF pingtest==true (
dir %directory% >nul 2>nul
if errorlevel 1 (
set directorytest=false
)
IF directorytest==true ( echo "%1;64bit" >> "%output%" ) ELSE ( ECHO "%1;32bit" >> "%output%" )
)
IF pingtest==false (
ECHO "%1;offline" >> "%output%"
)
:EOF
Upvotes: 1
Views: 2170
Reputation: 1418
There are a couple of things wrong with your code.
When assigning your %directory% variable you use \\%1%\c$\Program Files (x86)
. Here %1%
should be %1
You are using the c$
share, which is mostly not available anymore (or at least not without administative privilidges)
It could work like this:
@echo off
setlocal enabledelayedexpansion
echo Welcome
call :ping 192.168.1.1
echo.
echo done with everything
goto eof
:ping
echo Probing %1 ...
set pingtest=false
ping -n 1 %1 | find "Approximate round trip" > nul
IF %ERRORLEVEL% == 0 (
set tdir="\\%1\c$\Program Files (x86)"
echo Looking up !tdir!
dir !tdir!
if !ERRORLEVEL! == 0 (
set arch=64bit
) else (
set arch=32bit
)
echo Architecutre of %1 is: !arch! >> output.txt
) else (
echo Remote host unreachable: %1 >> output.exe
)
goto eof
BUT:
This will return 64bit
for every machine that runs the microsoft sharing service or samba
and that does not share C$
to just anyone, as dir
will only return a non zero ERRORLEVEL
when the target does not provide that service or generally is unavailable.
On the other hand every machine that does not provide the shareing service at all, will be marked as 32bit
For any machine that does provide access to it's root drive (which none should) the script does work.
Here is a more reliably method of checking for 32/64 bit Windows OS
@echo off
set RemoteHost=192.168.1.100
set RegQry=HKLM\Hardware\Description\System\CentralProcessor\0
reg \\%RemoteHost%\%RegQry% > checkOS.txt
find /i "x86" < CheckOS.txt > StringCheck.txt
IF %ERRORLEVEL% == 0 (
echo "This is 32 Bit Operating system"
) ELSE (
echo "This is 64 Bit Operating System"
)
This one utilizes the reg.exe
that can query the registry of remote Windows NT
machines.
There is also the possiblity of utilizing wmic
: Example
Upvotes: 1
Reputation: 79983
IF pingtest==true (
will never be true because the string pingtest
and true
are not the same.
You need
IF %pingtest%==true (
Even when you've fixed that, there's a further gotcha:
Within a block statement (a parenthesised series of statements)
, the entire block is parsed and then executed. Any %var%
within the block will be replaced by that variable's value at the time the block is parsed - before the block is executed - the same thing applies to a FOR ... DO (block)
.
Hence, IF (something) else (somethingelse)
will be executed using the values of %variables%
at the time the IF
is encountered.
Two common ways to overcome this are 1) to use setlocal enabledelayedexpansion
and use !var!
in place of %var%
to access the changed value of var
or 2) to call a subroutine to perform further processing using the changed values.
Search for 'delayed expansion' on SO for many, many, many examples.
Upvotes: 0