2015-03-13 51 views
1

我想在Powershell上运行psexec命令。我在Windows命令处理器上运行了这一行,它工作。Powershell上运行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" 
    } 
} 

这是我收到的错误:

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

尝试传递参数给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

+0

这工作。谢谢! – user3047391 2015-03-13 18:55:22

+0

不客气。我已经更新了我的答案,所以你可以真正理解为什么这会起作用;)。 – beatcracker 2015-03-13 19:12:30