我正在尝试在Powershell上运行psexec命令。我在Windows命令处理器上运行了这一行并且它运行良好。
$computers = gc "C:\temp\scripts\computers.txt"
foreach ($computer in $computers)
{
if (test-Connection -Cn $computer -quiet) {
& C:\temp\sysinternals\psexec.exe \\$computer "C:\Temp\CMA4.5\frminst.exe/forceuninstall/silent"
}else {
"$computer is not online"
}
}
这是我得到的错误:
psexec.exe : At C:\temp\Scripts\CopyFiles.ps1:19 char:9
+ & C:\temp\sysinternals\psexec.exe \\$computer "C:\Temp\CMA4.5\frminst.ex ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:String) [], RemoteException
+ FullyQualifiedErrorId : NativeCommandError
PsExec v1.94 - Execute processes remotely
Copyright (C) 2001-2008 Mark Russinovich
Sysinternals - www.sysinternals.com
The system cannot find the path specified.
Connecting to bdp00051291...
Starting PsExec service on bdp00051291...
Connecting with PsExec service on bdp00051291...
Starting C:\Temp\CMA4.5\frminst.exe/forceuninstall/silent on bdp00051291...
PsExec could not start C:\Temp\CMA4.5\frminst.exe/forceuninstall/silent on bdp00051291:
答案 0 :(得分:0)
尝试以这种方式将参数传递给PsExec
:
$computers = gc "C:\temp\scripts\computers.txt"
foreach ($computer in $computers)
{
if (test-Connection -Cn $computer -quiet) {
& 'C:\temp\sysinternals\psexec.exe' @("\\$computer", 'C:\Temp\CMA4.5\frminst.exe', '/forceuninstall', '/silent')
}else {
"$computer is not online"
}
}
这是PowerShell的秘密。如果指定值数组,它将自动将它们扩展为单独的参数。在99.99%的情况下,这将起作用并使您免于丑陋的字符串连接和弄乱引号。
您可以在本综合文章中阅读有关将参数从PowerShell传递到外部可执行文件的更多信息:PowerShell and external commands done right。