diff --git a/IntuneBackupAndRestore/Public/Invoke-IntuneBackupAssignmentFilter.ps1 b/IntuneBackupAndRestore/Public/Invoke-IntuneBackupAssignmentFilter.ps1 new file mode 100644 index 0000000..4be4ede --- /dev/null +++ b/IntuneBackupAndRestore/Public/Invoke-IntuneBackupAssignmentFilter.ps1 @@ -0,0 +1,51 @@ +function Invoke-IntuneBackupAssignmentFilter { + <# + .SYNOPSIS + Backup Intune Assignment Filter + + .DESCRIPTION + Backup Intune Assignment filters as JSON files per assignment filter to the specified Path. + + .PARAMETER Path + Path to store backup files + + .EXAMPLE + Invoke-IntuneBackupAssignmentFilter -Path "C:\temp" + #> + + [CmdletBinding()] + param( + [Parameter(Mandatory = $true)] + [string]$Path, + + [Parameter(Mandatory = $false)] + [ValidateSet("v1.0", "Beta")] + [string]$ApiVersion = "Beta" + ) + + # Set the Microsoft Graph API endpoint + if (-not ((Get-MSGraphEnvironment).SchemaVersion -eq $apiVersion)) { + Update-MSGraphEnvironment -SchemaVersion $apiVersion -Quiet + Connect-MSGraph -ForceNonInteractive -Quiet + } + + # Create folder if not exists + if (-not (Test-Path "$Path\Assignment Filters")) { + $null = New-Item -Path "$Path\Assignment Filters" -ItemType Directory + } + + # Get all App Protection Policies + $assignmentFilters = Invoke-MSGraphRequest -HttpMethod GET -Url "/deviceManagement/assignmentFilters"| Get-MSGraphAllPages + + foreach ($assignmentFilter in $assignmentFilters) { + $fileName = ($assignmentFilter.displayName).Split([IO.Path]::GetInvalidFileNameChars()) -join '_' + $assignmentFilter | ConvertTo-Json -Depth 100 | Out-File -LiteralPath "$path\Assignment Filters\$fileName.json" + + [PSCustomObject]@{ + "Action" = "Backup" + "Type" = "Assignment Filter" + "Name" = $assignmentFilter.displayName + "Path" = "Assignment Filters\$fileName.json" + } + } +} diff --git a/IntuneBackupAndRestore/Public/Invoke-IntuneRestoreAssignmentFilter.ps1 b/IntuneBackupAndRestore/Public/Invoke-IntuneRestoreAssignmentFilter.ps1 new file mode 100644 index 0000000..813b104 --- /dev/null +++ b/IntuneBackupAndRestore/Public/Invoke-IntuneRestoreAssignmentFilter.ps1 @@ -0,0 +1,71 @@ +function Invoke-IntuneRestoreAssignmentFilter { + <# + .SYNOPSIS + Restore Intune Assignment Filters + + .DESCRIPTION + Restore Intune Assignment Filters from JSON files per Assignment Filter Policy from the specified Path. + + .PARAMETER Path + Root path where backup files are located, created with the Invoke-IntuneBackupAssignmentFilter function + + .EXAMPLE + Invoke-IntuneRestoreAssignmentFilter -Path "C:\temp" -RestoreById $true + #> + + [CmdletBinding()] + param( + [Parameter(Mandatory = $true)] + [string]$Path, + + [Parameter(Mandatory = $false)] + [ValidateSet("v1.0", "Beta")] + [string]$ApiVersion = "Beta" + ) + + # Set the Microsoft Graph API endpoint + if (-not ((Get-MSGraphEnvironment).SchemaVersion -eq $apiVersion)) { + Update-MSGraphEnvironment -SchemaVersion $apiVersion -Quiet + Connect-MSGraph -ForceNonInteractive -Quiet + } + + # Get all Assignment Filters + $AssignmentFilters = Get-ChildItem -Path "$path\Assignment Filters" -File + + foreach ($AssignmentFilter in $AssignmentFilters) { + $AssignmentFilterContent = Get-Content -LiteralPath $AssignmentFilter.FullName -Raw + $AssignmentFilterDisplayName = ($AssignmentFilterContent | ConvertFrom-Json).displayName + + # Remove properties that are not available for creating a new configuration + $requestBodyObject = $AssignmentFilterContent | ConvertFrom-Json + # Set SupportsScopeTags to $false, because $true currently returns an HTTP Status 400 Bad Request error. + #if ($requestBodyObject.supportsScopeTags) { + # $requestBodyObject.supportsScopeTags = $false + #} + + $requestBodyObject.PSObject.Properties | Foreach-Object { + if ($null -ne $_.Value) { + if ($_.Value.GetType().Name -eq "DateTime") { + $_.Value = (Get-Date -Date $_.Value -Format s) + "Z" + } + } + } + + $requestBody = $requestBodyObject | Select-Object -Property * -ExcludeProperty id, deviceEnrollmentConfigurationId, createdDateTime, lastModifiedDateTime, version | ConvertTo-Json -Depth 100 + + # Restore the Assignment Filter + try { + $null = Invoke-MSGraphRequest -HttpMethod POST -Content $requestBody.toString() -Url "deviceManagement/AssignmentFilters" -ErrorAction Stop + [PSCustomObject]@{ + "Action" = "Restore" + "Type" = "Assignment Filter" + "Name" = $AssignmentFilterDisplayName + "Path" = "Assignment Filters\$($AssignmentFilter.Name)" + } + } + catch { + Write-Verbose "$AssignmentFilterDisplayName - Failed to restore Assignment Filter" -Verbose + Write-Error $_ -ErrorAction Continue + } + } +} \ No newline at end of file