Last active
August 29, 2015 14:12
-
-
Save mortenya/62c95b3ec975d4331db2 to your computer and use it in GitHub Desktop.
Function to grab Definition updates for Windows Defender. Felt a little messy how I used the foreach loop, but it works.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Function WSUSUpdate { | |
<# | |
Slight modification of https://gist.github.com/jacobludriks/9ca9ce61de251a5476f1 | |
#> | |
$Criteria = "IsInstalled=0 and Type='Software'" | |
$Searcher = New-Object -ComObject Microsoft.Update.Searcher | |
try { | |
$SearchResult = $Searcher.Search($Criteria).Updates | |
if ($SearchResult.Count -eq 0) { | |
Write-Output "There are no applicable updates." | |
exit | |
} else { | |
$Session = New-Object -ComObject Microsoft.Update.Session | |
$Downloader = $Session.CreateUpdateDownloader() | |
$Installer = New-Object -ComObject Microsoft.Update.Installer | |
foreach ($i in 0..$SearchResult.Count) { | |
try { | |
if (($SearchResult.Item($i).Title) -like 'Definition Update for Windows Defender - KB*') { | |
$Downloader.Updates = $SearchResult[$i] | |
$Downloader.Download() | |
$Installer.Updates = $SearchResult[$i] | |
$Result = $Installer.Install() | |
#$SearchResult[$i] | |
} | |
} | |
catch { } | |
} | |
} | |
} | |
catch { | |
Write-Output "There are no applicable updates." | |
} | |
} | |
WSUSUpdate | |
#If ($Result.rebootRequired) { Restart-Computer } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment