Pete Hinchley: Rerun an SCCM Advertisement on All Computers in a Collection

Here is a PowerShell code snippet to rerun an advertisement on all computers in a collection. It is dependent on access to a DLL from the Client Centre for Configuration Manager.

The code should be run from an elevated prompt on the primary site server.

$SiteServer   = "BORIS"
$SiteCode     = "LAB"
$CollectionId = "LAB02000"
$AdvertId     = "LAB20000"
$PackageId    = "LAB00010"
$ProgramName  = "Install"

Add-Type -Path "C:\Program Files\SCCM Tools\SCCM Client Center\smsclictr.automation.dll"

gwmi -ComputerName $SiteServer `
  -Namespace "ROOT\SMS\site_$SiteCode" `
  -Query "SELECT * FROM SMS_FullCollectionMembership WHERE CollectionID='$($id)' ORDER BY Name" |
  select Name | %{
    $computer = $_.Name
    if (Test-Connection $computer) {
      (New-Object -TypeName smsclictr.automation.SMSClient($computer)).SoftwareDistribution.RerunAdv($AdvertId, $PackageId, $ProgramName)
    }
  }