PowerShell Invoke-Command使用共享远程执行

时间:2013-10-29 14:21:53

标签: powershell invoke-command

我正在尝试使用PowerShell在另一台计算机上远程安装软件(.exe)。我现在有:

Invoke-Command -Authentication Credssp -Credential $cred -ComputerName "TargetComputer01" -ScriptBlock {Start-Process -FilePath $args[0] -ArgumentList ('/log "{0}" /quiet /norestart' -f $args[1]) -Wait -PassThru -Verb RunAs} -ArgumentList @($Installer, $LogPath)

这不起作用,没有错误,没有日志文件,没有安装软件。所以我不知道它为什么不起作用。我使用Credssp是因为安装程序位于共享上。当我将安装程序放在TargetComputer01的某个位置时,它正在使用会话,请参阅:

Invoke-Command -Session $session -ScriptBlock {Start-Process -FilePath $args[0] -ArgumentList ('/log "{0}" /quiet /norestart' -f $args[1]) -Wait -PassThru -Verb RunAs} -ArgumentList @($Installer, $LogPath)

知道为什么使用Credssp的第一个命令不起作用?


是的我也使用这些命令启用了Credssp。我的脚本似乎在TargetComputer01(Windows Server 2012)上成功运行,但在TargetComputer02(Windows Server 2008 R2)上没有成功运行。 PowerShell版本相同,所有其他配置也相同(例如防火墙设置)。

但是我发现了一种使用PSSession的方法,请参阅以下代码:

$cred = Get-Credential -UserName "domain\username" -Message "Enter your credentials"
$session = New-PSSession -ComputerName "TargetComputer02" -Credential $cred -Authentication Credssp

Invoke-Command -Session $session -ScriptBlock {
    param
    (
        $Installer, $LogPath
    )
    Start-Process -FilePath $Installer -ArgumentList ('/log "{0}" /quiet /norestart' -f $LogPath) -Wait -PassThru -Verb RunAs
} -ArgumentList @($Installer, $LogPath)

我不确定为什么没有会话但使用Credssp的其他Invoke-Command在Windows Server 2008 R2上不起作用,但上述代码适用于两个操作系统! :)

1 个答案:

答案 0 :(得分:1)

你真的在两台电脑上启用了CredSSP吗?请参阅Enable-WSManCredSSP命令。

在客户端计算机或运行脚本的计算机上,您需要将其设置为将凭据委派给目标计算机:

Enable-WSManCredSSP -role Client -DelegateComputer "TargetComputer01"

您应该通过进入gpedit.msc - >来验证这是否已正确设置计算机配置 - >系统 - >证书授权。现在应该启用“委派新证书”,当您打开它的详细信息时,TargetComputer01应该显示为“WSMAN / TargetComputer01”

现在在接收计算机上:

Enable-WSManCredSSP -role Server

还要确保在TargetComputer01上运行Enable-PSRemoting

请告诉我这是否适合您!

相关问题