当前位置:网站首页 > 更多 > 玩电脑 > 正文

[玩转系统] 使用 PSWindowsUpdate PowerShell 模块管理 Windows 更新

作者:精品下载站 日期:2024-12-14 23:19:21 浏览:12 分类:玩电脑

使用 PSWindowsUpdate PowerShell 模块管理 Windows 更新


您可以使用 PSWindowsUpdate PowerShell 模块从命令行管理 Windows 更新。 PSWindowsUpdate 模块未内置于 Windows 中,可从 PowerShell Gallery 存储库安装。 PSWindowsUpdate 允许管理员远程检查、安装、删除和隐藏 Windows 服务器和工作站上的更新。 PSWindowsUpdate 模块对于管理 Windows Server Core 或 Hyper-V Server(没有 GUI)上的更新以及在审核模式下配置 Windows 映像时特别有价值。

安装 PSWindowsUpdate 模块

您可以使用以下命令从在线存储库 (PSGallery) 在 Windows 10/11 和 Windows Server 2022/2019/2016 上安装 PSWindowsUpdate 模块:

Install-Module -Name PSWindowsUpdate -Force

安装完成后,需要检查包:

Get-Package -Name PSWindowsUpdate

[玩转系统] 使用 PSWindowsUpdate PowerShell 模块管理 Windows 更新

在早期版本的 Windows 2012R2/Windows 8.1 及更低版本上安装 PowerShell 模块时,您可能会收到错误:

Install-Module: Unable to download from URI.Unable to download the list of available providers. Check your internet connection.

安装模块需要使用TLS 1.2协议进行连接。使用以下命令为当前 PowerShell 会话启用它:

[Net.ServicePointManager]::SecurityProtocol = [Net.SecurityProtocolType]::Tls12

https://a-d.site/powershell-install-module-unable-download-uri/

如果您有较旧的 Windows 版本 (Windows 7/8.1/Windows Server 2008 R2/2012 R2) 或者您没有直接访问 Internet,您可以手动安装 PSWindowsUpdate(请参阅指南“如何离线安装 PowerShell 模块?”)。

  1. 将 PSWindowsUpdate 模块下载到任何在线计算机上:

    Save-Module -Name PSWindowsUpdate -Path C:\ps\

    ;

  2. 将模块复制到目标计算机上的以下文件夹

    %WINDIR%\System32\WindowsPowerShell\v1.0\Modules

    ;

    [玩转系统] 使用 PSWindowsUpdate PowerShell 模块管理 Windows 更新

  3. 配置PowerShell脚本执行策略:

    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned -force
  4. 您现在可以将该模块导入到 PowerShell 会话中:

    Import-Module PSWindowsUpdate

注意。在 Windows 7/Windows Server 2008 R2 中,导入 PSWindowsUpdate 模块时,可能会出现以下错误:术语“Unblock-File ”未被识别为 cmdlet 的名称。原因是该模块使用了一些仅在PowerShell 3.0中出现的功能。要使用这些功能,您必须更新 PowerShell 版本或删除

| Unblock-File

手动从 PSWindowsUpdate.psm1 文件中获取行。

在您的计算机上安装 PSWindowsUpdate 模块后,您可以使用 Update-WUModule cmdlet 将其远程安装到其他计算机或服务器上。例如,要将 PSWindowsUpdate 模块从您的计算机复制到两个远程主机,请运行以下命令(您需要通过 WinRM 协议访问远程服务器):

$Targets = "lon-fs02", "lon-db01"
Update-WUModule -ComputerName $Targets -Local

要将 PoSh 模块保存(导出)到共享网络文件夹以便进一步导入到其他计算机上,请运行:

Save-Module -Name PSWindowsUpdate -Path \lon-fs02\psmodules\

PSWindowsUpdate Cmdlet 列表

您可以在 PSWindowsUpdate 模块中显示可用 cmdlet 的列表,如下所示:

get-command -module PSWindowsUpdate

