运行 Powershell 上的 psexec

Running psexec on Powershell

我正在尝试 运行 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:

尝试以这种方式将参数传递给 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