Skip to content

Commit

Permalink
Include changes from PR jseerden#68 (jseerden#68)
Browse files Browse the repository at this point in the history
# Conflicts:
#	IntuneBackupAndRestore/Public/Invoke-IntuneBackupAppProtectionPolicyAssignment.ps1
#	IntuneBackupAndRestore/Public/Invoke-IntuneBackupDeviceConfiguration.ps1
#	IntuneBackupAndRestore/Public/Invoke-IntuneBackupDeviceHealthScript.ps1
#	IntuneBackupAndRestore/Public/Invoke-IntuneBackupDeviceHealthScriptAssignment.ps1
  • Loading branch information
Martin Huber (CT) committed Jun 17, 2024
2 parents 4943e67 + dcf67c9 commit f73a3ca
Show file tree
Hide file tree
Showing 36 changed files with 513 additions and 233 deletions.
41 changes: 41 additions & 0 deletions IntuneBackupAndRestore/Private/Get-MGGraphAllPages.ps1
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
function Get-MGGraphAllPages {
<#
.SYNOPSIS
Retrieve all pages of a Microsoft Graph Query
.DESCRIPTION
Retrieve all pages of a Microsoft Graph Query
.PARAMETER GraphResults
Microsoft Graph Query Results
.EXAMPLE
Invoke-MgGraphRequest -Uri "https://graph.microsoft.com/beta/deviceManagement/deviceConfigurations" | Get-MGGraphAllPages
#>

[CmdletBinding()]
param (
[Parameter(ValueFromPipeline)]$GraphResults
)
$uri = $null
$QueryResults = @()
do {
if($uri){$GraphResults = Invoke-MgGraphRequest -uri "$uri"}
if ($GraphResults.value) {
$QueryResults += $GraphResults.value
}
else {
$QueryResults += $GraphResults
}
$uri = $GraphResults.'@odata.nextlink'
} until (!($uri))

#Check for null Value
if(($QueryResults.count -eq 2) -and ([string]::IsNullOrEmpty($QueryResults.value)) -and ($QueryResults.'@odata.context' -match "https://graph.microsoft.com/")) {
$QueryResults = $null
}

return $QueryResults

}
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ function Invoke-IntuneBackupAppProtectionPolicy {
[string]$ApiVersion = "Beta"
)

#Connect to MS-Graph if required
if ($null -eq (Get-MgContext)) {
connect-mggraph -scopes "DeviceManagementApps.ReadWrite.All, DeviceManagementConfiguration.ReadWrite.All, DeviceManagementServiceConfig.ReadWrite.All, DeviceManagementManagedDevices.ReadWrite.All"
}

