-
-
Notifications
You must be signed in to change notification settings - Fork 58
Expand file tree
/
Copy pathBackup-IntuneConfiguration.ps1
More file actions
108 lines (98 loc) · 4.36 KB
/
Copy pathBackup-IntuneConfiguration.ps1
File metadata and controls
108 lines (98 loc) · 4.36 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
<#
.SYNOPSIS
Export Intune configuration objects to JSON for backup / source control.
.DESCRIPTION
Exports configuration profiles (classic + settings catalog), compliance
policies, platform scripts, proactive remediations, app protection policies,
Autopilot deployment profiles, assignment filters and mobileApps as JSON
files into a dated folder. Each object is written separately so the output
can be committed to git and diffed across runs.
.PARAMETER OutputPath
Root output folder. A timestamped subfolder is created inside. Default: .\backup
.PARAMETER IncludeAssignments
If set, also exports the assignments collection for each object.
.EXAMPLE
.\Backup-IntuneConfiguration.ps1 -IncludeAssignments
.NOTES
Author : Jannik Reinhard
Version: 1.0
#>
#Requires -Modules Microsoft.Graph.Authentication
[CmdletBinding()]
Param(
[string]$OutputPath = ".\backup",
[switch]$IncludeAssignments
)
$endpoints = @(
@{ Name = 'deviceConfigurations'; Uri = 'deviceManagement/deviceConfigurations' }
@{ Name = 'configurationPolicies'; Uri = 'deviceManagement/configurationPolicies' }
@{ Name = 'deviceCompliancePolicies'; Uri = 'deviceManagement/deviceCompliancePolicies' }
@{ Name = 'deviceManagementScripts'; Uri = 'deviceManagement/deviceManagementScripts' }
@{ Name = 'deviceShellScripts'; Uri = 'deviceManagement/deviceShellScripts' }
@{ Name = 'deviceHealthScripts'; Uri = 'deviceManagement/deviceHealthScripts' }
@{ Name = 'windowsAutopilotDeploymentProfiles'; Uri = 'deviceManagement/windowsAutopilotDeploymentProfiles' }
@{ Name = 'assignmentFilters'; Uri = 'deviceManagement/assignmentFilters' }
@{ Name = 'mobileApps'; Uri = 'deviceAppManagement/mobileApps' }
@{ Name = 'managedAppPolicies'; Uri = 'deviceAppManagement/managedAppPolicies' }
@{ Name = 'mobileAppConfigurations'; Uri = 'deviceAppManagement/mobileAppConfigurations' }
)
function Connect-MgGraphIfNeeded {
if (-not (Get-MgContext)) {
Connect-MgGraph -Scopes @(
"DeviceManagementConfiguration.Read.All",
"DeviceManagementApps.Read.All",
"DeviceManagementServiceConfig.Read.All"
) -NoWelcome
}
}
function Get-AllPages {
Param([Parameter(Mandatory)][string]$RelativeUri)
$items = [System.Collections.Generic.List[object]]::new()
$uri = "https://graph.microsoft.com/beta/$RelativeUri"
while ($uri) {
$page = Invoke-MgGraphRequest -Method GET -Uri $uri
foreach ($v in $page.value) { $items.Add($v) }
$uri = $page.'@odata.nextLink'
}
return $items
}
function Save-Object {
Param(
[Parameter(Mandatory)] [object]$Object,
[Parameter(Mandatory)] [string]$Folder
)
$name = if ($Object.displayName) { $Object.displayName } elseif ($Object.name) { $Object.name } else { $Object.id }
$safe = ($name -replace '[\\/:*?"<>|]', '_') + '_' + $Object.id + '.json'
$path = Join-Path $Folder $safe
($Object | ConvertTo-Json -Depth 25) | Out-File -FilePath $path -Encoding UTF8
}
try {
Connect-MgGraphIfNeeded
$stamp = (Get-Date).ToString('yyyyMMdd-HHmmss')
$root = Join-Path -Path $OutputPath -ChildPath $stamp
New-Item -ItemType Directory -Path $root -Force | Out-Null
foreach ($e in $endpoints) {
Write-Host "Exporting $($e.Name)..." -ForegroundColor Cyan
$folder = Join-Path $root $e.Name
New-Item -ItemType Directory -Path $folder -Force | Out-Null
$items = Get-AllPages -RelativeUri $e.Uri
Write-Host " $($items.Count) items"
foreach ($item in $items) {
if ($IncludeAssignments) {
try {
$a = Invoke-MgGraphRequest -Method GET `
-Uri "https://graph.microsoft.com/beta/$($e.Uri)/$($item.id)/assignments"
$item | Add-Member -NotePropertyName '_assignments' -NotePropertyValue $a.value -Force
} catch {
Write-Verbose "No assignments for $($item.id): $_"
}
}
Save-Object -Object $item -Folder $folder
}
}
Write-Host "Backup completed: $root" -ForegroundColor Green
exit 0
} catch {
Write-Error "Backup-IntuneConfiguration failed: $_"
exit 1
}