我试图从在远程机器上执行的powershell脚本返回退出代码。但是,当我检查ExitCode
它有一些随机数。为什么我的powershell脚本返回错误的退出代码?
我在做什么错了?另外,是否可以返回全文?
我的脚本
$proc = Start-Process -Filepath "$PSExec" -ArgumentList "\\$server -h -u $user -p $pass -d PowerShell $command" -PassThru -Wait
$proc.ExitCode
远程脚本
New-Item "c:\temp\1.txt" -type file -force
exit 123
UPDATE
$secureString = ConvertTo-SecureString $password -Force -AsPlainText #$password includes password in clear text
$cred = New-Object System.Management.Automation.PSCredential($usrName, $secureString)
$sess = New-PSSession -ComputerName $serverName -Credential $cred
$command = "`"C:\temp\1.ps1`""
$result = Invoke-Command -Session $sess -ScriptBlock {
Start-Process -Filepath "$PSExec" -ArgumentList "\\$server -h -u $usrName -p $password -d PowerShell $command" -PassThru -Wait
}
@when我跑'$会议=新的PSSession -ComputerName $ SERVERNAME -Credential(GET-凭证)'它会打开一个弹出窗口中输入用户名和密码。我如何在脚本中传递用户名和密码? – theateist
它在'New-PSSession'上抛出'Logon failure'异常。我知道用户名和密码是正确的,因为'PSExec'成功访问服务器。我用代码添加了对我的文章的更新。谢谢 – theateist
它可以是我的密码的东西。我的密码是'$ password =“$ mypass!”'。这里没有显示,但是我在“$”之前使用了“'”字符来将它转义为“$”符号。这是对的吗? – theateist