简体   繁体   中英

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. 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. How do I remove the updates with no msrcseverity value from the list before downloading??

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} , I've also tried that with just $result.MsrcSeverity and no go. I've tried the "where" pipe in a couple different places. 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. I've also tried adding onto the $searcher.Search( line with an and MsrcSeverity = 'Important'") just to test and that didn't do anything.

So far it still lists all the updates whether or not there's something in the MsrcSeverity column. 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

The BrowseOnly=0 unfortunately doesn't exclude Optional updates as seen in Windows Update program. But AutoSelectOnWebSites=1 does.

  • "BrowseOnly=1" finds updates that are considered optional.

  • "BrowseOnly=0" finds updates that are not considered optional.

  • "AutoSelectOnWebSites=1" finds updates that are flagged to be automatically selected by Windows Update.

  • "AutoSelectOnWebSites=0" finds updates that are not flagged for Automatic Updates.

$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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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