答案1
我现在用 Powershell 脚本来做这件事。它还不完美,但可以工作。
Set-StrictMode -Version Latest
$ErrorActionPreference = 'Inquire'
$updateSession = New-Object -ComObject 'Microsoft.Update.Session'
$updateSession.ClientApplicationID = 'Defender Updates installer'
$updateSearcher = $updateSession.CreateUpdateSearcher()
'Searching for updates...'
$searchResults = $updateSearcher.Search("IsInstalled=0 and Type='Software' and IsHidden=0")
if ( $searchResults.Updates.Count -eq 0 )
{
'There are no applicable updates.'
return
}
$updatesToDownload = New-Object -ComObject 'Microsoft.Update.UpdateColl'
for ( $i=0; $i -le $searchResults.Updates.Count - 1; $i++ )
{
$update = $searchResults.Updates.Item($i)
if ( $update.Title -like '*defender*' )
{
$update.AcceptEula()
$updatesToDownload.Add( $update )
}
}
if ( $updatesToDownload.Count -eq 0 )
{
'All applicable updates were skipped.'
return
}
'Downloading updates...'
$downloader = $updateSession.CreateUpdateDownloader()
$downloader.Updates = $updatesToDownload
$downloader.Download()
$updatesToInstall = New-Object -ComObject 'Microsoft.Update.UpdateColl'
'Successfully downloaded updates:'
for ( $i=0; $i -le $searchResults.Updates.Count - 1; $i++ )
{
$update = $searchResults.Updates.Item($i)
if ( $update.IsDownloaded -eq $true )
{
$updatesToInstall.Add( $update )
}
}
if ( $updatesToInstall.Count -eq 0 )
{
'No updates were successfully downloaded.'
return
}
'Installing updates...'
$installer = $updateSession.CreateUpdateInstaller()
$installer.Updates = $updatesToInstall
$installationResult = $installer.Install()
# Output results of install
'Installation Result: ' + $installationResult.ResultCode
'Reboot Required: ' + $installationResult.RebootRequired
'Listing of updates installed and individual installation results:'
for ( $i=0; $i -le $updatesToInstall.Count - 1; $i++ )
{
'> ' + $updatesToInstall.Item($i).Title + ': ' + $installationResult.GetUpdateResult($i).ResultCode
}