使用 PSWindowsUpdate Powershell 模块根据关键字隐藏 Windows 更新

使用 PSWindowsUpdate Powershell 模块根据关键字隐藏 Windows 更新

我正在尝试在我工作的图书馆自动执行整个更新过程。我编写了一个简单的脚本,它允许我使用 Chocolatey 更新我们所有的软件包,并使用 PSWindowsUpdate 模块执行 Windows 更新。但是,在使用 PSWindowsUpdate 隐藏 Windows 更新时,我遇到了一些困难。根据我读过的各种在线文章/文档,这似乎很简单,但出于某种原因,它对我来说不起作用。我特别参考了这个 Stackoverflow 问题并尝试将接受的答案作为我的解决方案,但没有得到任何回应。这里的答案也没有完全说明我想要完成的任务。让我先提供一些背景信息:

背景

我有 5 个 Windows 10 Pro 系统和 11 个 Windows 7 Pro 系统。全部都是 64 位的。Windows 7 系统之前都已更新到 WMF 5.1。我使用远程桌面企业版在多台计算机上同时执行脚本,以帮助我简化更新过程。

我尝试过的

我花了相当多的时间尝试通过各种 Google 搜索和使用 PS 脚本反复试验来解决自己的问题。当我在远程机器上执行脚本时,一切都运行良好,更新也很好。问题在于,正如我已经提到的,它并不总是隐藏我指定的 Windows 更新(因为我可能在某个地方搞砸了哈哈)。这是我的脚本:

#This script will update all chocolatey packages and will also download install any new Windows Updates

#Begin ExecutionPolicy Set
Echo "Setting Execution Policy Settings"
Set-ExecutionPolicy -Scope LocalMachine -ExecutionPolicy RemoteSigned -Force -Confirm:$false
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy Bypass -Force -Confirm:$false

Echo "Initializing Unattended Update Automation Powershell Script"

#Begin Chocolatey Update
Echo "Updating Chocolatey Packages"

#Remove Java and reinstall 64-bit  only
cuninst jre8 -y
cinst jre8 -PackageParameters "/exclude:32" -y

#Queries Chocolatey.org database for updates to locally installed packages and updates them if necessary
cup all -y --ignore-checksums

#Begin Windows Update
Echo "Enabling Windows Update Services"
Get-Service bits, wuauserv | Set-Service -StartupType manual

Echo "Copying Module PSWindowsUpdate to PowerShell Modules Folder"
#Grabs copy of PSWindowsUpdate Module and copies it to remote machine
Copy-Item -Path \\DIROFFICE\WindowsUpdate\PSWindowsUpdate -Destination C:\Windows\System32\WindowsPowerShell\v1.0\Modules -recurse -force

Echo "Importing Powershell Module PSWindowsUpdate"
Import-Module -Name PSWindowsUpdate -force
refreshenv

Echo "Enabling updates for additional Microsoft components and software"
Add-WUServiceManager -ServiceID 7971f918-a847-4430-9279-4a52d1efe18d -Confirm:$false

Echo "Querying Microsoft Update Server for Windows Updates"
Get-WUList -MicrosoftUpdate

Echo "Hiding Unnecessary Updates"
Hide-WUUpdate -Title "Update for Microsoft OneDrive" -HideStatus:$true -Confirm:$false

Echo "Downloading and Installing Windows Updates"
Get-WUInstall -MicrosoftUpdate -acceptall

Echo "Stopping Windows Update Services"
Get-Service bits,wuauserv | Stop-Service

Echo "Setting CurrentUser Execution Policy Back to RemoteSigned"
Set-ExecutionPolicy -Scope CurrentUser -ExecutionPolicy RemoteSigned -Force -Confirm:$false

问题/疑问

我最终想要实现的是根据更新标题中出现的某些关键字隐藏某些更新。例如,我们在所有计算机上都安装了 Microsoft 2016,但我希望能够隐藏 W10 和 W7 计算机上的所有 Outlook、OneDrive、Visio 和 Skype 更新,以及 W7 计算机上的 Microsoft Security Essentials。有没有一种有效的方法可以使用 PSWindowsUpdate 模块来隐藏包含某些单词的更新?

补充笔记

我的脚本中隐藏更新部分下列出的一小行代码仅用于测试目的。我最初尝试使用所述更新的完整标题来隐藏更新。例如,Outlook 2016 有许多更新,其标题要么是“Microsoft Outlook 2016 安全更新”,要么只是简单的“Microsoft Outlook 2016 更新”。我尝试使用Hide-WUUpdate -Title "Update for Microsoft Outlook" -HideStatus:$true -Confirm:$false以及Hide-WUUpdate -Title "Security update for Microsoft Outlook" -HideStatus:$true -Confirm:$false。这似乎不起作用,因为当我查看已安装的更新时,它显示已安装 4 或 5 个 Microsoft Outlook 更新。

我也尝试过使用通配符 (*),但我认为我并不完全理解它的工作原理。我尝试了一些方法,例如Hide-WUUpdate -Title "Update for Microsoft Outlook*" -HideStatus:$true -Confirm:$falseHide-WUUpdate -Title "Outlook*" -HideStatus:$true -Confirm:$false。我甚至尝试过使用双通配符,例如Hide-WUUpdate -Title "*Microsoft Oulook*" -HideStatus:$true -Confirm:$false

从我在 TechNet 和 MSDN 上读到的内容来看,(*) 通配符将匹配从指定位置开始的字符。如果包含空格,这会中断吗?

使用知识库文章 ID 不是我的首选方法,因为我每次都必须用需要隐藏的新知识库 ID 更新脚本。如果可能的话,我更喜欢关键字方法。我确实尝试使用上述 Stackoverflow 参考中列出的方法,只是为了好玩,但这最终不是我现在正在寻找的解决方案。

答案1

Hide-WUUpdate可能不支持一次更新多条记录。

在 Powershell 中,一种常见的方法是缩小管道前端的对象范围,然后将它们发送到管道末端的操作(例如Get-Process notepad | Stop-Process)。这仅适用于Hide-WUUpdate接受对象作为输入的情况。它可能不适用,只允许与其标志一起使用-Title,而标志可能不支持多个条目。

相关内容