简单介绍一下模块命令的用法:

  • Clear-WUJob - 使用Get-WUJob清除任务计划程序中的WUJob;

  • 下载-WindowsUpdate(别名为

     Get-WindowsUpdate -Download

    ) — 获取更新列表并下载它们;

  • Get-WUInstall、Install-WindowsUpdate(别名为

    Get-WindowsUpdate -Install

    ) - 安装 Windows 更新;

  • 隐藏 WindowsUpdate(别名为

    Get-WindowsUpdate -Hide:$false

    ) - 隐藏更新;

  • Uninstall-WindowsUpdate - 使用Remove-WindowsUpdate删除更新;

  • Add-WUServiceManager - 在计算机上注册更新服务器(Windows Update Service Manager);

  • Enable-WURemoting — 启用 Windows Defender 防火墙规则以允许远程使用 PSWindowsUpdate cmdlet;

  • Get-WindowsUpdate (Get-WUList) — 显示符合指定条件的更新列表,允许您查找并安装更新。这是 PSWindowsUpdate 模块的主要 cmdlet。允许从 WSUS 服务器或 Microsoft 更新下载并安装更新。允许您选择更新类别、特定更新并设置安装更新时计算机重新启动的规则;

  • Get-WUApiVersion - 获取计算机上的 Windows Update Agent 版本;

  • Get-WUHistory - 显示已安装更新的列表(更新历史记录);

  • Get-WUInstallerStatus — 检查 Windows Installer 服务状态;

  • Get-WUJob - 检查任务计划程序中的 WUJob 更新任务;

  • Get-WULastResults — 上次搜索和安装更新的日期(LastSearchSuccessDateLastInstallationSuccessDate);

  • Get-WURebootStatus — 允许您检查是否需要重新启动才能应用特定更新;

  • Get-WUServiceManager - 列出更新源;

  • Get-WUSettings - 获取 Windows 更新客户端设置;

  • Invoke-WUJob - 远程调用任务计划程序中的WUJobs任务,立即执行PSWindowsUpdate命令;

  • Remove-WindowsUpdate - 允许通过 KB ID 卸载更新;

  • Remove-WUServiceManager - 禁用 Windows Update 服务管理器;

  • Set-PSWUSettings - 将 PSWindowsUpdate 模块设置保存到 XML 文件;

  • Set-WUSettings - 配置 Windows 更新客户端设置;

  • Update-WUModule - 更新 PSWindowsUpdate 模块(您可以通过从当前计算机复制模块或从 PSGallery 更新来更新远程计算机上的模块);

  • Reset-WUComponents - 允许您将计算机上的 Windows Update 代理重置为默认状态。

[玩转系统] 使用 PSWindowsUpdate PowerShell 模块管理 Windows 更新

要检查当前的 Windows 更新客户端设置,请运行以下命令:

Get-WUSettings 
ComputerName                                 : WKS5S2N39S2
WUServer                                     : http://MN-WSUS:8530
WUStatusServer                               : http://MN-WSUS:8530
AcceptTrustedPublisherCerts                  : 1
ElevateNonAdmins                             : 1
DoNotConnectToWindowsUpdateInternetLocations : 1
TargetGroupEnabled                           : 1
TargetGroup                                  : ServersProd
NoAutoUpdate                                 : 0
AUOptions                                    : 3 - Notify before installation
ScheduledInstallDay                          : 0 - Every Day
ScheduledInstallTime                         : 3
UseWUServer                                  : 1
AutoInstallMinorUpdates                      : 0
AlwaysAutoRebootAtScheduledTime              : 0
DetectionFrequencyEnabled                    : 1
DetectionFrequency                         : 4

在此示例中,计算机上的 Windows Update 代理配置有 GPO,以从本地 WSUS 服务器接收更新。

Reset-WUComponents -Verbose

cmdlet 允许您重置所有 Windows Update 代理设置、重新注册库以及恢复

wususerv

服务恢复到默认状态。

[玩转系统] 使用 PSWindowsUpdate PowerShell 模块管理 Windows 更新

使用 PowerShell 扫描并下载 Windows 更新

您可以使用以下命令列出更新服务器上当前计算机可用的更新

Get-WindowsUpdate

或者

Get-WUList

命令。

[玩转系统] 使用 PSWindowsUpdate PowerShell 模块管理 Windows 更新

要检查远程计算机上的可用更新列表,请运行以下命令:

Get-WUList -ComputerName server2

您可以检查您的 Windows 应从哪里接收更新。运行以下命令:

Get-WUServiceManager
ServiceID IsManaged IsDefault Name
--------- --------- --------- ----
8b24b027-1dee-babb-9a95-3517dfb9c552 False False DCat Flighting Prod
855e8a7c-ecb4-4ca3-b045-1dfa50104289 False False Windows Store (DCat Prod)
3da21691-e39d-4da6-8a4b-b43877bcb1b7 True True Windows Server Update Service
9482f4b4-e343-43b6-b170-9a65bc822c77 False False Windows Update

[玩转系统] 使用 PSWindowsUpdate PowerShell 模块管理 Windows 更新

如您所见,计算机配置为从本地 WSUS 服务器接收更新(Windows Server Update Service=True)。在这种情况下,您应该会看到为您的计算机批准的更新列表。

如果要针对 Internet 上的 Microsoft 更新服务器扫描计算机(除了 Windows 更新之外,这些服务器还包含 Office 和其他 Microsoft 产品更新),请运行以下命令:

Get-WUlist -MicrosoftUpdate

您将收到此警告:

Get-WUlist : Service Windows Update was not found on computer. Use Get-WUServiceManager to get registered service.

要允许扫描 Microsoft 更新,请运行以下命令:

