SCCM Collection affinity script

Welcome Forums General PowerShell Q&A SCCM Collection affinity script

This topic contains 2 replies, has 3 voices, and was last updated by

 
Participant
2 years, 9 months ago.

  • Author
    Posts
  • #38308

    Participant
    Points: 0
    Rank: Member

    I've written the below script to get user and device affinity information. The script checks to see if its a user or device collection and switch runs against the right commands. It works, but for improvement purposes could i done something more efficiently ?

    Function Get-CollectionAffinityData {
    
    [cmdletbinding()]
    
    param (     
        
        [Parameter(Mandatory=$true)]
        [ValidateNotNull()]
        [ValidateNotNullOrEmpty()]
        [string]
        $Collection
    )
    
    
    $script:Collection = Get-CMCollection -Name $Collection
    
    switch ($script:Collection.CollectionType) 
        {
        
            0 { "Not supported in this script" }
      
            1 { $users = Get-CMUser -CollectionName $script:Collection.name
                            
                            $script:array  = @()
                            
                            $users | foreach {
    
                            $data = New-Object PSObject -Property @{
    
                                PrimaryUser =  $_.SMSID
                                PrimaryDevices = if (($_ = Get-CMUserDeviceAffinity -UserName $_.SMSID).ResourceName -eq $null) { '$Null' } else { $_.ResourceName }
                                LastClientCheckDate =  ForEach ($y in $_.ResourceName ) { ( Get-CMDevice -Name $y ).LastClientCheckTime.ToShortDateString() }                     
                                    
                                }   
                            $script:array += $data
                                }
        
             } #End of switch "1"
      
           2 { $Devices = Get-CMDevice -CollectionName $script:Collection.Name
                            
                            $script:array = @()
    
                            $users = $devices | foreach { $_.UserDomainName + "\" + $_.UserName } | foreach { Get-CMUser -Name $_ }
                            
                            $users | foreach {   
                                                                               
                            $data = New-Object PSObject -Property @{
    
                                PrimaryUser =  $_.SMSID
                                PrimaryDevices = if (($_ = Get-CMUserDeviceAffinity -UserName $_.SMSID).ResourceName -eq $null) { '$Null' } else { $_.ResourceName }
                                LastClientCheckDate =  ForEach ($y in $_.ResourceName ) { ( Get-CMDevice -Name $y ).LastClientCheckTime.ToShortDateString() }                     
                                }    
                            $script:array += $data
                                } 
    
            } #End of switch "2"
    
        }
    
    echo $script:array 
    
    }#End of function
    
    

    output looks like this:

    PrimaryDevices LastClientCheckDate PrimaryUser
    ————– ——————- ———–
    {DEVICE1, DEVICE2} {20/04/2016, 08/03/2016} DOMAIN\USER1, DOMAIN\USER2
    DEVICE3 01/03/2016 DOMAIN\USER3

  • #38359

    Participant
    Points: 0
    Rank: Member

    There may be some efficiency improvements based on your $devices switch doing a get-CMDevice for the collection and then in the foreach doing another get-CMDevice. But if it is working your are that the point where you would need to start measuring the impact of changes and digging into the data the is available in your current objects.

  • #38372

    Participant
    Points: 0
    Rank: Member

    Thank you jonathan, I will take a look. Appricate your reply.

The topic ‘SCCM Collection affinity script’ is closed to new replies.

denizli escort samsun escort muğla escort ataşehir escort kuşadası escort