forked from Micke-K/IntuneManagement
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathEnrollmentStatusPage.psm1
More file actions
288 lines (236 loc) · 8.78 KB
/
EnrollmentStatusPage.psm1
File metadata and controls
288 lines (236 loc) · 8.78 KB
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
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
########################################################
#
# Common module functions
#
########################################################
function Add-ModuleMenuItems
{
Add-MenuItem (New-Object PSObject -Property @{
Title = (Get-ESPName)
MenuID = "IntuneGraphAPI"
Script = [ScriptBlock]{Get-ESPs}
})
}
function Get-SupportedImportObjects
{
$global:importObjects += (New-Object PSObject -Property @{
Selected = $true
Title = (Get-ESPName)
Script = [ScriptBlock]{
param($rootFolder)
Write-Status "Import all enrollment status page settings"
Import-AllESPObjects (Join-Path $rootFolder (Get-ESPFolderName))
}
})
}
function Get-SupportedExportObjects
{
$global:exportObjects += (New-Object PSObject -Property @{
Selected = $true
Title = (Get-ESPName)
Script = [ScriptBlock]{
param($rootFolder)
Write-Status "Export all enrollment status page settings"
Get-ESPObjects | ForEach-Object { Export-SingleESP $PSItem.Object (Join-Path $rootFolder (Get-ESPFolderName)) }
}
})
}
function Export-AllObjects
{
param($addObjectSubfolder)
$subFolder = ""
if($addObjectSubfolder) { $subFolder = Get-ESPFolderName }
}
########################################################
#
# Object specific functions
#
########################################################
function Get-ESPName
{
return "Enrollment Status Page"
}
function Get-ESPFolderName
{
return "EnrollmentStatusPage"
}
function Get-ESPs
{
Write-Status "Loading enrollment status page objects"
$dgObjects.ItemsSource = @(Get-ESPObjects)
#Scriptblocks that will perform the export tasks. empty by default
$script:exportParams = @{}
$script:exportParams.Add("ExportAllScript", [ScriptBlock]{
Export-AllESPs $global:txtExportPath.Text
Set-ObjectGrid
Write-Status ""
})
$script:exportParams.Add("ExportSelectedScript", [ScriptBlock]{
Export-SelectedESP $global:txtExportPath.Text
Set-ObjectGrid
Write-Status ""
})
#Scriptblock that will perform the import all files
$script:importAll = [ScriptBlock]{
Import-AllESPObjects $global:txtExportPath.Text
Set-ObjectGrid
}
#Scriptblock that will perform the import of selected files
$script:importSelected = [ScriptBlock]{
Import-ESPObjects $global:lstFiles.ItemsSource -Selected
Set-ObjectGrid
}
#Scriptblock that will read json files
$script:getImportFiles = [ScriptBlock]{
Show-FileListBox
$global:lstFiles.ItemsSource = @(Get-JsonFileObjects $global:txtImportPath.Text -Exclude "*_Settings.json")
}
Add-DefaultObjectButtons -export ([scriptblock]{Show-DefaultExportGrid @script:exportParams}) -import ([scriptblock]{Show-DefaultImportGrid -ImportAll $script:importAll -ImportSelected $script:importSelected -GetFiles $script:getImportFiles}) -copy ([scriptblock]{Copy-ESP}) -ViewFullObject ([scriptblock]{Get-ESPObject $global:dgObjects.SelectedItem.Object})
}
function Get-ESPObjects
{
Get-GraphObjects -Url "/deviceManagement/deviceEnrollmentConfigurations"
}
function Get-ESPObject
{
param($object, $additional = "")
if(-not $Object.id) { return }
Invoke-GraphRequest -Url "/deviceManagement/deviceEnrollmentConfigurations/$($Object.id)$additional"
}
function Export-AllESPs
{
param($path = "$env:Temp")
if(-not (Test-Path $path)) { mkdir -Path $path -Force -ErrorAction SilentlyContinue | Out-Null }
if(Test-Path $path)
{
foreach($objTmp in ($global:dgObjects.ItemsSource))
{
Export-SingleESP $objTmp.Object $path
}
}
}
function Export-SelectedESP
{
param($path = "$env:Temp")
Export-SingleESP $global:dgObjects.SelectedItem.Object $path
}
function Export-SingleESP
{
param($psObj, $path = "$env:Temp")
if(-not $psObj) { return }
if($global:runningBulkExport -ne $true)
{
if($global:chkAddCompanyName.IsChecked) { $path = Join-Path $path $global:organization.displayName }
if($global:chkAddObjectType.IsChecked) { $path = Join-Path $path (Get-ESPFolderName) }
}
if(-not (Test-Path $path)) { mkdir -Path $path -Force -ErrorAction SilentlyContinue | Out-Null }
if(Test-Path $path)
{
Write-Status "Export $($psObj.displayName)"
$obj = Invoke-GraphRequest -Url "/deviceManagement/deviceEnrollmentConfigurations/$($psObj.id)" #?`$expand=assignments"
if($obj)
{
if($obj.id -like "*_default*")
{
$idx = $obj.id.ToLower().IndexOf("_default")
$baseName = "Default_" + $obj.id.SubString($idx + "_default".Length)
}
else
{
# ?`$expand=assignments is not working so get assignments
$assignments = Invoke-GraphRequest -Url "/deviceManagement/deviceEnrollmentConfigurations/$($obj.id)/assignments"
if($assignments.value)
{
$obj | Add-Member -NotePropertyName "assignments" -NotePropertyValue $assignments.value
}
$baseName = Remove-InvalidFileNameChars $obj.displayName
}
$fileName = "$path\$baseName.json"
ConvertTo-Json $obj -Depth 5 | Out-File $fileName -Force
Add-MigrationInfo $obj.assignments
}
$global:exportedObjects++
}
}
function Copy-ESP
{
if(-not $dgObjects.SelectedItem)
{
[System.Windows.MessageBox]::Show("No object selected`n`nSelect enrollment status page item you want to copy", "Error", "OK", "Error") | Out-Null
return
}
if($dgObjects.SelectedItem.Object.id -like "*_default*")
{
[System.Windows.MessageBox]::Show("You cannot copy default items`n`nSelect custom entrollment status page item", "Error", "OK", "Error") | Out-Null
return
}
$ret = Show-InputDialog "Copy enrollment status page" "Select name for the new object" "$($dgObjects.SelectedItem.displayName) - Copy"
if($ret)
{
# Export profile
Write-Status "Export $($dgObjects.SelectedItem.displayName)"
# Convert to Json and back to clone the object
$obj = ConvertTo-Json $dgObjects.SelectedItem.Object -Depth 5 | ConvertFrom-Json
if($obj)
{
# Import new profile
$obj.displayName = $ret
Import-ESP $obj | Out-Null
$dgObjects.ItemsSource = @(Get-ESPObjects)
}
Write-Status ""
}
$dgObjects.Focus()
}
function Import-ESP
{
param($obj)
Start-PreImport $obj
if($obj.id -like "*_default*")
{
Write-Status "Update $($obj.displayName)"
Invoke-GraphRequest -Url "/deviceManagement/deviceEnrollmentConfigurations/$($obj.id)" -Content (ConvertTo-Json $obj -Depth 5) -HttpMethod PATCH
}
else
{
Write-Status "Import $($obj.displayName)"
Invoke-GraphRequest -Url "/deviceManagement/deviceEnrollmentConfigurations" -Content (ConvertTo-Json $obj -Depth 5) -HttpMethod POST
}
}
function Import-AllESPObjects
{
param($path = "$env:Temp")
Import-ESPObjects (Get-JsonFileObjects $path)
}
function Import-ESPObjects
{
param(
$Objects,
[switch]
$Selected
)
Write-Status "Import enrollment status page"
foreach($obj in $Objects)
{
if($Selected -and $obj.Selected -ne $true) { continue }
if($obj.Object.id -like "*_default*")
{
$idx = $obj.Object.id.ToLower().IndexOf("_default")
$extInfo = " ($($obj.Object.id.SubString($idx + "_default".Length)))"
}
else
{
$extInfo = ""
}
Write-Log "Import Enrollment Status Page: $($obj.Object.displayName)$extInfo"
$assignments = Get-GraphAssignmentsObject $obj.Object ($obj.FileInfo.DirectoryName + "\" + $obj.FileInfo.BaseName + "_assignments.json")
$response = Import-ESP $obj.Object
if($response)
{
$global:importedObjects++
Import-GraphAssignments $assignments "enrollmentConfigurationAssignments" "/deviceManagement/deviceEnrollmentConfigurations/$($response.Id)/assign"
}
}
$dgObjects.ItemsSource = @(Get-ESPObjects)
Write-Status ""
}