The powershell script discussed in this article will help you reset and purge the policies on a SCCM client remotely using PowerShell.
SCCM(System Centre Configuration Manager) has variety of WMI classes and one of them is SMS_Client. This class helps you querying the machine policy remotely as well as reset and purge the existing policies.
You can check http://msdn.microsoft.com/en-us/library/cc146352.aspx for various methods available in this class. The script I am going to discuss below relies on ResetPolicy method to reset and purge the policies.
The script takes two arguments. 1) ComputerName 2) Purge. You can pass one or more computers(cama separated) to the ComputerName parameter. When this parameter is not specified it executes against local computer. The Purge argument is a switch type parameter. When it is used, the policy data will be purged. When not specified the reset policy will get executed with argument 1 which means full download of existing policy.
[cmdletbinding()] param( [string[]]$ComputerName = $env:ComputerName, [switch]$Purge = 0 ) if($Purge) { $Gflag = 1 } else { $Gflag = 0 } foreach($Computer in $ComputerName) { $client = Get-WMIObject -Class SMS_Client -Namespace root\ccm -ComputerName $Computer $returnval = $Client.ResetPolicy($Gflag) if($returnvalue) { Write-Warning "Error occurred while resetting/purging the policy on $Computer" } else { Write-Host "Purge/Reset successful on $Computer" } }
I learned this from one of the contributor in “Hyderabad Powershell User Group” FB group. I am not an expert in SCCM but given that use case of this seems very high, I thought of converting it to a post.
Since I haven’t tried this script practically, I STRONGLY request you to try in a lab environment to see if it is giving desired results. Use at your own risk.
Comments on this entry are closed.
The namespace you are using in your script above is incorrect. Probably a typo, but should -Namespace root\ccm
Or use a one-liner:
Invoke-WMIMethod -Namespace root\ccm -Class SMS_Client -Name ResetPolicy -ArgumentList “1” -Computername “NAME”
Yes that works for adhoc requirements. If you want something that works for multiple computers with error handling then the script will help.
This doesn’t work in current version. Instead, the $client line should read:
$client = Get-WMIObject -Class SMS_Client -Namespace root\ccm -ComputerName $Computer -List
Hi Jon,
what is the problem you faced with the code in the post? The only difference I noticed is usage of -List parameter at the end of Get-WMIObject cmdlet which doesn’t add any value. Please help us understand so we can update the post accordingly.
To make it working with todays Powershell it must be written like this:
[cmdletbinding()]
param(
[string[]]$ComputerName = $env:ComputerName,
)
# ATTENTION HERE THE -ArgumentList @(1) is the flag 1 for purging. 0 is for full reset policy only. # Purging does more but may introduce other issues. See documentation on
# https://docs.microsoft.com/en-
#us/mem/configmgr/develop/reference/core/clients/client-classes/resetpolicy-method-in-class-
#sms_client
foreach($Computer in $ComputerName) {
$client = Invoke-WmiMethod -ComputerName $Computer -Namespace root\ccm -Class sms_client -Name ResetPolicy -ArgumentList @(1)
$returnvalue = $client.ReturnValue
if($returnvalue) {
Write-Warning “Error occurred while resetting/purging the policy on $Computer”
} else {
Write-Host “Purge/Reset successful on $Computer”
}
}