Add-WUServiceManager -ServiceID "7971f918-a847-4430-9279-4a52d1efe18d" -AddServiceFlag 7

您现在可以扫描 Microsoft 更新。在本例中,发现了 Microsoft Visual C++ 2008 和 Microsoft Silverlight 的其他更新。

[玩转系统] 使用 PSWindowsUpdate PowerShell 模块管理 Windows 更新

要检查计算机上 Windows Update 代理的版本,请运行以下命令:

Get-WUApiVersion
ComputerName PSWindowsUpdate PSWUModuleDll ApiVersion WuapiDllVersion
------------ --------------- ------------- ---------- ---------------
DESKTOP-J... 2.1.1.2 2.2.0.2 8.0 10.0.19041.1320

[玩转系统] 使用 PSWindowsUpdate PowerShell 模块管理 Windows 更新

要从计算机收到的更新列表中删除特定产品或知识库,您可以通过以下方式排除它们:

  • 类别 (

    -NotCategory

    );

  • 标题 (

    -NotCategory

    );

  • 更新号(

    -NotKBArticleID

    )。

例如,我们从列表中排除 OneDrive、驱动程序更新和特定 KB:

Get-WUlist -NotCategory "Drivers" -NotTitle "OneDrive" -NotKBArticleID KB4489873

使用 PowerShell 安装 Windows 更新 (Install-WindowsUpdate)

要从 Windows 更新服务器(而不是本地 WSUS)自动下载并安装 Windows 设备的所有可用更新,请运行以下命令:

Install-WindowsUpdate -MicrosoftUpdate -AcceptAll -AutoReboot

AcceptAll 参数接受所有更新包的安装,AutoReboot 允许 Windows 在安装更新后自动重新启动。

您还可以使用以下选项:

  • IgnoreReboot - 禁用自动重启;

  • ScheduleReboot - 设置重新启动计算机的确切时间。

您可以将更新安装历史记录保存到日志文件(您可以使用它而不是 WindowsUpdate.log 文件)。

Install-WindowsUpdate -AcceptAll -Install -AutoReboot | Out-File "c:\logs$(get-date -f yyyy-MM-dd)-WindowsUpdate.log" -force

您只能按 KB 编号安装特定更新包:

Get-WindowsUpdate -KBArticleID KB2267602, KB4533002 -Install

[玩转系统] 使用 PSWindowsUpdate PowerShell 模块管理 Windows 更新

在这种情况下,您需要手动确认每个更新的安装。

如果您想从安装列表中排除某些更新,请运行以下命令:

Install-WindowsUpdate -NotCategory "Drivers" -NotTitle OneDrive -NotKBArticleID KB4011670 -AcceptAll -IgnoreReboot

使用 PowerShell 在远程计算机上安装 Windows 更新

PSWindowsUpdate 模块允许您同时在多个工作站或服务器上远程安装更新(必须在这些计算机上安装/导入 PSWindowsUpdate)。这非常方便,因为管理员不必手动登录远程 Windows 主机来安装更新。必须在远程计算机上启用和配置 WinRM(手动或通过 GPO)。

几乎所有 PSWindowsUpdate 模块 cmdlet 都允许您使用以下命令在远程计算机上管理和安装 Windows 更新

-Computername

属性。

在远程计算机上安装 PSWindowsUpdate 模块,并允许通过动态 RPC 端口访问 Windows Defender 防火墙中的 dllhost.exe 进程。您可以使用 Invoke-Command cmdlet 在远程计算机上配置 PSWindowsUpdate 模块:

$Targets = "lon-fs02", "lon-db01"
Invoke-Command -ComputerName $Target -ScriptBlock {Set-ExecutionPolicy RemoteSigned -force }
Invoke-Command -ComputerName $Target -ScriptBlock {Import-Module PSWindowsUpdate; Enable-WURemoting}

PSWindowsUpdate 模块可用于远程管理 AD 域和工作组中的计算机上的 Windows 更新(需要工作组环境的 PowerShell Remoting 配置)。

为了管理远程计算机上的更新,您需要将主机名添加到 winrm 可信主机列表中或通过 HTTPS 配置 PowerShell 远程处理 (WinRM):

winrm set winrm/config/client '@{TrustedHosts="server1,server2,…"}'

或者使用 PowerShell :

Set-Item wsman:\localhost\client\TrustedHosts -Value server1 -Force

以下命令将在三台远程 Windows 主机上安装所有可用更新:

$ServerNames = "server1, server2, server3"
Invoke-WUJob -ComputerName $ServerNames -Script {ipmo PSWindowsUpdate; Install-WindowsUpdate -AcceptAll | Out-File C:\Windows\PSWindowsUpdate.log } -RunNow -Confirm:$false -Verbose -ErrorAction Ignore