# Set the Microsoft Graph API endpoint
if (-not ((Get-MSGraphEnvironment).SchemaVersion -eq $apiVersion)) {
Update-MSGraphEnvironment -SchemaVersion $apiVersion -Quiet
Connect-MSGraph -ForceNonInteractive -Quiet
if (-not ((Get-MgProfile).name -eq $apiVersion)) {
Select-MgProfile -Name "beta"
}

# Create folder if not exists
Expand All @@ -35,9 +39,20 @@ function Invoke-IntuneBackupAppProtectionPolicy {
}

# Get all App Protection Policies
$appProtectionPolicies = Get-IntuneAppProtectionPolicy | Get-MSGraphAllPages
$appProtectionPolicies = Invoke-MgGraphRequest -Uri "/$ApiVersion/deviceAppManagement/managedAppPolicies" | Get-MgGraphAllPages

foreach ($appProtectionPolicy in $appProtectionPolicies) {

if (($appProtectionPolicy.AppGroupType -eq "selectedPublicApps") -and ($appProtectionPolicy.'@odata.type' -eq '#microsoft.graph.androidManagedAppProtection')) {
$uri = "$ApiVersion/deviceAppManagement/androidManagedAppProtections('$($appProtectionPolicy.id)')"+'?$expand=apps'
$appProtectionPolicy.apps = (Invoke-MgGraphRequest -method get -Uri $uri).apps
}

if (($appProtectionPolicy.AppGroupType -eq "selectedPublicApps") -and ($appProtectionPolicy.'@odata.type' -eq '#microsoft.graph.iosManagedAppProtection')) {
$uri = "$ApiVersion/deviceAppManagement/iosManagedAppProtections('$($appProtectionPolicy.id)')"+'?$expand=apps'
$appProtectionPolicy.add("apps",(Invoke-MgGraphRequest -method get -Uri $uri).apps)
}

$fileName = ($appProtectionPolicy.displayName).Split([IO.Path]::GetInvalidFileNameChars()) -join '_'
$appProtectionPolicy | ConvertTo-Json -Depth 100 | Out-File -LiteralPath "$path\App Protection Policies\$fileName.json"

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,19 +23,22 @@ function Invoke-IntuneBackupAppProtectionPolicyAssignment {
[string]$ApiVersion = "Beta"
)

#Connect to MS-Graph if required
if ($null -eq (Get-MgContext)) {
connect-mggraph -scopes "DeviceManagementApps.ReadWrite.All, DeviceManagementConfiguration.ReadWrite.All, DeviceManagementServiceConfig.ReadWrite.All, DeviceManagementManagedDevices.ReadWrite.All"
}

# Set the Microsoft Graph API endpoint
if (-not ((Get-MSGraphEnvironment).SchemaVersion -eq $apiVersion)) {
Update-MSGraphEnvironment -SchemaVersion $apiVersion -Quiet
Connect-MSGraph -ForceNonInteractive -Quiet
if (-not ((Get-MgProfile).name -eq $apiVersion)) {
Select-MgProfile -Name "beta"
}

# Create folder if not exists
if (-not (Test-Path "$Path\App Protection Policies\Assignments")) {
$null = New-Item -Path "$Path\App Protection Policies\Assignments" -ItemType Directory
}

# Get all assignments from all policies
$appProtectionPolicies = Get-IntuneAppProtectionPolicy | Get-MSGraphAllPages
$appProtectionPolicies = Invoke-MgGraphRequest -Uri "/$ApiVersion/deviceAppManagement/managedAppPolicies" | Get-MgGraphAllPages

foreach ($appProtectionPolicy in $appProtectionPolicies) {
switch ($appProtectionPolicy.'@odata.type') {
Expand Down Expand Up @@ -63,7 +66,7 @@ function Invoke-IntuneBackupAppProtectionPolicyAssignment {
continue
}
}
$assignments = Invoke-MSGraphRequest -HttpMethod GET -Url "deviceAppManagement/$dataType('$($appProtectionPolicy.id)')/assignments"
$assignments = Invoke-MgGraphRequest -Uri "deviceAppManagement/$dataType('$($appProtectionPolicy.id)')/assignments"

$fileName = ($appProtectionPolicy.displayName).Split([IO.Path]::GetInvalidFileNameChars()) -join '_'
$assignments | ConvertTo-Json -Depth 100 | Out-File -LiteralPath "$path\App Protection Policies\Assignments\$($appProtectionPolicy.id) - $fileName.json"
Expand Down
17 changes: 11 additions & 6 deletions IntuneBackupAndRestore/Public/Invoke-IntuneBackupClientApp.ps1
Original file line number Diff line number Diff line change
Expand Up @@ -22,11 +22,15 @@ function Invoke-IntuneBackupClientApp {
[ValidateSet("v1.0", "Beta")]
[string]$ApiVersion = "Beta"
)

#Connect to MS-Graph if required
if($null -eq (Get-MgContext)){
connect-mggraph -scopes "DeviceManagementApps.ReadWrite.All, DeviceManagementConfiguration.ReadWrite.All, DeviceManagementServiceConfig.ReadWrite.All, DeviceManagementManagedDevices.ReadWrite.All"
}

# Set the Microsoft Graph API endpoint
if (-not ((Get-MSGraphEnvironment).SchemaVersion -eq $apiVersion)) {
Update-MSGraphEnvironment -SchemaVersion $apiVersion -Quiet
Connect-MSGraph -ForceNonInteractive -Quiet
if (-not ((Get-MgProfile).name -eq $apiVersion)) {
Select-MgProfile -Name "beta"
}

# Create folder if not exists
Expand All @@ -35,14 +39,15 @@ function Invoke-IntuneBackupClientApp {
}

# Get all Client Apps
$clientApps = Invoke-MSGraphRequest -Url 'deviceAppManagement/mobileApps?$filter=(microsoft.graph.managedApp/appAvailability%20eq%20null%20or%20microsoft.graph.managedApp/appAvailability%20eq%20%27lineOfBusiness%27%20or%20isAssigned%20eq%20true)' | Get-MSGraphAllPages
$filter = "microsoft.graph.managedApp/appAvailability eq null or microsoft.graph.managedApp/appAvailability eq 'lineOfBusiness' or isAssigned eq true"
$clientApps = Invoke-MgRestMethod -Uri "$apiversion/deviceAppManagement/mobileApps?filter=$filter" | Get-MgGraphAllPages

foreach ($clientApp in $clientApps) {
$clientAppType = $clientApp.'@odata.type'.split('.')[-1]

$fileName = ($clientApp.displayName).Split([IO.Path]::GetInvalidFileNameChars()) -join '_'
$clientAppDetails = Invoke-MSGraphRequest -HttpMethod GET -Url "deviceAppManagement/mobileApps/$($clientApp.id)"
$clientAppDetails | ConvertTo-Json | Out-File -LiteralPath "$path\Client Apps\$($clientAppType)_$($fileName).json"
$clientAppDetails = Invoke-MgRestMethod -Uri "$apiversion/deviceAppManagement/mobileApps/$($clientApp.id)"
$clientAppDetails | ConvertTo-Json -depth 3 | Out-File -LiteralPath "$path\Client Apps\$($clientAppType)_$($fileName).json"

[PSCustomObject]@{
"Action" = "Backup"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,28 @@ function Invoke-IntuneBackupClientAppAssignment {
[string]$ApiVersion = "Beta"
)

#Connect to MS-Graph if required
if($null -eq (Get-MgContext)){
connect-mggraph -scopes "DeviceManagementApps.ReadWrite.All, DeviceManagementConfiguration.ReadWrite.All, DeviceManagementServiceConfig.ReadWrite.All, DeviceManagementManagedDevices.ReadWrite.All"
}

# Set the Microsoft Graph API endpoint
if (-not ((Get-MSGraphEnvironment).SchemaVersion -eq $apiVersion)) {
Update-MSGraphEnvironment -SchemaVersion $apiVersion -Quiet
Connect-MSGraph -ForceNonInteractive -Quiet
if (-not ((Get-MgProfile).name -eq $apiVersion)) {
Select-MgProfile -Name "beta"
}


# Create folder if not exists
if (-not (Test-Path "$Path\Client Apps\Assignments")) {
$null = New-Item -Path "$Path\Client Apps\Assignments" -ItemType Directory
}

# Get all assignments from all policies
$clientApps = Invoke-MSGraphRequest -Url 'deviceAppManagement/mobileApps?$filter=(microsoft.graph.managedApp/appAvailability%20eq%20null%20or%20microsoft.graph.managedApp/appAvailability%20eq%20%27lineOfBusiness%27%20or%20isAssigned%20eq%20true)' | Get-MSGraphAllPages
# Get all Client Apps
$filter = "microsoft.graph.managedApp/appAvailability eq null or microsoft.graph.managedApp/appAvailability eq 'lineOfBusiness' or isAssigned eq true"
$clientApps = Invoke-MgRestMethod -Uri "$apiversion/deviceAppManagement/mobileApps?filter=$filter" | Get-MgGraphAllPages

foreach ($clientApp in $clientApps) {
$assignments = Get-DeviceAppManagement_MobileApps_Assignments -MobileAppId $clientApp.id
$assignments = (Invoke-MgRestMethod -Uri "/$apiversion/deviceAppManagement/mobileApps/$($clientApp.id)/assignments").value
if ($assignments) {
$fileName = ($clientApp.displayName).Split([IO.Path]::GetInvalidFileNameChars()) -join '_'
$assignments | ConvertTo-Json -Depth 100 | Out-File -LiteralPath "$path\Client Apps\Assignments\$($clientApp.id) - $fileName.json"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ function Invoke-IntuneBackupConfigurationPolicy {
[string]$ApiVersion = "Beta"
)

#Connect to MS-Graph if required
if($null -eq (Get-MgContext)){
connect-mggraph -scopes "DeviceManagementApps.ReadWrite.All, DeviceManagementConfiguration.ReadWrite.All, DeviceManagementServiceConfig.ReadWrite.All, DeviceManagementManagedDevices.ReadWrite.All"
}

# Set the Microsoft Graph API endpoint
if (-not ((Get-MSGraphEnvironment).SchemaVersion -eq $apiVersion)) {
Update-MSGraphEnvironment -SchemaVersion $apiVersion -Quiet
Connect-MSGraph -ForceNonInteractive -Quiet
if (-not ((Get-MgProfile).name -eq $apiVersion)) {
Select-MgProfile -Name "beta"
}

# Create folder if not exists
Expand All @@ -35,11 +39,11 @@ function Invoke-IntuneBackupConfigurationPolicy {
}

# Get all Setting Catalogs Policies
$configurationPolicies = Invoke-MSGraphRequest -HttpMethod GET -Url "deviceManagement/configurationPolicies" | Get-MSGraphAllPages
$configurationPolicies = Invoke-MgGraphRequest -Uri "$ApiVersion/deviceManagement/configurationPolicies" | Get-MGGraphAllPages

foreach ($configurationPolicy in $configurationPolicies) {
$configurationPolicy | Add-Member -MemberType NoteProperty -Name 'settings' -Value @() -Force
$settings = Invoke-MSGraphRequest -HttpMethod GET -Url "deviceManagement/configurationPolicies/$($configurationPolicy.id)/settings" | Get-MSGraphAllPages
$settings = (Invoke-MgGraphRequest -Uri "$ApiVersion/deviceManagement/configurationPolicies/$($configurationPolicy.id)/settings").value

if ($settings -isnot [System.Array]) {
$configurationPolicy.Settings = @($settings)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ function Invoke-IntuneBackupConfigurationPolicyAssignment {
[string]$ApiVersion = "Beta"
)

#Connect to MS-Graph if required
if($null -eq (Get-MgContext)){
connect-mggraph -scopes "DeviceManagementApps.ReadWrite.All, DeviceManagementConfiguration.ReadWrite.All, DeviceManagementServiceConfig.ReadWrite.All, DeviceManagementManagedDevices.ReadWrite.All"
}

# Set the Microsoft Graph API endpoint
if (-not ((Get-MSGraphEnvironment).SchemaVersion -eq $apiVersion)) {
Update-MSGraphEnvironment -SchemaVersion $apiVersion -Quiet
Connect-MSGraph -ForceNonInteractive -Quiet
if (-not ((Get-MgProfile).name -eq $apiVersion)) {
Select-MgProfile -Name "beta"
}

# Create folder if not exists
Expand All @@ -35,11 +39,10 @@ function Invoke-IntuneBackupConfigurationPolicyAssignment {
}

# Get all assignments from all policies
$configurationPolicies = Invoke-MSGraphRequest -HttpMethod GET -Url "deviceManagement/configurationPolicies" | Get-MSGraphAllPages
$configurationPolicies = (Invoke-MgGraphRequest -Uri "$ApiVersion/deviceManagement/configurationPolicies").value

foreach ($configurationPolicy in $configurationPolicies) {
$assignments = Invoke-MSGraphRequest -HttpMethod GET -Url "deviceManagement/configurationPolicies/$($configurationPolicy.id)/assignments" | Get-MSGraphAllPages

$assignments = (Invoke-MgGraphRequest -Uri "$ApiVersion/deviceManagement/configurationPolicies/$($configurationPolicy.id)/assignments").value
if ($assignments) {
$fileName = ($configurationPolicy.name).Split([IO.Path]::GetInvalidFileNameChars()) -join '_'
$assignments | ConvertTo-Json | Out-File -LiteralPath "$path\Settings Catalog\Assignments\$fileName.json"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ function Invoke-IntuneBackupDeviceCompliancePolicy {
[string]$ApiVersion = "Beta"
)

#Connect to MS-Graph if required
if($null -eq (Get-MgContext)){
connect-mggraph -scopes "DeviceManagementApps.ReadWrite.All, DeviceManagementConfiguration.ReadWrite.All, DeviceManagementServiceConfig.ReadWrite.All"
}

# Set the Microsoft Graph API endpoint
if (-not ((Get-MSGraphEnvironment).SchemaVersion -eq $apiVersion)) {
Update-MSGraphEnvironment -SchemaVersion $apiVersion -Quiet
Connect-MSGraph -ForceNonInteractive -Quiet
if (-not ((Get-MgProfile).name -eq $apiVersion)) {
Select-MgProfile -Name "beta"
}

# Create folder if not exists
Expand All @@ -35,8 +39,7 @@ function Invoke-IntuneBackupDeviceCompliancePolicy {
}

# Get all Device Compliance Policies
$deviceCompliancePolicies = Get-DeviceManagement_DeviceCompliancePolicies | Get-MSGraphAllPages

$deviceCompliancePolicies = Invoke-MgGraphRequest -Uri "$ApiVersion/deviceManagement/deviceCompliancePolicies" | Get-MGGraphAllPages
foreach ($deviceCompliancePolicy in $deviceCompliancePolicies) {
$fileName = ($deviceCompliancePolicy.displayName).Split([IO.Path]::GetInvalidFileNameChars()) -join '_'
$deviceCompliancePolicy | ConvertTo-Json -Depth 100 | Out-File -LiteralPath "$path\Device Compliance Policies\$fileName.json"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,22 +23,26 @@ function Invoke-IntuneBackupDeviceCompliancePolicyAssignment {
[string]$ApiVersion = "Beta"
)

#Connect to MS-Graph if required
if($null -eq (Get-MgContext)){
connect-mggraph -scopes "DeviceManagementApps.ReadWrite.All, DeviceManagementConfiguration.ReadWrite.All, DeviceManagementServiceConfig.ReadWrite.All"
}

# Set the Microsoft Graph API endpoint
if (-not ((Get-MSGraphEnvironment).SchemaVersion -eq $apiVersion)) {
Update-MSGraphEnvironment -SchemaVersion $apiVersion -Quiet
Connect-MSGraph -ForceNonInteractive -Quiet
if (-not ((Get-MgProfile).name -eq $apiVersion)) {
Select-MgProfile -Name "beta"
}

# Create folder if not exists
if (-not (Test-Path "$Path\Device Compliance Policies\Assignments")) {
$null = New-Item -Path "$Path\Device Compliance Policies\Assignments" -ItemType Directory
}

# Get all assignments from all policies
$deviceCompliancePolicies = Get-DeviceManagement_DeviceCompliancePolicies | Get-MSGraphAllPages
# Get all Device Compliance Policies
$deviceCompliancePolicies = Invoke-MgGraphRequest -Uri "$ApiVersion/deviceManagement/deviceCompliancePolicies" | Get-MGGraphAllPages

foreach ($deviceCompliancePolicy in $deviceCompliancePolicies) {
$assignments = Get-DeviceManagement_DeviceCompliancePolicies_Assignments -DeviceCompliancePolicyId $deviceCompliancePolicy.id
$assignments = Invoke-MgGraphRequest -Uri "$ApiVersion/deviceManagement/deviceCompliancePolicies/$($deviceCompliancePolicy.id)/assignments" | Get-MGGraphAllPages
if ($assignments) {
$fileName = ($deviceCompliancePolicy.displayName).Split([IO.Path]::GetInvalidFileNameChars()) -join '_'
$assignments | ConvertTo-Json | Out-File -LiteralPath "$path\Device Compliance Policies\Assignments\$fileName.json"
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,14 @@ function Invoke-IntuneBackupDeviceConfiguration {
[string]$ApiVersion = "Beta"
)

#Connect to MS-Graph if required
if($null -eq (Get-MgContext)){
connect-mggraph -scopes "DeviceManagementApps.ReadWrite.All, DeviceManagementConfiguration.ReadWrite.All, DeviceManagementServiceConfig.ReadWrite.All, DeviceManagementManagedDevices.ReadWrite.All"
}

# Set the Microsoft Graph API endpoint
if (-not ((Get-MSGraphEnvironment).SchemaVersion -eq $apiVersion)) {
Update-MSGraphEnvironment -SchemaVersion $apiVersion -Quiet
Connect-MSGraph -ForceNonInteractive -Quiet
if (-not ((Get-MgProfile).name -eq $apiVersion)) {
Select-MgProfile -Name "beta"
}

# Create folder if not exists
Expand All @@ -35,7 +39,7 @@ function Invoke-IntuneBackupDeviceConfiguration {
}

# Get all device configurations
$deviceConfigurations = Get-DeviceManagement_DeviceConfigurations | Get-MSGraphAllPages
$deviceConfigurations = Invoke-MgGraphRequest -Uri "$apiVersion/deviceManagement/deviceConfigurations" | Get-MGGraphAllPages


foreach ($deviceConfiguration in $deviceConfigurations) {
Expand All @@ -48,7 +52,7 @@ function Invoke-IntuneBackupDeviceConfiguration {
foreach ($omaSetting in $deviceConfiguration.omaSettings) {
# Check if this particular setting is encrypted, and get the plaintext only if necessary
if ($omaSetting.isEncrypted) {
$omaSettingValue = Invoke-MSGraphRequest -HttpMethod GET -Url "deviceManagement/deviceConfigurations/$($deviceConfiguration.id)/getOmaSettingPlainTextValue(secretReferenceValueId='$($omaSetting.secretReferenceValueId)')" | Get-MSGraphAllPages
$omaSettingValue = Invoke-MgGraphRequest -Uri "$apiVersion/deviceManagement/deviceConfigurations/$($deviceConfiguration.id)/getOmaSettingPlainTextValue(secretReferenceValueId='$($omaSetting.secretReferenceValueId)')" | Get-MgGraphAllPages
} else {
$omaSettingValue
}
Expand Down
Loading

0 comments on commit f73a3ca

Please sign in to comment.