Get all the windows update and put in to the ciminstance

NAUFAL SAIDHUS SYUHUR 0 Reputation points
2024-04-25T03:09:13.66+00:00

I have to get all windows update to put inside ciminstance, i already try using this script

$wmiCustomNamespace = "Get_Update_SCDA"
$wmiCustomClass = "W32_Updates"
$classExists = Get-CimClass -Namespace root\$wmiCustomNamespace -ClassName $wmiCustomClass -ErrorAction SilentlyContinue
if (-not $classExists) {
    Write-CMLogEntry -Value "$wmiCustomClass WMI Class does not exist in the ROOT\$wmiCustomNamespace namespace. Creating..." -Severity 1
    $newClass = New-Object System.Management.ManagementClass ("ROOT\$($wmiCustomNamespace)", [String]::Empty, $null); 
    $newClass["__CLASS"] = $wmiCustomClass; 
    $newClass.Qualifiers.Add("Static", $true)
    $newClass.Properties.Add("Hostname", [System.Management.CimType]::String, $false)
    $newClass.Properties["Hostname"].Qualifiers.Add("Key", $true)
    $newClass.Properties.Add("Install_Date", [System.Management.CimType]::String, $false)
    $newClass.Properties.Add("KB", [System.Management.CimType]::String, $false)
    $newClass.Properties.Add("Title", [System.Management.CimType]::String, $false)
    $newClass.Properties.Add("Description", [System.Management.CimType]::String, $false)
    $newClass.Properties.Add("Operation", [System.Management.CimType]::String, $false)
    $newClass.Properties.Add("Status", [System.Management.CimType]::String, $false)
    $newClass.Put() | Out-Null
}

# RegEx to pull the KB number from the Title field.
$regex = "(KB\d{6,7})"
$hostname = $env:COMPUTERNAME
$Session = New-Object -ComObject "Microsoft.Update.Session"
$Searcher = $Session.CreateUpdateSearcher()
$FormatEnumerationLimit=-1
$historyCount = $Searcher.GetTotalHistoryCount()

$Updates = $Searcher.QueryHistory(0, $historyCount)  | 
    Where-Object { $_.Title -match $regex } | 
    Select-Object  @{name="HostName"; expression = { $hostname }},
                    @{name="Install_Date"; expression = { $_.Date }},
                    @{name="KB"; expression = { $Matches[0] }},
                    Title, 
                    Description, 
                    @{name="Operation"; expression={switch($_.operation){
                                                        1 {"Installation"}
                                                        2 {"Uninstallation"}
                                                        3 {"Other"}
                                                    }
                                                }
                    }, 
                    @{name="Status"; expression={switch($_.resultcode){
                                                    1 {"In Progress"}
                                                    2 {"Succeeded"}
                                                     3 {"Succeeded With Errors"}
                                                    4 {"Failed"}
                                                    5 {"Aborted"}
                                                }
                                            }
                    }, 
                    SupportUrl

foreach ($Update in $Updates) {
    # Create new instance in WMI
    $newRec = New-CimInstance -Namespace root\Get_Update_SCDA -ClassName W32_Updates -Property @{
        Hostname = $Update.HostName
        Install_Date = [System.Management.ManagementDateTimeConverter]::ToDmtfDateTime($Update.Install_Date)
        KB = $Update.KB
        Title = $Update.Title
        Description = $Update.Description
        Operation = $Update.Operation
        Status = $Update.Status
    }
    # Save to WMI
    $newRec | Set-CimInstance
}


but it's only get 1 windows update.

User's image

PowerShell
PowerShell
A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
2,081 questions
0 comments No comments
{count} votes