1

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
1
  • without having tested your code: There is a % too much in set directory="\\%1%\c$\Program Files (x86)\" (probably a typo, because you are using %1` correctly in other lines). Commented Apr 14, 2014 at 15:42

2 Answers 2

1

There are a couple of things wrong with your code.

  1. When assigning your %directory% variable you use \\%1%\c$\Program Files (x86). Here %1% should be %1

  2. 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"
)

[source]

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

Sign up to request clarification or add additional context in comments.

5 Comments

Right, that was an error on my part (I had the wrong find string and got a zero errorlevel all the time) ... just another brick in the wall
Now you fell into the famous "delayed expansion trap" ^^
I did not since I have not been using the for loop
but you use a (block) with if. For explanation see @Magoo `s answer.
Right. True the second ERRORLEVEL check and the tdir variable were faulty. Corrected those. But the paragraph after BUT remains true, since the reasons were not acquired on empirical basis but rather on the facts how dir produces an errorlevel and of course the fact that relying on the C$ share is unreliable at best of times.
0
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.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.