-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSCCM - User Display Name to Primary Devices.ps1
65 lines (45 loc) · 2.08 KB
/
SCCM - User Display Name to Primary Devices.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
<#
.NOTES
===========================================================================
Created on: 2022-04-27
Created by: Brian Thorp
===========================================================================
.Description
Imports a CSV File of User's DisplayNames, Looks them up in AD -> Username, and adds primarydevice from SCCM to it
#>
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process
Import-Module ActiveDirectory
$Domain = "Contoso"
# ---------------------------------------------------------------------
# Import SCCM Site
# ---------------------------------------------------------------------
# Site configuration
$SiteCode = "CM1" # Site code
$ProviderMachineName = "sccm.contoso.com" # SMS Provider machine name
# Customizations
$initParams = @{}
# Do not change anything below this line
# Import the ConfigurationManager.psd1 module
if((Get-Module ConfigurationManager) -eq $null) {
Import-Module "$($ENV:SMS_ADMIN_UI_PATH)\..\ConfigurationManager.psd1" @initParams
}
# Connect to the site's drive if it is not already present
if((Get-PSDrive -Name $SiteCode -PSProvider CMSite -ErrorAction SilentlyContinue) -eq $null) {
New-PSDrive -Name $SiteCode -PSProvider CMSite -Root $ProviderMachineName @initParams
}
# Set the current location to be the site code.
Set-Location "$($SiteCode):\" @initParams
# ---------------------------------------------------------------------
$List = Import-CSV -Path "UserList.csv"
$NewCSV = ForEach ($Entry in $List)
{
#write-host $Entry
$UserDisplayName = $Entry.'User Name'
$UserDisplayName = $UserDisplayName -Replace '\s', "*"
# Input Firstname Lastname and output sAMAccountname
$sAMAccountname = (Get-ADUser -Filter { Name -like $UserDisplayName }).samaccountname
# Get User Device Affinity
$Entry.'Primary Computer Name' = Get-CMUserDeviceAffinity -UserName "$($Domain)\$($sAMAccountName)" | Select-Object -ExpandProperty ResourceName
$Entry
}
$NewCSV | Export-CSV C:\Contoso\tstusrs2.csv -NoTypeInformation