I need help to achieve the following through a batch file. Here’s what I’m trying to do,
-
Run psexec to execute “systeminfo” and find the string “System Type”
-
Determine the “System Type” (whether “X86-based PC” or “X64-based PC”) from the PsExec output.
-
Map the drive locally on my PC and copy the file based on the system type.
Here’s my script!
@echo off
set logs="C:\logs.txt"
for /f %%i in (C:\pro_arch.txt) do (
echo %%i >> %logs%
PsExec.exe \\%%i -u domain\username -p "password" systeminfo | findstr /c:"System Type" >> "%Logs%"
if "%%i"=="System Type: X86-based PC" (
net use m: \\%%i\C$ /user:domain\username "password"
xcopy "C:\batch_file\x86.txt" \\%%i\C$\_support\setup\ /y
net use m: /delete
) else (
net use m: \\%%i\C$ /user:domain\username "password"
xcopy "C:\batch_file\x64.txt" \\%%i\C$\_support\setup\ /y
net use m: /delete
)
)
The script works well untill this part
@echo off
set logs="C:\logs.txt"
for /f %%i in (C:\pro_arch.txt) do (
echo %%i >> %logs%
PsExec.exe \\%%i -u domain\username -p "password" systeminfo | findstr /c:"System Type" >> "%Logs%"
and outputs the following based on the system type:
System Type: X86-based PC
or
System Type: X64-based PC
The problem comes when the IF statement executes. It only copies the x64.txt to remote machines irrespective of the system type of the remote machine.
Command substitution using the psexec output doesn’t work either,
for /f "tokens=*" %%a in '(PsExec.exe \\IPaddress -u domain\username -p "password" systeminfo | findstr /c:"System Type")' do set myvar=%%a
it says
| was unexpected at this time
Could someone help?
%%i contains the computer name, not the results of your PsExec, so of course your IF statement will never be true.
You must either quote or escape special characters like
|when used in a FOR /F DO() clause. I recommend escaping as^|