-
Notifications
You must be signed in to change notification settings - Fork 11
/
Copy pathSet-PrimaryUser.ps1
98 lines (82 loc) · 3.8 KB
/
Set-PrimaryUser.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
####################################################
# Connect to Intune
$resourceURL = "https://graph.microsoft.com/"
$response = [System.Text.Encoding]::Default.GetString((Invoke-WebRequest -UseBasicParsing -Uri "$($env:IDENTITY_ENDPOINT)?resource=$resourceURL" -Method 'GET' -Headers @{'X-IDENTITY-HEADER' = "$env:IDENTITY_HEADER"; 'Metadata' = 'True'}).RawContentStream.ToArray()) | ConvertFrom-Json
#$script:authToken = $response.access_token
$script:authToken = @{
'Content-Type' = 'application/json'
'Authorization' = "Bearer " + $response.access_token
}
####################################################
Function Get-VirtualDevices() {
$uri = "https://graph.microsoft.com/beta/deviceManagement/managedDevices?`$filter=startswith(deviceName,'VM-')&`$select=id,usersLoggedOn"
$DevicesResponse = (Invoke-RestMethod -Uri $uri -Headers $authToken -Method Get)
$Devices += $DevicesResponse.value
$DevicesNextLink = $DevicesResponse."@odata.nextLink"
while ($null -ne $DevicesNextLink) {
$DevicesResponse = (Invoke-RestMethod -Uri $DevicesNextLink -Headers $authToken -Method Get)
$DevicesNextLink = $DevicesResponse."@odata.nextLink"
$Devices += $DevicesResponse.value
}
<#
$uri = "https://graph.microsoft.com/beta/deviceManagement/managedDevices?`$filter=model eq 'Virtual Machine'&`$select=id,usersLoggedOn"
$DevicesResponse = (Invoke-RestMethod -Uri $uri -Headers $authToken -Method Get)
$Devices += $DevicesResponse.value
$DevicesNextLink = $DevicesResponse."@odata.nextLink"
while ($null -ne $DevicesNextLink) {
$DevicesResponse = (Invoke-RestMethod -Uri $DevicesNextLink -Headers $authToken -Method Get)
$DevicesNextLink = $DevicesResponse."@odata.nextLink"
$Devices += $DevicesResponse.value
}
#>
return $Devices
}
function Set-PrimaryUser() {
param (
$DEVID,
$USR
)
try {
$uri = "https://graph.microsoft.com/beta/deviceManagement/manageddevices('$DEVID')/users/`$ref"
$USRUri = "https://graph.microsoft.com/beta/users/" + $USR
$id = "@odata.id"
$Body = @{ $id="$USRUri" } | ConvertTo-Json -Compress
$response = (Invoke-RestMethod -Uri $uri -Headers $authToken -Method POST -Body $Body -ContentType "application/json")
return $response
}
catch {
Write-output "Error : $($error[0].exception.message)"
}
}
function Get-PrimaryUser() {
param (
$DEVID
)
try {
$uri = "https://graph.microsoft.com/beta/deviceManagement/manageddevices('$DEVID')/users"
$DevicesResponse = (Invoke-RestMethod -Uri $uri -Headers $authToken -Method Get)
$User = $DevicesResponse.value.id
return $User
}
catch {
Write-output "Error : $($error[0].exception.message)"
}
}
####################################################
$Devices = Get-VirtualDevices
Foreach ($Device in $Devices){
$ID=$Device.ID
$loggedonUser=$Device.usersLoggedOn.userId | Select-Object -Last 1
Write-output "Checking Device $ID with LoggedonUser $loggedonUser"
$PrimUser=Get-PrimaryUser -DEVID $ID
Write-output "Got PrimaryUser of Device $ID with ID $PrimUser"
If(($null -ne $loggedonUser) -and ($loggedonUser -ne $PrimUser))
{
Write-output "LoggedonUser is different from Primary User, setting Primary User"
Set-PrimaryUser -DEVID $ID -USR $loggedonUser
}
Write-output ""
Write-output "################################################################################"
Write-output ""
}