Change device ownership – The journey through log files, WMI and PowerShell of ConfigMgr 2012

Last week I’ve got the question about how to quickly change the device owner of all mobile devices. This question was based on the fact that by default the device owner is set to Personal and that default value cannot be changed. The Configuration Manager console answer is easy, select the devices, right-click and select Change Ownership.  But now the real question(s), how does this work and is this scriptable? This blog post will contain my small journey through the SMSProv.log, WMI, PowerShell and TechNet for an answer.

PowerShell part I – Change Ownership

After a quick browse through the cmdlet referent on TechNet, I noticed that this should be very easy to achieve via PowerShell. There is a cmdlet available Set-CMDeviceOwnership that does exactly this. Even better it allows an array as input for devices and a simple Personal or Company as ownership. The best thing is that it’s possible to simply input an array with all devices and only the applicable mobile devices will be changed.

Set-CMDeviceOwnership -DeviceId $ResourceIDs -OwnershipType $DeviceOwner

As this is a cmdlet, I still don’t really know what it does. After holding the SMSprov.log next to my script, or my Configuration Manager console action, I saw that it simply calls the WMI method ChangeOwnership from the SMS_Collection class. Even though it’s a documented method, I would like to highlight two important things about this method, as it’s not clear from the documentation, and then directly provide an example.

  1. The order of the parameters should be DeviceOwner, ResourceIDs.
  2. The return value is the number of devices that were successfully reassigned ownership.
Invoke-WmiMethod -Namespace root/SMS/site_$($SiteCode) ` -Class SMS_Collection -Name ChangeOwnership ` -ArgumentList @($DeviceOwner,$ResourceIDs) -ComputerName $SiteServer

PowerShell part II – Get the resources

Even though I actually found my answer already, I would like to continue with a small piece about getting the devices to fill the array with resource ids as input for either method. This is the part why I’m not really a fan of the cmdlets, yet, because they’re not always that efficient. If I’m going to use the cmdlets for getting the resource ids of all the devices in a specific collection, I would need to use Get-CMDevice. It would make a nice and easy one-liner like the following.

$ResourceIDs = (Get-CMDevice -CollectionName "All Systems").ResourceId

Running this relatively simple command took a lot longer then I expected… So again, I ran the command, but this time with the SMSprov.log next to it. I just needed to know why it took so long. The reason showed up very soon. This cmdlet queries SMS_Collection, (in this case) SMS_CM_RES_COLL_SMS00001 and SMS_CombinedDeviceResources to gather all the information it needs to fill its properties. In this case it’s a lot more efficient to directly query WMI to gather the needed information. The following command might look a bit longer, but for just getting my resource ids, it’s huge factor faster, as it simply queries SMS_FullCollectionMembership for the members of a specific collection.

$ResourceIDs = (Get-WmiObject -ComputerName $SiteServer ` -Namespace root\SMS\Site_$SiteCode -Class SMS_FullCollectionMembership ` -Filter "CollectionID='SMS00001'").ResourceID

PowerShell part III – Complete examples

As I like to provide options, I created two example functions to query the All Systems collection and then change the device owner to Company for all applicable devices. One example is based on the cmdlets (of course, first import the ConfigurationManager module and set a location)  and one is based on WMI.

PowerShell cmdlets example

function Change-Ownership { param([string]$DeviceOwner) $ResourceIDs = (Get-CMDevice -CollectionName "All Systems").ResourceId Set-CMDeviceOwnership -DeviceId $ResourceIDs -OwnershipType $DeviceOwner } Change-Ownership "Company" #Company or Personal

PowerShell WMI example

function Change-Ownership { param([string]$SiteCode, [string]$SiteServer, [string]$DeviceOwner) $ResourceIDs = (Get-WmiObject -ComputerName $SiteServer ` -Namespace root\SMS\Site_$SiteCode -Class SMS_FullCollectionMembership` -Filter "DeviceOwner=2 AND CollectionID='SMS00001'").ResourceID Invoke-WmiMethod -Namespace root/SMS/site_$($SiteCode) ` -Class SMS_Collection -Name ChangeOwnership ` -ArgumentList @($DeviceOwner,$ResourceIDs) -ComputerName $SiteServer } Change-Ownership "PCP" "CLDSRV02" "1" #1=Company 2=Personal

Share

6 thoughts on “Change device ownership – The journey through log files, WMI and PowerShell of ConfigMgr 2012

  1. Thanks, Is there any other way to change the device ownership value in Intune?
    Have you heard about any plans to add powershell to Intune?

Leave a Comment