-
Notifications
You must be signed in to change notification settings - Fork 0
/
AssignGroupTag_AutopilotPC.ps1
140 lines (94 loc) · 3.67 KB
/
AssignGroupTag_AutopilotPC.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
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
function Read-HostYesNo ([string]$Title, [string]$Prompt, [boolean]$Default)
{
# Set up native PowerShell choice prompt with Yes and No
$yes = New-Object System.Management.Automation.Host.ChoiceDescription "&Yes"
$no = New-Object System.Management.Automation.Host.ChoiceDescription "&No"
$options = [System.Management.Automation.Host.ChoiceDescription[]]($yes, $no)
# Set default option
$defaultChoice = 0 # first choice = Yes
if ($Default -eq $false) { # only if it was given and is false
$defaultChoice = 1 # second choice = No
}
$result = $Host.UI.PromptForChoice($Title, $Prompt, $options, $defaultChoice)
if ($result -eq 0) { # 0 is yes
return $true
} else {
return $false
}
}
####################################################
function ConnectToGraph
{
if (Get-Module -ListAvailable -Name Microsoft.Graph.Intune)
{
}
else {
Write-Host "Microsoft.Graph.Intune Module does not exist, installing..."
Install-Module -Name Microsoft.Graph.Intune
}
<#
$yourUPN = "xxx@kunintune.onmicrosoft.com"
$password = ConvertTo-SecureString 'xxxxx' -AsPlainText -Force
$creds = New-Object System.Management.Automation.PSCredential ($yourUPN, $password)
#>
#Connect-MSGraph -PSCredential $creds
connect-MSGraph
}
########################################################
function Get-AllAutopilotdevices()
{
$URL = "https://graph.microsoft.com/beta/deviceManagement/windowsAutopilotDeviceIdentities"
$result = invoke-MSGraphRequest -HttpMethod GET -Url $URL | Get-MSGraphAllPages
return $result
}
#######################################################
function update-AutopilotgroupTab($autopilotDeviceID, $GroupTag)
{
$URL = "https://graph.microsoft.com/beta/deviceManagement/windowsAutopilotDeviceIdentities/$autopilotDeviceID/UpdateDeviceProperties"
Write-host $URL
try {
Invoke-MSGraphRequest -Url $URL -Content "{groupTag: '$GroupTag'}" -HttpMethod POST -Verbose
}
catch {
Write-Host($_)
Write-host $URL
Continue
}
}
#######################################################
ConnectToGraph
#read CSv
# When entering a CSV path, "" is unnecessary even if there is a space in the file path.
$CSVPath = Read-Host("CSV Enter the file path")
$CSVDevices = Import-Csv -Path $CSVPath -Header "Serialnumber"
Write-host $CSVDevices
#all Autopilot devices
$Autopilotdevice = Get-AllAUtopilotdevices | select id, groupTag, serialNumber
$Autopilotdevice | format-table
$GroupTag = Read-Host("Put Group tag need to be added")
write-host
$displayTable = Read-HostYesNo -Prompt "Do you want to change group tag to $GroupTag ?" -Default $true
if($displayTable){
if($CSVDevices){
foreach($Device in $CSVDevices)
{
if ($Device -eq "Serial number")
{
Continue
}
else
{
$DeviceGroupTagNeedChange = $Autopilotdevice | Where-Object {$_.Serialnumber -eq $Device.Serialnumber}
if ($DeviceGroupTagNeedChange)
{
write-host $DeviceGroupTagNeedChange.id
update-AutopilotgroupTab -autopilotDeviceID $DeviceGroupTagNeedChange.id -GroupTag $GroupTag
$deviceseralnumber = $Device.Serialnumber
Write-host "Device with Serial number $deviceseralnumber has been changed its GroupTag with $GroupTag"
write-host
}
}
}
}
Write-Host
}