Public/Invoke-IntuneRestoreDeviceConfigurationSP.ps1
function Invoke-IntuneRestoreDeviceConfigurationSP { <# .SYNOPSIS Restore Intune Device Configurations .DESCRIPTION Restore Intune Device Configurations from JSON files per Device Configuration Policy from the specified Path. .PARAMETER Path Root path where backup files are located, created with the Invoke-IntuneBackupDeviceConfigurations function .EXAMPLE Invoke-IntuneRestoreDeviceConfiguration -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 configurations $deviceConfigurations = Get-ChildItem -Path "$path\Device Configurations" -File foreach ($deviceConfiguration in $deviceConfigurations) { $deviceConfigurationContent = Get-Content -LiteralPath $deviceConfiguration.FullName -Raw $deviceConfigurationDisplayName = ($deviceConfigurationContent | ConvertFrom-Json).displayName # Remove properties that are not available for creating a new configuration $requestBodyObject = $deviceConfigurationContent | 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, createdDateTime, lastModifiedDateTime, version | ConvertTo-Json -Depth 100 # Restore the device configuration try { $null = Invoke-MGGraphRequest -Method POST -Body $requestBody.toString() -Uri "$apiVersion/deviceManagement/deviceConfigurations" -ErrorAction Stop [PSCustomObject]@{ "Action" = "Restore" "Type" = "Device Configuration" "Name" = $deviceConfigurationDisplayName "Path" = "Device Configurations\$($deviceConfiguration.Name)" } } catch { Write-Verbose "$deviceConfigurationDisplayName - Failed to restore Device Configuration" -Verbose Write-Error $_ -ErrorAction Continue } } } |