2017-02-17 38 views
0

我试图从在远程机器上执行的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 
} 

回答

0

你能使用Invoke-Command作为替代?

实施例:

$session = New-PSSesson -ComputerName $serverName -Credential (Get-Credential) 
$result = Invoke-Command -Session $session -ScriptBlock { 
    Start-Process ... 
} 

作为替代Get-Credential可以创建凭据对象,并且经由-Credential放慢参数把它传递给Invoke-Command。例如:

$secureString = ConvertTo-SecureString $password -Force -AsPlainText #$password includes password in clear text 
$cred = [System.Management.Automation.PSCredential]::new($usrName, $secureString) 
$sess = New-PSSession -ComputerName $ComputerName -Credential $cred 
Invoke-Command -Session $sess -ScriptBlock { ... } 

$result还应包括ExitCode的性质,因为PowerShell远程串行远程对象。我总是建议将Powershell Remoting与cmdlet特定的ComputerName实现进行比较。它使用更加标准化的方式(WsMan - > HTTP(S))。有关更多详细信息,请参阅此link

希望有所帮助。

+0

@when我跑'$会议=新的PSSession -ComputerName $ SERVERNAME -Credential(GET-凭证)'它会打开一个弹出窗口中输入用户名和密码。我如何在脚本中传递用户名和密码? – theateist

+0

它在'New-PSSession'上抛出'Logon failure'异常。我知道用户名和密码是正确的,因为'PSExec'成功访问服务器。我用代码添加了对我的文章的更新。谢谢 – theateist

+0

它可以是我的密码的东西。我的密码是'$ password =“$ mypass!”'。这里没有显示,但是我在“$”之前使用了“'”字符来将它转义为“$”符号。这是对的吗? – theateist

0

对于你的第一种方法,你的问题是当运行psexec-d(不要等待)标志时,它会返回启动它的命令的pid,而不是等待并返回退出码。

总而言之,您的流程也可以进行优化。首先,如果您想使用psexec.exe,我没有看到Start-Process的原因,因为您正在等待并通过。只需& $psexec ...就足够了。

然而Moerwald的建议使用Invoke-Command是一个很好的建议。在您更新的代码中,您仍在运行不需要的Start-ProcessPsexec。当你调用的命令,你已经在远程运行代码,所以只需运行该代码:

$secureString = ConvertTo-SecureString $password -Force -AsPlainText 
$cred = New-Object System.Management.Automation.PSCredential($usrName, $secureString) 
$result = Invoke-Command -ComputerName $serverName -Credential $cred -ScriptBlock { 
    New-Item "c:\temp\1.txt" -type file -force 
    exit 123 
} 

而且,因为它看起来并不像你正在重用的会议上,我放弃了保存会话到变量。最好用Get-Credential替换所有的凭证设置,而不是传递明文密码(避免密码以保存的转录本结尾)。这将是这样的:

$result = Invoke-Command -ComputerName $serverName -Credential (Get-Credential) -ScriptBlock { 
    New-Item "c:\temp\1.txt" -type file -force 
    exit 123 
} 
+0

但您的最后一个代码将弹出凭据窗口来输入用户名和密码 – theateist

+0

我想要做的是在远程计算机上运行powershell脚本。为此,我需要首先在远程计算机上启用'ExecutionPolicy'。我不想手动做,但一切都远程。我有VPN到远程机器。所以,我所做的是将批处理文件复制到远程计算机,该计算机使用'ExecutionPolicy = RemoteSigned'添加一个注册表。我想用'PsExec'来执行该批处理文件,然后我就可以在该远程机器上运行powershell脚本。 'Invoke-Command'要求我配置所有的远程服务器。有了'PsExec',我不必配置任何东西。 – theateist

+0

在任何情况下,我删除了'-d',但仍然没有退出代码。你可以请发表一个工作的例子吗? – theateist

相关问题