【问题标题】:PowerShell Invoke-Command remotely using a sharePowerShell Invoke-Command 远程使用共享
【发布时间】:2018-07-18 11:06:26
【问题描述】:

我正在尝试使用 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 上不起作用,但上面的代码在两个操作系统上都有效! :)

【问题讨论】:

  • Credssp 正常工作可能有点痛苦。你能解释一下为什么你认为你需要使用 Credssp 吗?确保您的 $cred 对象不为空。
  • 我更喜欢使用会话,但我认为我必须使用 Credssp,因为我的 .exe 文件位于 (DFS) 共享上。当我使用第二个命令(使用会话)时,我在使用共享时收到拒绝访问错误。

标签: powershell invoke-command


【解决方案1】:

您是否真的在两台计算机上启用了 CredSSP?请参阅Enable-WSManCredSSP 命令。

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

Enable-WSManCredSSP -role Client -DelegateComputer "TargetComputer01"

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

现在在接收计算机上:

Enable-WSManCredSSP -role Server

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

让我知道这是否适合你!

【讨论】:

    猜你喜欢
    • 2014-10-02
    • 2018-03-07
    • 2016-09-24
    • 2012-03-28
    • 1970-01-01
    • 1970-01-01
    • 2016-12-05
    • 2022-12-02
    • 2019-07-07
    相关资源
    最近更新 更多