使用 PowerShell 在远程服务器上安装可执行文件 (.msi/.exe) 时出错

Getting error in installing executables (.msi/.exe) on remote servers using PowerShell

我有一个可执行文件列表(.msi.exe),我必须将其安装在一些远程服务器上(在同一域中)静默 使用PowerShell 脚本。首先,我将所有程序从本地服务器复制到远程服务器。接下来,我试图将所有这些程序一个一个地安装到远程服务器上。为此,我使用以下代码:

Copy-Item -Path "C:\path\to\softwares\*" -Destination "C:\path\to\destination"  # this is copying all softwares on destination path

$destItem = Get-ChildItem -Path "C:\path\to\destination"

foreach($software in $destItem)
{
    $setup = Invoke-Command -ComputerName <computer> -ScriptBlock {$temp=Start-Process "C:\path\to$software" -ArgumentList "/s" -Wait -PassThrough;$Temp}
}

问题是: 当我 运行 这个脚本时,我得到以下错误:

[172.xx.xx.xxx] Connecting to remote server 172.xx.xx.xxx failed with the following error message : The WinRM client cannot process the request. Default authentication may be used with an IP address under the following conditions: the transport is HTTPS or the destination is in the TrustedHosts list, and explicit credentials are provided. Use winrm.cmd to configure TrustedHosts. Note that computers in the TrustedHosts list might not be authenticated. For more information on how to set TrustedHosts run the following command: winrm help config. For more information, see the about_Remote_Troubleshooting Help topic.
+ CategoryInfo : OpenError: (172.xx.xx.xxx:String) [], PSRemotingTransportException
+ FullyQualifiedErrorId : CannotUseIPAddress,PSSessionStateBroken

我 运行 在本地机器上命令 winrm quickconfig 并得到这个结果:

WinRM service is already running on this machine.
WinRM is already set up for remote management on this computer.

并且从远程服务器得到了这个输出:

WinRm already is set up to recieve requests on this machine.
WinRm already is set up for remote management on this machine.

问题是:如何解决这个问题并在远程服务器上安装可执行文件?

您可以尝试将远程计算机放入受信任主机列表中:

  • 查看 TrustedHosts 列表 get-item wsman:\localhost\Client\TrustedHosts

  • 将所有计算机(小心!)添加到 TrustedHosts 列表 set-item wsman:\localhost\Client\TrustedHosts -value *

  • 将具有特定 IP 地址的计算机添加到受信任主机列表 set-item wsman:\localhost\Client\TrustedHosts -value 192.168.0.10

查看 Matt Wrock 关于在 windows 上远程安装软件的文章:

http://www.hurryupandwait.io/blog/safely-running-windows-automation-operations-that-typically-fail-over-winrm-or-powershell-remoting

在他的 Boxstarter 库中,他通过使用 Invoke-FromTask 命令将命令包装在计划任务中来解决这个问题:

Invoke-FromTask @"
    Start-Process "$env:temp\net45.exe" -verb runas -wait `
      -argumentList "/quiet /norestart /log $env:temp\net45.log"
"@