Forums on Intune, SCCM, and Windows 11

Welcome to the forums. Register a free account today to become a member! Once signed in, you'll be able to participate on this site by adding your topics and posts, as well as connect with other members through your own private inbox!

SOLVED Updating SCCM collection Using PS script

  • Thread starter Thread starter Gokul
  • Start date Start date
  • Replies Replies 0
  • Views Views 2K

Gokul

Well-Known Member
Staff member
Messages
321
Solutions
4
Reaction score
58
Points
28
GOOD DAY ALL

We have faced many issue while preparing customer reports , Main issue was collections were not getting updated and actual count was not showing in reports . So we came up with PS to update the collection . So we could run the script before taking the reports. Here i am showing Script and how it will work . So that everyone can make the use of it

1.Make one folder under any drive and copy the script and create one notepad Collections_list.txt - mention the collection names ,
SS 1.JPG
2. I have mentioned some areas in the script to modify and not to modify .

#Script created by Gokul
# Press 'F5' to run this script. Running this script will load the ConfigurationManager
# module for Windows PowerShell and will connect to the site.
#
# This script was auto-generated at '3/17/2020 1:41:05 PM'.

# Uncomment the line below if running in an environment where script signing is
# required.
#Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process

# Site configuration
$SiteCode = "S01" # Mention Site code
$ProviderMachineName = "XYZ.com" # Mention SMS Provider machine name

# Customizations
$initParams = @{}
#$initParams.Add("Verbose", $true) # Uncomment this line to enable verbose logging
#$initParams.Add("ErrorAction", "Stop") # Uncomment this line to stop the script on any errors

# Do not change anything below this line

# Import the ConfigurationManager.psd1 module
if((Get-Module ConfigurationManager) -eq $null) {
Import-Module "$($ENV:SMS_ADMIN_UI_PATH)\..\ConfigurationManager.psd1" @initParams
}

# Connect to the site's drive if it is not already present
if((Get-PSDrive -Name $SiteCode -PSProvider CMSite -ErrorAction SilentlyContinue) -eq $null) {
New-PSDrive -Name $SiteCode -PSProvider CMSite -Root $ProviderMachineName @initParams
}

# Set the current location to be the site code.
Set-Location "$($SiteCode):\" @initParams
$ScriptDir = Split-Path $Script:MyInvocation.Mycommand.path
$date = Get-Date -Format "dd-MM-yyyy hh:mm:ss"
$log = "$ScriptDir\Status.log"
$ImportColl = Get-Content $ScriptDir\"Collections_list.txt"
$DeviceCollections = Get-CMDeviceCollection -Name $ImportColl | Select Name , CollectionID
"--------------------- Script executed on $date (dd-MM-yyyy hh:mm:ss) ---------------------" + "`r`n" | Out-File $log -append
foreach($Devicecollection in $DeviceCollections)
{
try
{
"$date [Info]`t Getting Collection Name '$($Devicecollection.Name)'" |Out-File -Append $log
Invoke-WmiMethod ` -path "root\sms\site_P01:SMS_Collection.CollectionID='$($Devicecollection.CollectionID)'" ` -Name RequestRefresh -ComputerName $ProviderMachineName | Out-Null
"$date [Info]`t Updated the Collection Membership on Collection '$($Devicecollection.Name)'" | Out-File -Append $log
}
catch
{
"$date [ERROR]`t Unable to update membership on collecion '$($Devicecollection.Name)" | Out-File -Append $log
}
}
"--------------------- Script execution Completed on $date (dd-MM-yyyy hh:mm:ss) ---------------------" + "`r`n" | Out-File $log -append


OUTPUT


SS 1.JPG
 
Back
Top