RU | EN | DE Kurzversion – Force-WSUSCheckIn.ps1:

try {
    $updateSession = New-Object -ComObject Microsoft.Update.Session
    $null = $updateSession.CreateUpdateSearcher().Search("( IsInstalled = 0 and IsHidden = 0 )").Updates
    exit 0
}
catch {
    exit 1
}

Vollversion – Force-WSUSCheckIn.ps1:

[CmdletBinding()]
param()
 
$ErrorActionPreference = 'Stop'
 
$LogDir = 'C:\ProgramData\WSUS'
$LogFile = Join-Path $LogDir 'Force-WSUSCheckIn.log'
 
if (-not (Test-Path $LogDir)) {
    New-Item -Path $LogDir -ItemType Directory -Force | Out-Null
}
 
function Write-Log {
    param(
        [string]$Message,
        [string]$Level = 'INFO'
    )
 
    $line = '{0} [{1}] {2}' -f (Get-Date -Format 'yyyy-MM-dd HH:mm:ss'), $Level, $Message
    Add-Content -Path $LogFile -Value $line
    Write-Output $line
}
 
try {
    Write-Log '--- Start WSUS trigger ---'
 
    try {
        $wua = Get-Service -Name wuauserv -ErrorAction Stop
        if ($wua.Status -ne 'Running') {
            Write-Log 'Starting service wuauserv'
            Start-Service -Name wuauserv -ErrorAction Stop
            Start-Sleep -Seconds 3
        }
        else {
            Write-Log 'Service wuauserv already running'
        }
    }
    catch {
        Write-Log "Could not verify/start wuauserv: $($_.Exception.Message)" 'WARN'
    }
 
    Write-Log 'Creating Microsoft.Update.Session COM object'
    $updateSession = New-Object -ComObject Microsoft.Update.Session
 
    Write-Log 'Running update search'
    $searchResult = $updateSession.CreateUpdateSearcher().Search("( IsInstalled = 0 and IsHidden = 0 )")
 
    if ($null -ne $searchResult -and $null -ne $searchResult.Updates) {
        Write-Log ("Search completed successfully. Updates found: {0}" -f $searchResult.Updates.Count)
    }
    else {
        Write-Log 'Search completed, but result object is empty' 'WARN'
    }
 
    Write-Log '--- Finish WSUS trigger OK ---'
    exit 0
}
catch {
    Write-Log ("ERROR: {0}" -f $_.Exception.Message) 'ERROR'
    Write-Log '--- Finish WSUS trigger FAILED ---' 'ERROR'
    exit 1
}