MSEndpointMgr

Get Primary User and Device relationship assignment source with PowerShell

Primary User and Device relationships in ConfigMgr are something that’s not very easy to make out in the ConfigMgr console for the administrator, at least not for a collection of Devices or Users. For instance I’ve been longing for the ability to show the Primary User or Primary Device as a column in the Device Collections and User Collections nodes in the console ever since ConfigMgr 2012 was released. Last week I was at a customer site and was tasked with determining what relationships between a Device and a Primary User was assigned by an administrator. In order to determine that, I started looking at the different WMI Server classes and found the SMS_UserMachineRelationship. This class holds all the relationships between a Device and the Primary User that’s been assigned by any assignment source. What do I mean by assignment source you may ask?
There’s a property called Sources for every instance in the SMS_UserMachineRelationship class. This property is an array of assignment sources. It holds an array of integers where each integer represents a type of assignment source. Here’s a detailed list of the assignment source types:

1Software CatalogThe end user enabled the relationship by selecting the option in the AppCatalog Web page.
2AdministratorAn administrator created the relationship manually in the UI.
3UserUnused/deprecated.
4Usage AgentThe threshold of activity triggered a relationship to be created.
5Device ManagementThe user/device were tied together during enrollment.
6OSDThe user/device were tied together as part of OSD imaging.
7Fast InstallThe user/device were tied together temporarily to enable an on-demand install from the catalog if no UDA relationship installed before the Install was triggered.
8Exchange Server connectorThe device was provisioned through EAS.

What this script does is that it evaluates all of the instances in the SMS_UserMachineRelationship class by the specified assignment source. It then outputs a custom PowerShell object showing what relationships have been created by the specified assignment source. As an extra feature, I’ve added a switch to the script that will allow you to remove the instances matching the assignment source you’ve specified. In environments where there’s a lot of devices, I’ve added the ShowProgress switch to the script that will give you an overview of the current progress of the script, so that you can determine for how long it may take to execute.

Download the script

I’ve made the script available for you to download it from the TechNet Gallery.

Using the script

In the case that you need to know what relationship have been assigned by the Usage Agent (created by the ConfigMgr client when enabled in Client Settings with the specified threshold settings), use the script like below:
1. Open an elevated PowerShell console and browse to where you’ve downloaded the script, e.g. C:\Scripts.
2. Run the following command:

.\Get-CMPrimaryUserAssignmentSource.ps1 -SiteServer CAS01 -AssignedBy UsageAgent -ShowProgress -Verbose

135_1
3. Once the script has successfully processed all of the targeted relationships, it will output each of them to the console.
If you wish to remove these relationships, simply just add the -Remove parameter and it will remove those instances. You can add the -WhatIf switch if you want to see what actions the script will perform before you remove the instances.
I hope this helps!

Nickolaj Andersen

Chief Technical Architect and Enterprise Mobility MVP since 2016. Nickolaj has been in the IT industry for the past 10 years specializing in Enterprise Mobility and Security, Windows devices and deployments including automation. Awarded as PowerShell Hero in 2015 by the community for his script and tools contributions. Creator of ConfigMgr Prerequisites Tool, ConfigMgr OSD FrontEnd, ConfigMgr WebService to name a few. Frequent speaker at conferences such as Microsoft Ignite, NIC Conference and IT/Dev Connections including nordic user groups.

16 comments

  • Hi
    First of all, thank you for publishing this script and article, really useful.
    Though, it seems like all users are removed from a computer if there are multiple users added from different sources, even though a source is specified on the command line.
    When testing in my lab, I modified the code on the line that removes the user from WMI like this: “….-Filter “ResourceID like ‘$($PrimaryUser.ResourceID)’ And Sources like ‘$($CurrentAssignment)'”…..”, and it worked. It would be good if you could confirm.
    Would be useful if someone needs to clear all added from a particular source, but keep all others.
    Regards
    Kenneth

  • Great script Nickolaj. May i know whether there’s an option to do this on specific collection i.e. user or device collection rather than whole site?
    Thanks.

  • I’m a PowerShell noob. Is there an easy way to also output the results to a .CSV file, without (or with) modifying the script?

    • Hi Kevin,
      All you need to do is pipeline the command through and use the Export-CSV cmdlet. Example;
      .\Get-CMPrimaryUserAssignmentSource.ps1 -SiteServer YOURSITESERVER -AssignedBy UsageAgent | Export-CSV -Path PATHTOFILE\FILENAME.CSV -NoTypeInformation
      Maurice

    • Hi Mike,
      There are several other ways of working with ConfigMgr and crunch the data it holds than just using the built-in tools. This post was meant to provide an alternative to the reports available in the product.
      Regards,
      Nickolaj

  • One more time, very well done, Nickolaj.
    Good job! Thank you for this script.
    Eden Oliveira

  • Or is there a way to get the PrimaryUserName in a column in the SCCM console 🙂
    Thanks anyway for this script!

    • Hi Mark,
      Not that I know of, but why not submit that as feedback using the User Voice site. Perhaps they can extend the console to include that feature in a future release!
      Regards,
      Nickolaj

      • Hi again,
        I am using this report to find users with no prim. device.
        “Device Affinity Associations Per Collection”

      • Hello,
        Thanks for posting this. I’m wondering if you can elaborate? I too would like this report. Are you modifying the .ps1?

      • It is not related to the script. It is a build in report in SCCM (Current Branch)

  • Hi,
    Is there a way to get a list of user that do NOT have any Primary Device assigned?

Sponsors

Categories

MSEndpointMgr.com use cookies to ensure that we give you the best experience on our website.