Monday, April 18, 2016

Set SCCM primary User/Device (Affinity). use as module

When you want to set a device to a primary User.
This script lets you view, add en remove primary users from SCCM without the need of a SCCM management console. Use an account that has a appropriate security level.

Import this script like: import-module 'path to script' -argumentlist 'SITESERVER','SITECODE'
Use Functions like:
Get-PrimaryDevice 'userID'
Get-PrimaryUser 'Computername'
Set-Affinity 'userID' 'ComputerName'
Remove-Affinity 'userID' 'ComputerName'


 param(  
      [string]$Siteserver,  
      [string]$Sitecode  
      )  

 Function Get-PrimaryDevice  
 {  
   PARAM
      (  
           [String]$User       
      )  
   Get-WmiObject -ComputerName $SITESERVER -Namespace "root\SMS\Site_$SITECODE" -Query "select * from SMS_UserMachineRelationship WHERE UniqueUserName like'%$User%' AND IsActive = '1' AND Types = '1'" 
 }  
 Function Get-PrimaryUser  
 {  
   PARAM
      (  
           [String]$Device  
      )    
   Get-WmiObject -ComputerName $SiteServer -Namespace "root\SMS\Site_$SiteCode" -Query "select * from SMS_UserMachineRelationship WHERE ResourceName like '%$Device%' AND IsActive = '1' AND Types = '1'"
 }  
 Function Set-Affinity  
 {  
   PARAM
      (  
     [String]$User,  
           [String]$Device  
      )    
   $CMDeviceResourceID = Get-WmiObject -Namespace "root\SMS\site_$SiteCode" -Class SMS_R_System -ComputerName $SiteServer -Filter "Name like '%$Device%'" | Select-Object -ExpandProperty ResourceID  
      $CMUserName = Get-WmiObject -Namespace "root\SMS\site_$SiteCode" -Class SMS_R_User -ComputerName $SiteServer -Filter "Name like '%$User%'" | Select-Object -ExpandProperty Name  
   $WMIConnection = [WmiClass]"\\$($SiteServer)\root\SMS\site_$($SiteCode):SMS_UserMachineRelationship"  
      $NewRelation = $WMIConnection.psbase.GetMethodParameters("CreateRelationship")  
   $NewRelation.MachineResourceId = $CMDeviceResourceID  
      $NewRelation.SourceId = 2  
      $NewRelation.TypeId = 1  
      $NewRelation.UserAccountName = $CMUserName  
   $WMIConnection.psbase.InvokeMethod("CreateRelationship", $NewRelation, $null)  
 }  
 Function Remove-Affinity  
 {  
   PARAM
      (  
     [String]$User,  
     [String]$Device  
      )  
      $CMDeviceResourceID = Get-WmiObject -Namespace "root\SMS\site_$SiteCode" -Class SMS_R_System -ComputerName $SiteServer -Filter "Name like '%$Device%'" | Select-Object -ExpandProperty ResourceID  
      $CMUserName = Get-WmiObject -Namespace "root\SMS\site_$SiteCode" -Class SMS_R_User -ComputerName $SiteServer -Filter "Name like '%$User%'" | Select-Object -ExpandProperty Name  
      $RelationshipResourceID = Get-WmiObject -ComputerName $SiteServer -Namespace "root\SMS\Site_$SiteCode" -Query "select * from SMS_UserMachineRelationship WHERE UniqueUserName like'%$User%' AND ResourceName like '%$Device' AND IsActive = '1' AND Types = '1'"  
   $RelationshipResourceID.psbase.delete() | New-PSSession -computername $Siteserver  
 }  
 Write-host "Import this script like: import-module 'path to script' -argumentlist 'SITESERVER','SITECODE'`nUse Functions like:`nGet-PrimaryDevice 'userID'`nGet-PrimaryUser 'Computername'`nSet-Affinity 'userID' 'ComputerName'`nRemove-Affinity 'userID ComputerName'" -ForegroundColor Cyan   

2 comments:

  1. Hello Dimitry, this looks like exactly what I am looking for. I am very new to Powershell, and SCCM. What I hope to accomplish is to set a collection variable in SCCM where the front line staff can enter a user name "ad\user" that would become the primary user of the currently imaged computer.
    Could you assist me in how to add this to a task sequence? I saved your script to our share point, and created a new script to import and call the function:

    import-module '\\our_site_server\source$\SCCM-Scripts\SetAffinityModule.ps1' -ArgumentList 'OurSiteServer','OurSiteCode'
    $tsenv = New-Object -ComObject Microsoft.SMS.TSEnvironment
    $PUser = $tsenv.value("PrimaryUser")
    $HName = hostname
    Set-Affinity '$PUser' '$HName'

    the value of PrimaryUser comes from the collection variable that was filled in by the imaging technician.

    ReplyDelete
  2. Borgata Hotel Casino & Spa - Mapyro
    Borgata Hotel Casino & 천안 출장안마 Spa · Rooms · Business/Area · Business/Area · Business/Area. 1 Borgata Way Atlantic City, NJ 08401 (609) 317-7117. Rating: 2.9 · ‎1,896 reviews · ‎Price range: $ (Based on Average Nightly Rates for a Standard Room from our 전라남도 출장마사지 Partners)What are some of the 당진 출장샵 property amenities at Borgata Hotel Casino & Spa?Which 고양 출장마사지 room amenities are available at Borgata Hotel Casino 태백 출장안마 & Spa?

    ReplyDelete