The other day I was tasked with checking a list of KB articles if they where deployed or not. Since we’ve just upgraded to Configuration Manager 2012 SP1, I thought that I would use PowerShell and the new cmdlets that you get on SP1. So I started exploring the new cmdlets and soon found that the script I wanted to generate, was not giving me the correct output when using the Get-CMSoftwareUpdate cmdlet. A fellow blogger called David O’brien (check out his blog here) suggested that I took another approach, that being using WMI instead.
So I started to rebuild the script I’d created to work with WMI instead, and was surprised how well everything worked.
Overview of the script
The script will first create an array of content provided from a text file. This text file contains all the artice ID’s that we want to check the status of (see picture below how it’s structured). It then connects to theSMS_SoftwareUpdate WMI class on a primary site. For each article ID in the text file, it will select a few properties and first check if the software update that corresponds with the article ID is found in the WMI class. If the software update is not detected, it will get added to a hash table and later written as output. Second the script will export the detected software updates to a CSV file, remove all the quotes in the CSV file and finally import it again. Once imported, a variable with all objects are being checked for certain conditions. If the property isDeployed is True it will write the output in green text. If it’s False, the output will become red.
The script
#Create an empty array $array = @() #Empty hash table $hash = @{} #Import data into the empty array $array = Get-Content -Path E:\scripts\updates.txt #Parse each object in the array, query for software updates in WMI and select "LocalizedDisplayName" and "isDeployed" properties $array | ForEach-Object { $CurrentUpdate = $_ $updates = Get-WmiObject -Class SMS_SoftwareUpdate -Namespace root\sms\site_p01 | Where-Object {$_.ArticleID -like "$CurrentUpdate"} | Select-Object LocalizedDisplayName,IsDeployed if ($updates -eq $null) { $hash.Add($CurrentUpdate,"Update not found") } #Export update to CSV and append the file $updates | Export-CSV E:\Scripts\updates.csv -NoTypeInformation -Append } #List of updates that are not deployed Write-Host "List of updates not found:" $hash Write-Host "" #Write the list of updates where "isDeployed" is "True" or "False" Write-Host "Deployment status of software updates:" Write-Host "" #Remove the quotes from the exported CSV file $file = "E:\Scripts\updates.csv" (Get-Content $file) -replace('"','') | Out-File $file -Force #Import the exported CSV with delimiter "," $Import = Import-CSV -Path E:\Scripts\updates.csv -Delimiter "," #Parse each line in the imported CSV and write output if "isDeployed" is either "True" or "False" $Import | ForEach-Object { if ($_.isDeployed -eq "True") { Write-Host -ForegroundColor Green $_.LocalizedDisplayName } elseif ($_.isDeployed -eq "False") { Write-Host -ForegroundColor Red $_.LocalizedDisplayName } } #Delete the exported CSV file Get-Item E:\Scripts\updates.csv | Remove-Item -Force
Add comment