Invoke-WUJob cmdlet(以前称为 Invoke-WUInstall)将在远程计算机上创建在本地 SYSTEM 帐户下运行的计划程序任务。

您可以指定安装 Windows 更新的确切时间:

Invoke-WUJob -ComputerName $ServerNames -Script {ipmo PSWindowsUpdate; Install-WindowsUpdate -AcceptAll -AutoReboot | Out-File C:\Windows\PSWindowsUpdate.log } -Confirm:$false -TriggerDate (Get-Date -Hour 22 -Minute 0 -Second 0)

您可以使用 Get-WUJob 检查更新安装任务的状态:

Get-WUJob -ComputerName $ServerNames

如果该命令返回空列表,则所有计算机上的更新安装任务均已完成。

您可以在远程计算机上安装更新并向管理员发送电子邮件报告:

Install-WindowsUpdate -ComputerName nysrv1 -MicrosoftUpdate -AcceptAll - IgnoreReboot -SendReport -PSWUSettings @{SmtpServer="smtp.a-d.site";From="[email protected]";To="[email protected]";Port=25} -Verbose

使用 PowerShell 检查 Windows 更新历史记录 (Get-WUHistory)

使用 Get-WUHistory cmdlet,您可以自动或手动获取计算机上先前安装的更新列表。

[玩转系统] 使用 PSWindowsUpdate PowerShell 模块管理 Windows 更新

您可以获得有关特定更新的安装日期的信息:

Get-WUHistory| Where-Object {$_.Title -match "KB4517389"} | Select-Object *|ft

[玩转系统] 使用 PSWindowsUpdate PowerShell 模块管理 Windows 更新

要查明特定更新是否已安装在多台远程计算机上,您可以使用以下 PowerShell 代码:

"server1","server2" | Get-WUHistory| Where-Object {$_.Title -match "KB4011634"} | Select-Object *|ft

检查安装更新后是否需要重新启动计算机(等待重新启动):

Get-WURebootStatus -ComputerName WKS21TJS

[玩转系统] 使用 PSWindowsUpdate PowerShell 模块管理 Windows 更新

检查 RebootRequiredRebootScheduled 属性的值。

您可以使用 Get-ADComputer cmdlet(来自 Active Directory for PowerShell 模块)生成包含域中所有计算机上次安装更新的日期的报告:

$Computers=Get-ADComputer -Filter {enabled -eq "true" -and OperatingSystem -Like '*Windows*' }
Foreach ($Computer in $Computers)
{
Get-WULastResults -ComputerName $Computer.Name|select ComputerName, LastSearchSuccessDate, LastInstallationSuccessDate
}

以此类推,您可以找到超过 60 天未安装更新的计算机,并将结果显示在 Out-GridView 交互式表格中:

$result=@()
Foreach ($Computer in $Computers) {
$result+= Get-WULastResults -ComputerName $Computer.Name
}
$result| Where-Object { $_.LastInstallationSuccessDate -lt ((Get-Date).AddDays(-60)) }| Out-GridView

使用 PowerShell 卸载 Windows 更新 (Remove-WindowsUpdate)

您可以使用 Remove-WindowsUpdate cmdlet 通过 PowerShell 正确卸载更新。只需将 KB 编号指定为 KBArticleID 参数的参数即可。要延迟计算机自动重新启动,请添加 -NoRestart 选项:

Remove-WindowsUpdate -KBArticleID KB4489873 -NoRestart

如何使用 PowerShell 隐藏 Windows 更新?

您可以隐藏特定更新,这样 Windows 更新服务就永远不会在您的计算机上安装它们(大多数情况下您需要隐藏驱动程序更新)。例如,要隐藏 KB4489873 和 KB4489243 更新,请运行以下命令:

$HideList = "KB4489873", "KB4489243"
Get-WindowsUpdate -KBArticleID $HideList -Hide

[玩转系统] 使用 PSWindowsUpdate PowerShell 模块管理 Windows 更新

现在,下次使用 Get-WUlist 命令扫描更新时,隐藏的更新将不会显示在可安装的更新列表中。

您可以通过以下方式显示此计算机上隐藏的更新列表:

Get-WindowsUpdate -IsHidden

请注意,

H

(隐藏)属性已出现在隐藏更新的状态栏中。

[玩转系统] 使用 PSWindowsUpdate PowerShell 模块管理 Windows 更新

要取消隐藏某些更新,请运行以下命令:

Get-WindowsUpdate -KBArticleID $HideList -WithHidden -Hide:$false

或者 :

Show-WindowsUpdate -KBArticleID $HideList

对于那些对 PowerShell 控制台感到不舒服的人,我建议使用图形化的 Windows Update MiniTool 来管理 Windows 10/11 和 Windows Server 2022/2019 中的更新。

您需要 登录账户 后才能发表评论

取消回复欢迎 发表评论:

关灯