powershell invoke-command无法正常工作,但是psexec可以

时间:2015-04-09 15:29:04

标签: powershell powershell-remoting

我正在使用invoke-command cmdlet尝试使用powershell脚本在多台PC上卸载旧版本的Flash。该脚本运行没有错误,但永远不会卸载闪存。这是我正在使用的:

$flash = get-content "C:\11.6.txt"
$flash | Foreach {
if (Test-Connection -ComputerName $_ -Count 1 -ErrorAction SilentlyContinue) {
Invoke-Command -ComputerName $_ -ScriptBlock {Start-Process cmd.exe "/c start /wait MsiExec.exe /X{346137E0-7160-403B-AD21-3FF01D25037B} /qn" -NoNewWindow}
Write-Host "Uninstall from $_ complete."}
else {
Write-Host "$_ is offline"
}
}

如果我通过psexec运行卸载程序,它可以正常工作:

start /wait msiexec.exe /X{346137E0-7160-403B-AD21-3FF01D25037B} /qn

我不能为我的生活弄清楚为什么invoke-command不起作用。我对PowerShell相对较新,所以它可能是愚蠢的,但我无法理解。

2 个答案:

答案 0 :(得分:0)

在远程计算机上执行代码时,我从未使用Invoke-Command成功。然而,PSExec对我来说一点都不好。

尝试这样的事情。我现在无法测试,但请告诉我它是否适合您。 注意:将FilePath位置更改为PSExec.exe位置。

Start-Process -FilePath "C:\PSExec.exe" -ArgumentList {"\\$ComputerNameHere CMD.exe '/C start /wait msiexec.exe /X{346137E0-7160-403B-AD21-3FF01D25037B} /qn'"}

答案 1 :(得分:0)

以下是最终为我工作的代码:

$flashPuters = Get-Content "C:\11.8.800.txt"
foreach ($computer in $flashPuters) 
{
if (test-Connection -ComputerName $computer -Count 1 -ErrorAction SilentlyContinue) {
     & 'C:\PSTools\psexec.exe' @("\\$computer", 'msiexec.exe', '/X{BFBC6337-B7B9-4AEE-BC19-CA910EED755D}', '/qn')
     "Uninstall complete for $computer" >> C:\11.8.800.log 
}else {  
"$computer is not online" >> C:\11.8.800.log
} 

}

感谢@HiTech帮助我!

相关问题