Powershell脚本在本地计算机上执行命令行
$j = "remote_machine"
$comp = "\\"+$j
$command = 'D:\PSTools\PsExec.exe $comp -u Administrator -p plaintextpassword -accepteula powershell.exe c:\share\script.ps1'
Invoke-Expression "& $command"
这有效,但输出以下内容
PsExec.exe:在第1行:char:1 +&D:\ PSTools \ PsExec.exe $ comp -u管理员-p plaintextpassword -accepteula powersh ... + ~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ ~~~~~~~~~~~~~~~ + + CategoryInfo:(:String)[],RemoteException + FullyQualifiedErrorId:NativeCommandError PsExec v2.0-远程执行进程版权所有(C)2001-2013 Mark Russinovich Sysinternals-www.sysinternals.com连接到remote_machine ...在remote_machine上启动PSEXESVC服务...在remote_machine上连接PsExec服务...在remote_machine上启动powershell.exe ...在remote_machine上退出powershell.exe,错误代码为0。
怎么修?
重新阅读您的问题后,我看到这是之前已经讨论过的标准PSExec vs PowerShell问题。这是由于PSExec将其某些文本的标题行输出到stderr(标准错误)流的事实。执行工作正常,并且确实显示退出代码0,指示实际上没有错误。
仅在PowerShell ISE中,而不在标准PowerShell控制台中,此问题才明显(除非使用PSExec <command & args> 2>&1
或类似方法将StdErr重定向到StdOut )。要解决此问题,如果要在ISE中运行脚本,则可以使用Start-Process的-RedirectStandardError参数,或通过其他方式重定向StdErr。
本文收集自互联网,转载请注明来源。
如有侵权,请联系 [email protected] 删除。
我来说两句