简体   繁体   English

如何在powershell中跳过可选的Windows更新

[英]How to skip optional windows updates in powershell

I found a script to download windows updates that I've been tweaking to fit my needs.我找到了一个下载 Windows 更新的脚本,我一直在调整它以满足我的需要。 It seems to work fine except I can't figure out how to remove the optional updates before downloading.它似乎工作正常,但我不知道如何在下载之前删除可选更新。 I've found that the "Critical", "Important", and "Moderate" updates will have a MsrcSeverity value of one of those 3 words, where optional will be blank.我发现“关键”、“重要”和“中等”更新的 MsrcSeverity 值为这三个词之一,其中 optional 将为空。 How do I remove the updates with no msrcseverity value from the list before downloading??下载前如何从列表中删除没有 msrcseverity 值的更新?

Here's the whole code...这是整个代码...

$global:scriptpath = $MyInvocation.MyCommand.Path
$global:dir = Split-Path $scriptpath
$global:logfile = "$dir\updatelog.txt"

write-host " Searching for updates..."
$session = New-Object -ComObject Microsoft.Update.Session
$searcher = $session.CreateUpdateSearcher()
$result = $searcher.Search("IsInstalled=0 and Type='Software' and IsHidden=0")

if ($result.Updates.Count -eq 0) {
     Write-Host "No updates to install"
}

else {

    $result.Updates | Select Title
    $result.Title >> $logfile

}

$downloads = New-Object -ComObject Microsoft.Update.UpdateColl

foreach ($update in $result){

    $downloads.Add($update)

}

$count = $result.Updates.Count

write-host ""
write-host "There are $($count) updates available."
write-host ""

read-host "Press Enter to download\install updates"

$downloader = $session.CreateUpdateDownLoader()
$downloader.Updates = $downloads
$downloader.Download()

$installs = New-Object -ComObject Microsoft.Update.UpdateColl
foreach ($update in $result.Updates){
     if ($update.IsDownloaded){
           $installs.Add($update)
     }
}

$installer = $session.CreateUpdateInstaller()
$installer.Updates = $installs
$installresult = $installer.Install()
$installresult

I have the "read-host" in there right now to stop it from downloading until I get this figured out.我现在有“读取主机”可以阻止它下载,直到我弄清楚了。 I've tried putting an extra pipe in $result.updates | Select Title | where {$result.Updates.MsrcSeverity -ne $null}我试过在$result.updates | Select Title | where {$result.Updates.MsrcSeverity -ne $null}放置一个额外的管道$result.updates | Select Title | where {$result.Updates.MsrcSeverity -ne $null} $result.updates | Select Title | where {$result.Updates.MsrcSeverity -ne $null} , I've also tried that with just $result.MsrcSeverity and no go. $result.updates | Select Title | where {$result.Updates.MsrcSeverity -ne $null} ,我也试过只用$result.MsrcSeverity并且没有去。 I've tried the "where" pipe in a couple different places.我已经在几个不同的地方尝试过“where”管道。 I've also tried making an If statement in a couple places that says if the MsrcSeverity doesn't equal null then add it to the list.我还尝试在几个地方做一个 If 语句,说明如果 MsrcSeverity 不等于 null,则将其添加到列表中。 I've also tried adding onto the $searcher.Search( line with an and MsrcSeverity = 'Important'") just to test and that didn't do anything.我还尝试添加到$searcher.Search( line with an and MsrcSeverity = 'Important'")只是为了测试,但没有做任何事情。

So far it still lists all the updates whether or not there's something in the MsrcSeverity column.到目前为止,无论 MsrcSeverity 列中是否存在某些内容,它仍会列出所有更新。 Am I looking in the wrong place?我找错地方了吗? It's the only thing I can see that tells the difference between an Important update and an Optional.这是我能看到的唯一可以区分重要更新和可选更新的东西。

Thanks.谢谢。

Search criterias are documented at IUpdateSearcher::Search method搜索条件记录在IUpdateSearcher::Search 方法中

The BrowseOnly=0 unfortunately doesn't exclude Optional updates as seen in Windows Update program.不幸的是, BrowseOnly=0并没有排除在 Windows 更新程序中看到的可选更新。 But AutoSelectOnWebSites=1 does.AutoSelectOnWebSites=1确实如此。

  • "BrowseOnly=1" finds updates that are considered optional. "BrowseOnly=1" 查找被认为是可选的更新。

  • "BrowseOnly=0" finds updates that are not considered optional. "BrowseOnly=0" 查找不被视为可选的更新。

  • "AutoSelectOnWebSites=1" finds updates that are flagged to be automatically selected by Windows Update. “AutoSelectOnWebSites=1”查找标记为由 Windows 更新自动选择的更新。

  • "AutoSelectOnWebSites=0" finds updates that are not flagged for Automatic Updates. "AutoSelectOnWebSites=0" 查找未标记为自动更新的更新。

$session1 = New-Object -ComObject Microsoft.Update.Session -ErrorAction silentlycontinue
$searcher = $session1.CreateUpdateSearcher()
#Do not search for optional updates and exclude hidden
$result = $searcher.Search("IsInstalled=0 AND AutoSelectOnWebSites=1 AND IsHidden=0")

Thanks for all the help, everyone.谢谢各位的帮助。 I got so many helpful suggestions I didn't know where to begin...我得到了很多有用的建议,我不知道从哪里开始......

I got it figured out, thanks.我搞清楚了,谢谢。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM