PowerShell + ConfigMgr Trick: Remove WMI Instances
There was a question on TechNet on how to Remove User Device Affinity via WMI?
The User Device Affinity Rule creates a relationship between a Device and User. One can alternatively use PowerShell with WMI to create one of these. For this trick, let's see how to remove an existing UDA using PowerShell and WMI in Configuration Manager 2012.
If you go to the question asked, Peter suggests using Remove type method to do the same but there are many ways to skin a cat using PowerShell. Removing a WMI/CIM instance should be pretty straightforward.
While working with ConfigMgr using WMI & PowerShell, we usually set the PSDefaultparameters (about_Parameters_Default_Values) at the very beginning like below:
$PSDefaultParameterValues =@{"get-cimInstance:computername"="DexSCCM";"get-ciminstance:namespace"="Root\SMS\site_DEX"} |
Now making CIM/WMI Class is easy as I don't have to specify the ConfigMgr Server hosting SMS Provider and the Namespace:
PS DEX:\> Get-CimInstance -ClassName SMS_UserMachineRelationship CreationTime : 10/28/2014 3:19:49 AM IsActive : True RelationshipResourceID : 25165824 ResourceClientType : ResourceID : 2097152001 ResourceName : DEXDC Sources : {2} Types : {1} UniqueUserName : DEX\DexterPOSH PSComputerName : DexSCCM |
The above cmdlet returns me the only UDA rule we have created in the environment. One might have to apply filters in order to get the desired UDA rule and then do the below:
PS DEX:\> Get-CimInstance -ClassName SMS_UserMachineRelationship | Remove-CimInstance -Verbose VERBOSE: Performing the operation "Remove-CimInstance" on target "SMS_UserMachineRelationship (RelationshipResourceID = 25165824)". VERBOSE: Perform operation 'Delete CimInstance' with following parameters, ''namespaceName' = root/sms/site_dex,'instance' = SMS_UserMachineRelationship (RelationshipResourceID = 25165824)'. VERBOSE: Operation 'Delete CimInstance' complete. PS DEX:\> |
This might look super easy and in order to verify it really worked take a look in the SMSProv.log on the ConfigMgr Server you connected to for any errors.
Similarly, this concept of using Remove-CimInstance can be applied to various other aspects too. We did use this earlier on removing the content from DPs in ConfigMgr and are documented at below link:
http://www.dexterposh.com/2014/02/use-powershell-to-remove-packages-from.html
One can get really creative with PowerShell and WMI/CIM cmdlets and the best thing about this approach is you don't have a dependency on a PS Module.