使用 PSwindowsUpdate 模块自动化 Windows Server 更新问题

问题描述

我正在尝试为多个服务器自动安装 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的脚本块中即可。