Clear Device User Affinity in Configuration Manager 2012 R2

A couple of days ago I got a question on how one would go ahead and clear all Device/User affinity in a Configuration Manager 2012 Site. There are some PowerShell methods that we can utilize for our script to do this. It is quite simple “for-each” actually but of course we do prefer to add some checks and logging as well.

Note: This script have been tested on both Configuration Manager 2012 SP1 CU3 and Configuration Manager 2012 R2 in a Single Primary Standalone configuration.

#Find the PowerShell Module for ConfigMgr
$ConfigMgrModulePath = Join-Path ($Env:SMS_ADMIN_UI_PATH.ToString().SubString(0,$Env:SMS_ADMIN_UI_PATH.Length – 5)) "ConfigurationManager.psd1"

#Load the PowerShell module for ConfigMgr
Import-Module $ConfigMgrModulePath -Verbose

#Ask the user if we are running ‘debug’ or ‘for real’
            $title = "Execution Mode"
            $message = "Would you like to run in Execute Mode or Demo Mode?"

            $exec = New-Object System.Management.Automation.Host.ChoiceDescription "&Execute", `
                "Executes action to remove User/Device Affinity."

            $query = New-Object System.Management.Automation.Host.ChoiceDescription "&Demo", `
                "Performs Query to see what would have been done."

            $options = [System.Management.Automation.Host.ChoiceDescription[]]($exec, $query)

            #Mode value 0 = execute, 1 = query
            $ExecutionMode = $host.ui.PromptForChoice($title, $message, $options, 0)

#Enter PSDrive
cd "$((Get-PSDrive -PSProvider CMSite).Name):"

#Get all those devices
foreach ($Device in (Get-CMDevice))
{
    #Get ALL users registred on this device
    $PrimaryUsers = Get-CMUserDeviceAffinity -DeviceNam $Device.Name
    #For each user on that device…
    foreach ($PrimaryUser in $PrimaryUsers)
    {
        # … try to…
        try
        {
            # if we are in execution mode of course…
            if ($ExecutionMode -eq 0)
            {
                # remove the device from the user
                Remove-CMDeviceAffinityFromUser -DeviceId $Device.ResourceID -UserName $PrimaryUser.UniqueUserName
                Write-Host "Successfully removed device $($Device.Name) from user $($PrimaryUser.UniqueUserName)."
            }
            else
            {
                Write-Host "Would remove device $($Device.Name) from user $($PrimaryUser.UniqueUserName)."
            }
        }
        catch
        {
            Write-Error "Unable to remove device $($Device.Name) from user $($PrimaryUser.UniqueUserName)."
            break
        }
    }
}

 

The interesting parts in this script are:

  • “foreach ($Device in (Get-CMDevice))” where we enumerats all devices
  • “$PrimaryUsers = Get-CMUserDeviceAffinity -DeviceNam $Device.Name” where we get all users for a device
  • “Remove-CMDeviceAffinityFromUser -DeviceId $Device.ResourceID -UserName $PrimaryUser.UniqueUserName” where we actually do the removal

Hope this helps!

 

You can download the script here.

m4s0n501

About The Author

Tim Nilimaa is a consultant with Lumagate in Sweden. He has been working with Configuration Manager for 8 years. His knowledge has been selected as a speaker at many events among them Microsoft Management Summit.

No Comments

Leave A Reply