PowerShell Script to Cleanse WMI and Package Library

This PowerShell script cleanses the Windows Management Instrumentation (WMI) repository of packages that are present in the Package Library, but not present in the WMI repository. It also deletes any .INI files of these packages from the Package Library.

The Script

# Get the list of packages in WMI
$WMIPkgList = Get-WMIObject -NameSpace Root\SCCMDP -Computername $env:computername -Class SMS_PackagesInContLib | Select-Object -ExpandProperty PackageID | Sort-Object

# Open the registry key for the Content Library path
$Reg = [Microsoft.Win32.RegistryKey]::OpenRemoteBaseKey('LocalMachine', $env:computername)
$RegKey = $Reg.OpenSubKey("SOFTWARE\\Microsoft\\SMS\\DP")

# Get the value of the Content Library path from the registry
$ContentLib = $RegKey.GetValue("ContentLibraryPath")

# Construct the full path to the Package Library
$PkgLibPath = ($ContentLib) + "\PkgLib"

# Extract the drive letter from the package library path
$drive = $PkgLibPath.SubString(0, 1)

# Replace the drive letter with the current machine's UNC path
$PkgLibPath = $PkgLibPath.Replace(($drive + ":\"), ("\\" + $env:computername + "\" + $drive + "$\"))

# Get a list of packages in the Package Library
$PkgLibList = (Get-ChildItem $PkgLibPath | Select-Object -ExpandProperty Name | Sort-Object)

# Remove any .INI extensions from the package names
$PkgLibList = ($PKgLibList | ForEach-Object { $_.replace(".INI", "") })

# Compare the WMI list with the Package Library list to find discrepancies
$PksinWMIButNotContentLib = Compare-Object -ReferenceObject $WMIPkgList -DifferenceObject $PKgLibList -PassThru | Where-Object { $_.SideIndicator -eq "<=" }
$PksinContentLibButNotWMI = Compare-Object -ReferenceObject $WMIPkgList -DifferenceObject $PKgLibList -PassThru | Where-Object { $_.SideIndicator -eq "=>" }

# Cleanse WMI
if ($PksinWMIButNotContentLib) {
    Write-Host "Delete these items from WMI: " -NoNewline
    Write-host $PksinWMIButNotContentLib -ForegroundColor red
    $packageID = $PksinWMIButNotContentLib
    foreach ($packageID in $PksinWMIButNotContentLib) {
        Get-WMIObject -ComputerName $env:computername -Namespace "root\sccmdp" -Query ("Select * from SMS_PackagesInContLib where PackageID = '" + $PackageID + "'") | Remove-WmiObject -Verbose
    }    
}
else {
    Write-host "WMI: No inconsistancies found :)" -ForegroundColor Green
}

# Cleanse Packagelib
if ($PksinContentLibButNotWMI) {
    Write-Host "Delete .INI files of these packages from the PkgLib folder: " -NoNewline
    Write-host $PksinContentLibButNotWMI -ForegroundColor red
    foreach ($packageID in $PksinContentLibButNotWMI) {
        if (test-path "\\$($env:COMPUTERNAME)\sccmcontentlib`$\PkgLib\$packageID") {
            remove-item -path "\\$($env:COMPUTERNAME)\sccmcontentlib\`$PkgLib\$packageID" -force -verbose
        }
    }
}
else {
    Write-host "PkgLib: No inconsistancies found :)" -ForegroundColor Green
}

How It Works

The script works by first getting a list of packages in the WMI repository. It then gets the value of the Content Library path from the registry and uses this to construct the full path to the Package Library. The script then compares the WMI list with the Package Library list to find discrepancies, which are packages that are present in the Package Library but not in the WMI repository.

Key Code Snippets

# Get-WMIObject -NameSpace Root\SCCMDP -Computername $env:computername -Class SMS_PackagesInContLib
# OpenRemoteBaseKey 'LocalMachine', $env:computername
# Compare-Object -ReferenceObject $WMIPkgList -DifferenceObject $PKgLibList -PassThru
# Get-ChildItem $PkgLibPath | Select-Object -ExpandProperty Name

Usage Examples

This script should be run on a machine with administrative privileges to ensure that the necessary registry keys and WMI repositories are accessed correctly. The script can be used to identify and clean up packages in the Package Library that are no longer present in the WMI repository.

Conclusion

The PowerShell script provided cleanses the WMI repository of packages that are present in the Package Library but not present in the WMI repository, as well as deletes any .INI files of these packages from the Package Library. The script is a useful tool for maintaining the integrity of the Package Library and ensuring that it remains synchronized with the WMI repository.