Payton Flint's Tech Blog
Menu
  • Home
  • Blog
  • Categories
  • Resources
  • About
  • Contact
Menu

PowerShell – Get Affiliated Devices by User

Posted on January 13, 2023June 4, 2023 by paytonflint

The Configuration Manager PowerShell module contains the Get-CMUserDeviceAffinity cmdlet to allow one to use an AD user object to search for devices that the user is the Primary User for. I’ve written a script which first runs an LDAP query to derive the AD user objects from a list in First Name + Last Name format that a Project Manager is likely to have available to them, and then provides a report complete with the UPN, the sAMAccountName, and each device that the user is the Primary User for in MECM.

This is a simple, but very convenient script to allow one to translate from the info a PM will likely be able to provide to the information needed as an engineer. Obviously, this is not a replacement for a user-based collection/deployment, nor should it be used as such. Oftentimes, I have found it preferential to manage devices, especially for large-scale deployments, but this is not always the case, in such instances as an application utilizing user-based licensing or a specialty application. You’ll find the script below.

# Script written by Payton Flint
# See https://paytonflint.com/powershell-get-affiliated-devices-by-user/
 
#=//Prerequisites//==============================================================================================
 
# Clear variables for repeatability
Get-Variable -Exclude PWD,*Preference | Remove-Variable -EA 0
 
# Identify location of script
$ScriptPath = Split-Path ($MyInvocation.MyCommand.Path) -Parent
 
# Install/check for ConfigurationManager module
Import-Module ConfigurationManager -ErrorAction 'Stop'
 
#=//Variables//=================================================================================================
 
# Set CM Site Code
$SiteCode = '<CM SITE CODE>'
 
# Get domain name
$Domain = Get-ADDomain | Select-Object -ExpandProperty NetBIOSName
 
# Set list file name
$FileName = "UserList.txt"
 
# Set output file name
$OutputFile = "DeviceList.csv"
 
#=//Body//======================================================================================================
 
# Get content from list, ignore blank lines
$ListContent = Get-Content "$ScriptPath\$FileName" | Where-Object {$_.Trim() -ne "" }
 
# Get properties for each user w/ corresponding display name
$ListContent | ForEach-Object {
 
    # If user present in AD, get user properties
    If (Get-ADUser -LDAPFilter "(displayName=$_)") {
 
        # Get AD user properties
        $ADUser = Get-ADUser -LDAPFilter "(displayName=$_)"
 
        $ADUser | ForEach-Object {
 
            $SamAcctName = $_ | Select-Object -ExpandProperty SamAccountName
 
            # User Properties
            $UserObjProps = @{
                DisplayName       = $_.GivenName + " " + $_.Surname
                SamAcctName       = $SamAcctName
                UserPrincipalName = $_.UserPrincipalName
                DomainUser        = Join-Path $Domain $SamAcctName
            }
 
            # Create application objects using above properties
            $UserObj = New-Object psobject -Property $UserObjProps
            # Add user objects to list
            $Users += ,$UserObj
 
        }
    }
}
 
# Change provider to CM site
Set-Location $SiteCode':'
 
# For each user...
$Users | ForEach-Object {
    # Get user object instance
    $UserObjInst = $_
    # Get domain user property
    $DomainUser = $_ | Select-Object -ExpandProperty DomainUser
    # Get user device affinity from CM
    $UserDevice = Get-CMUserDeviceAffinity -UserName "$DomainUser"
    # Reset counter per user
    $count = $null
 
    # Get resource names of affiliated device and add to object
    $UserDevice.ResourceName | ForEach-Object {
        # Count up per resource instance
        $count++
        # Derive resource number
        $ResCount = "Resource" + "$count"
        # Add resource property to object
        Add-Member -MemberType NoteProperty -InputObject $UserObjInst -Name "$ResCount" -Value ($_) -Force
    }
}
 
# Change to local provider
Set-Location 'C:'
 
# Export to .CSV
$Users `
| Sort-Object -Property DisplayName `
| Export-Csv -Path "$ScriptPath\$OutputFile"

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

About The Author

Author's Portrait

In my journey as a technologist and 11 years of experience as an IT professional, I have found my niche as Director of Infrastructure Services; developing my skillsets in management, scripting, cloud infrastructure, identity management, and networking.

I have experience as a Systems Administrator and Engineer for large enterprises including the DoD, government agencies, and a nuclear-generation site.

I've been blessed to collaborate with engineers at esteemed Fortune 50 corporations, and one of Africa's largest, to ensure successful implementation of my work.

GitHub Button

Credentials

M365 Endpoint Administrator Associate
M365 Fundamentals
Microsoft AZ-900
CompTIA CSIS
CompTIA CIOS
CompTIA Security+
CompTIA Network+
CompTIA A+
  • April 2025
  • December 2024
  • November 2024
  • October 2024
  • September 2024
  • August 2024
  • May 2024
  • April 2024
  • March 2024
  • February 2024
  • January 2024
  • December 2023
  • November 2023
  • October 2023
  • September 2023
  • August 2023
  • July 2023
  • June 2023
  • May 2023
  • April 2023
  • March 2023
  • February 2023
  • January 2023
  • December 2022
  • November 2022
  • October 2022
  • September 2022
  • August 2022
© 2022 Payton Flint | The views and opinions expressed on this website belong solely to the author/owner and do not represent the perspectives of any individuals, institutions, or organizations, whether affiliated personally or professionally, unless explicitly stated otherwise. The content and products on this website are provided as-is with no warranties or guaranties, are for informational/demonstrative purposes only, do not constitute professional advice, and are not to be used maliciously. The author/owner is not responsible for any consequences arising from actions taken based on information provided on this website, nor from the use/misuse of products from this site. All trademarks are the property of their respective owners.