Wednesday, January 29, 2014

Powershell script to find in which deployment package a software update is

Troubleshooting security updates stuck at “downloading 0%”  in Software Center, I’m often wondering in which “Deployment Packages” xxxxxxxx update is located ? 
I spent some times opening each deployment packages and see if the update is there… That bored me and decided to write a powershell script…
Replace ServerName and SiteCode by yours.


$ServerName = "Server FQDN"
$SiteCode = "XXX"
$ArticleID = Read-Host "Enter Article ID"

$AllPkgUpdate = Get-WmiObject –NameSpace Root\SMS\Site_$SiteCode –Class SMS_SoftwareUpdatesPackage -ComputerName $ServerName | Select PackageID,Name
$update = Get-WmiObject –NameSpace Root\SMS\Site_$SiteCode –Class SMS_SoftwareUpdate -ComputerName $ServerName –Filter ArticleID="'$ArticleID'" | Select CI_UniqueID,LocalizedDisplayName,CI_ID

Foreach ($objectUpdate in $update)
{
$Question = Read-Host "Do you want to look for " $objectUpdate.LocalizedDisplayName " ? (Y/N)"
    If ($Question -eq "Y")
    {
    $UniqueID = $objectUpdate.CI_UniqueID
    $ContentID= Get-WmiObject –NameSpace Root\SMS\Site_$SiteCode –Class SMS_CIToContent -ComputerName $ServerName –Filter CI_UniqueID="'$UniqueID'" | Select ContentID
    $TargetUpdate = $ContentID | ForEach-Object {$_.ContentID}
    Write-Host "Please wait..."
        Foreach ($PkgItem in $AllPkgUpdate)
        {
        $PkgID = $PkgItem.PackageID
        $ArrContentID = Get-WmiObject –NameSpace Root\SMS\Site_$SiteCode –Class SMS_PackageToContent -ComputerName $ServerName –Filter PackageID="'$PkgID'" | Select ContentID
        $TargetContentID = $ArrContentID | ForEach-Object {$_.ContentID}
            If ($TargetUpdate -isnot [system.array])
            {$Test = $TargetContentID -contains $TargetUpdate}
            Else{$Test = -not ($TargetUpdate | where {$TargetContentID -notcontains $_}).count}
            If ($Test -eq $true)
            {$PkgName = $PkgItem.Name
            Write-Host $objectUpdate.LocalizedDisplayName " found in "$PkgName" - "$PkgID
            }
        }
    }

}