我需要帮助才能通过批处理文件实现以下功能。这是我正在尝试做的事情,
运行psexec执行“systeminfo”并找到字符串“System Type”
从PsExec输出确定“系统类型”(无论是“基于X86的PC”还是“基于X64的PC”)。
在我的电脑上本地映射驱动器,并根据系统类型复制文件。
这是我的剧本!
@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
)
)
脚本运行良好,直到这部分
@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%"
并根据系统类型输出以下内容:
系统类型:基于X86的PC 要么 系统类型:基于X64的PC
IF语句执行时出现问题。它只将x64.txt复制到远程计算机,而不管远程计算机的系统类型如何。
使用psexec输出的命令替换也不起作用,
for /f "tokens=*" %%a in '(PsExec.exe \\IPaddress -u domain\username -p "password" systeminfo | findstr /c:"System Type")' do set myvar=%%a
它说
| was unexpected at this time
有人可以帮忙吗?
答案 0 :(得分:2)
%% i包含计算机名称,而不是PsExec的结果,因此您的IF语句当然永远不会成立。
在FOR / F DO()子句中使用时,您必须引用或转义|
之类的特殊字符。我建议转义为^|
@echo off
set logs="C:\logs.txt"
for /f %%i in (C:\pro_arch.txt) do (
echo %%i >> %logs%
for /f "delims=" %%A in ('PsExec.exe \\%%i -u domain\username -p "password" systeminfo ^| findstr /c:"System Type"') do (
>> "%Logs%" echo %%A
if "%%A"=="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
)
)
)