forked from Kaidja/ConfigMgrSDK
-
Notifications
You must be signed in to change notification settings - Fork 0
/
How to replicate Primary Device information from ConfigMgr to AD DS
58 lines (47 loc) · 1.93 KB
/
How to replicate Primary Device information from ConfigMgr to AD DS
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
Param(
$SiteCode = "PS1",
$SiteServer= "Localhost",
$UserName = "LAB\\Kenny"
)
Function Set-ADUserPrimaryDevice
{
Param(
$UserName,
$DeviceName
)
If(($DeviceName.GetType()).BaseType -eq [System.Array]){
[ADSISearcher]$FindUser ="(&(&(&(objectCategory=user)(objectClass=user)(sAMAccountName=$(($UserName.Split("\\"))[2])))))"
$User = $FindUser.FindOne()
$EmptyArray = @()
foreach($item in $DeviceName){
[ADSISearcher]$FindDevice ="(&(objectCategory=Computer)(Name=$($item.ResourceName)))"
$Device = $FindDevice.FindOne()
$EmptyArray += $($Device.Properties.distinguishedname)
}
$AddUserProperty = $User.GetDirectoryEntry()
$AddUserProperty."msds-primarycomputer" = $EmptyArray
$AddUserProperty.SetInfo()
}
Else{
[ADSISearcher]$FindUser ="(&(&(&(objectCategory=user)(objectClass=user)(sAMAccountName=$UserName))))"
$User = $FindUser.FindOne()
[ADSISearcher]$FindDevice ="(&(objectCategory=Computer)(Name=$($DeviceName.ResourceName)))"
$Device = $FindDevice.FindOne()
$AddUserProperty = $User.GetDirectoryEntry()
$AddUserProperty."msds-primarycomputer" = $($Device.Properties.distinguishedname)
$AddUserProperty.SetInfo()
}
}
Function Get-CMUserPrimaryDevice
{
Param(
$SiteCode,
$SiteServer,
$UserName
)
$PrimaryDeviceQuery = Get-WmiObject -Namespace "root\SMS\Site_$SiteCode" -Class SMS_UserMachineRelationship -Filter "UniqueUserName='$UserName' and ResourceClientType='1'" -ComputerName $SiteServer
$PrimaryDeviceQuery
}
# ******************** Script Entry Point ****************************************
$PrimaryDevice = Get-CMUserPrimaryDevice -SiteCode $SiteCode -SiteServer $SiteServer -UserName $UserName
Set-ADUserPrimaryDevice -UserName $UserName -DeviceName $PrimaryDevice