Powershell Script to refresh SCCM collections based on AD group names

I had a requirement to refresh collection members whenever the AD group is modified.

So the below script will refresh SCCM deployment collections which begins with ADG or AD from Active Directory. which was modified in the last hour.


$SCCMPrimaryserver = 'Server101'

$siteCode = 'ABC'

$currentTime = Get-Date

$oneHourAgo = $currentTime.AddHours(-1)

# Get all AD groups that were modified in the last hour

$modifiedGroups = Get-ADGroup -Filter {Modified -ge $oneHourAgo }

# Get the name of the  modified group which Begins with ADG or AD

$latestModifiedGroup = $modifiedGroups | where-object {$_.name -like 'ADG_*' -or $_.name -like 'AD_*'} | Select-Object Name

Foreach ($collection in $latestModifiedGroup){

Write-Host "Refreshing $collection"

$RefreshingCol= Get-WmiObject -Namespace "root\sms\site_$($siteCode)" -ComputerName $SCCMPrimaryserver -Class "SMS_Collection" -Filter "Name='$collection'"

# Refresh the collection

$RefreshingCol.RequestRefresh()

}


No comments:

Post a Comment