Public/Invoke-IntuneRestoreDeviceManagementIntentSP.ps1
function Invoke-IntuneRestoreDeviceManagementIntentSP { <# .SYNOPSIS Restore Intune Device Management Intents .DESCRIPTION Restore Intune Device Management Intents from JSON files per Device Management Intent from the specified Path. .PARAMETER Path Root path where backup files are located, created with the Invoke-IntuneBackupDeviceManagementIntent function .EXAMPLE Invoke-IntuneRestoreDeviceManagementIntent -Path "C:\temp" -RestoreById $true #> [CmdletBinding()] param( [Parameter(Mandatory = $true)] [string]$Path, [Parameter(Mandatory = $false)] [ValidateSet("v1.0", "Beta")] [string]$ApiVersion = "Beta" ) # Ensure the Microsoft Graph module is installed and imported if (-not (Get-Module -Name Microsoft.Graph -ListAvailable)) { Install-Module -Name Microsoft.Graph -Scope CurrentUser -Force } Import-Module Microsoft.Graph.DeviceManagement # Connect to Microsoft Graph if not already connected if (-not (Get-MgUser -UserId me -ErrorAction SilentlyContinue)) { Connect-MgGraph -Scopes "DeviceManagementApps.Read.All","DeviceManagementApps.ReadWrite.All","DeviceManagementConfiguration.Read.All","DeviceManagementConfiguration.ReadWrite.All","DeviceManagementServiceConfig.Read.All","DeviceManagementServiceConfig.ReadWrite.All" } # Function to get all pages of results function Get-AllPages { param ( [Parameter(Mandatory = $true)] [string]$Uri ) $results = @() $response = Invoke-MgGraphRequest -Method GET -Uri $Uri $results += $response.value while ($null -ne $response.'@odata.nextLink') { $response = Invoke-MgGraphRequest -Method GET -Uri $response.'@odata.nextLink' $results += $response.value } return $results } # Get all device management intents $deviceManagementIntents = Get-ChildItem -Path "$Path\Device Management Intents" -Recurse -File foreach ($deviceManagementIntent in $deviceManagementIntents) { $deviceManagementIntentContent = Get-Content -LiteralPath $deviceManagementIntent.FullName -Raw $deviceManagementIntentDisplayName = ($deviceManagementIntentContent | ConvertFrom-Json).displayName $templateId = $deviceManagementIntent.Name.Split("_")[0] $templateDisplayName = ($deviceManagementIntent).DirectoryName.Split('\')[-1] # Restore the device management intent try { $null = Invoke-MGGraphRequest -Method POST -Uri "$apiVersion/deviceManagement/templates/$($templateId)/createInstance" -Body $deviceManagementIntentContent.toString() -ErrorAction Stop [PSCustomObject]@{ "Action" = "Restore" "Type" = "Device Management Intent" "Name" = $deviceManagementIntentDisplayName "Path" = "Device Management Intents\$($deviceManagementIntent.Name)" } } catch { Write-Verbose "$deviceManagementIntentDisplayName - Failed to restore Device Management Intent ($templateDisplayName)" -Verbose Write-Error $_ -ErrorAction Continue } } } |