使用 PSwindowsUpdate 模块自动执行 Windows 服务器更新。问题

Automating Windows Server Updates with PSwindowsUpdate module. Issue

我正在尝试为多台服务器自动执行 windows 服务器更新安装。我已经在所有服务器上安装了该模块,还在 winrm 信任主机中添加了主机名。 所有服务器主机名都存储在 txt 文件中,并使用来自 PSwindowswupdate 模块的不同命令对每个循环进行循环处理。

$Hostname = Get-Content -Path "C:\TEMP\powershell_patching_script\module\hostnamesallwsus.txt"

Import-Module PSWindowsUpdate

foreach ($i in $Hostname) {

write-host $i


Get-WUHistory -ComputerName $i -last 3

}

问题是某些主机名的循环随机失败,错误为:

BGxxxxxxx01 #this is the hostname
Get-WUHistory : BGxxxxxxx01: Unknown failure. 
At C:\TEMP\powershell_patching_script\Module\History.ps1:10 char:1
+ Get-WUHistory -ComputerName $i -last 3
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : PermissionDenied: (:) [Get-WUHistory], Exception
    + FullyQualifiedErrorId : Unknown,PSWindowsUpdate.GetWUHistory 

如果我 运行 使用主机名而不是变量的命令,它将再次失败并出现相同的错误。 如果我 运行 相同但主机名前面有 $(即使未定义此类变量),该命令有效!非常适用

Get-WUHistory -ComputerName $BGxxxxxxx01 -last 3

本地执行的命令也有效。

这个问题似乎是在我列表中的多个主机名的随机基础上发生的。 我找不到任何关于此错误的有用信息。

我们将不胜感激任何帮助! 提前致谢!

我发现 Invoke-command 有效。 只需要将命令放在Invoke-command的脚本块中即可。