-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathAddIosDevicesToEnrollmentProfile.ps1
245 lines (193 loc) · 9.06 KB
/
AddIosDevicesToEnrollmentProfile.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
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
<#
Version: 1.0
Author: Jay Williams
Script: AddIosDevicesToEnrollmentProfile.ps1
Description:
Uses Graph API to get Enrollment Tokens, Enrollment Profiles, and creates a POST to assign serials to enrollment profile.
Assumes Graph auth access token variable is $Token. See https://www.thelazyadministrator.com/2019/07/22/connect-and-navigate-the-microsoft-graph-api-with-powershell/ for more details.
The script is provided "AS IS" with no warranties.
#>
Add-Type -AssemblyName System.Windows.Forms,PresentationCore,PresentationFramework,System.Drawing
############ STEP 1 // Enrollment Program Token ############
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Select an Enrollment Program Token'
$form.Size = New-Object System.Drawing.Size(300,200)
$form.StartPosition = 'CenterScreen'
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = 'OK'
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = 'Cancel'
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Please select an Enrollment Program Token:'
$form.Controls.Add($label)
$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point(10,40)
$listBox.Size = New-Object System.Drawing.Size(260,20)
$listBox.Height = 80
$apiUrl1 = "https://graph.microsoft.com/beta/deviceManagement/depOnboardingSettings"
$rest1 = Invoke-RestMethod -Headers @{Authorization = "Bearer $($Token)"} -Uri $apiUrl1 -Method Get
$enTokens = @($rest1.value)
$listboxCollection = @()
foreach ($enToken in $enTokens) {
$object = New-Object System.Object
$object | Add-Member -MemberType NoteProperty -Name TokenId -value $enToken.id
$object | Add-Member -MemberType NoteProperty -Name TokenName -value $enToken.tokenName
$object | Add-Member -MemberType NoteProperty -Name Token -value $enToken
$listboxCollection += $object
}
$listBox.Items.AddRange($listboxCollection)
$listBox.ValueMember = "TokenId"
$listBox.DisplayMember = "TokenName"
$form.Controls.Add($listBox)
$form.Topmost = $true
$result = $form.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK) {
$selectedTokenName = $listBox.SelectedItem.TokenName
$selectedTokenId = $listBox.SelectedItem.TokenId
}
############ STEP 2 // Enrollment Profile ############
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Select an Enrollment Profile'
$form.Size = New-Object System.Drawing.Size(300,200)
$form.StartPosition = 'CenterScreen'
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = 'OK'
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = 'Cancel'
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Please select an Enrollment Profile:'
$form.Controls.Add($label)
$listBox = New-Object System.Windows.Forms.ListBox
$listBox.Location = New-Object System.Drawing.Point(10,40)
$listBox.Size = New-Object System.Drawing.Size(260,20)
$listBox.Height = 80
$apiUrl2 = "https://graph.microsoft.com/beta/deviceManagement/depOnboardingSettings/$selectedTokenId/enrollmentProfiles"
$rest2 = Invoke-RestMethod -Headers @{Authorization = "Bearer $($Token)"} -Uri $apiUrl2 -Method Get
$enProfiles = @($rest2.value)
$listboxCollection = @()
foreach ($enProfile in $enProfiles) {
$object = New-Object System.Object
$object | Add-Member -MemberType NoteProperty -Name ProfileId -value $enProfile.id
$object | Add-Member -MemberType NoteProperty -Name ProfileName -value $enProfile.displayName
$object | Add-Member -MemberType NoteProperty -Name Profile -value $enProfile
$listboxCollection += $object
}
$listBox.Items.AddRange($listboxCollection)
$listBox.ValueMember = "ProfileId"
$listBox.DisplayMember = "ProfileName"
$form.Controls.Add($listBox)
$form.Topmost = $true
$result = $form.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK) {
$selectedProfileName = $listBox.SelectedItem.ProfileName
$selectedProfileId = $listBox.SelectedItem.ProfileId
}
############ STEP 3 // Load CSV and convert to JSON ############
$browse = New-Object System.Windows.Forms.OpenFileDialog
$browse.InitialDirectory = "c.\\"
$browse.Filter = ".csv files (*.csv)|*.CSV"
$browse.FilterIndex = 1
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Path to .CSV of Serial Numbers'
$form.Size = New-Object System.Drawing.Size(400,150)
$form.StartPosition = 'CenterScreen'
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(125,80)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = 'OK'
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(200,80)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = 'Cancel'
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,20)
$label.Text = 'Path to CSV:'
$form.Controls.Add($label)
$BrowseButton = New-Object System.Windows.Forms.Button
$BrowseButton.Location = New-Object System.Drawing.Point(300,38)
$BrowseButton.Size = New-Object System.Drawing.Size(75,22)
$BrowseButton.Text = 'Browse'
$BrowseButton.Add_Click(
{
$browse.ShowDialog()
$textBox.Text = $browse.FileName
}
)
$form.Controls.Add($BrowseButton)
$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,40)
$textBox.Size = New-Object System.Drawing.Size(285,20)
$textBox.Text = $browse.FileName
$form.Controls.Add($textBox)
$result = $form.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$csv = $textBox.Text
}
$Serials = Get-Content $csv | ConvertFrom-Csv -Header A
$body = @{
deviceIds = $Serials.A
}
$body = $body | ConvertTo-Json
############ STEP 4 // Verify and Submit ############
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Verification'
$form.Size = New-Object System.Drawing.Size(300,200)
$form.StartPosition = 'CenterScreen'
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(280,100)
$label.Text = 'Assign Serial Numbers in '+$csv+' to '+$selectedProfileName+' enrollment profile?'
$form.Controls.Add($label)
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = 'OK'
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)
$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = 'Cancel'
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)
$result = $form.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$apiUrl3 = "https://graph.microsoft.com/beta/deviceManagement/depOnboardingSettings/$selectedTokenId/enrollmentProfiles/$selectedProfileId/updateDeviceProfileAssignment"
$rest3 = Invoke-WebRequest -Headers @{Authorization = "Bearer $($Token)"} -Uri $apiUrl3 -Method Post -Body $body -ContentType 'application/json'
}
[System.Windows.MessageBox]::Show('Result: '+$rest3.StatusDescription)