Public/Invoke-IntuneRestoreClientAppsSP.ps1

function Invoke-IntuneRestoreClientAppsSP {
    <#
    .SYNOPSIS
    Restore Intune Client Apps
     
    .DESCRIPTION
    Restore Intune Client Apps from JSON files per Client App from the specified Path.
     
    .PARAMETER Path
    Root path where backup files are located, created with the Invoke-IntuneBackupConfigurationPolicy function
     
    .EXAMPLE
    Invoke-IntuneRestoreClientApps -Path "C:\temp"
    #>

    
    [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 Client Apps
    $clientApps = Get-ChildItem -Path "$Path\Client Apps" -File

    foreach ($clientapp in $clientApps) {
        $clientappContent = Get-Content -LiteralPath $clientapp.FullName -Raw | ConvertFrom-Json
        
        # Remove properties that are not available for creating a new configuration
        $requestBody = $clientappContent | Select-Object -Property * -ExcludeProperty "@odata.context", uploadState, publishingState, isAssigned, dependentAppCount, supersedingAppCount, supersededAppCount, id, createdDateTime, lastModifiedDateTime, settingCount, creationSource | ConvertTo-Json -Depth 100

        # Restore the Settings Catalog Policy
        try {
            $null = Invoke-MGGraphRequest -Method POST -Body $requestBody.toString() -Uri "$apiVersion/deviceAppManagement/mobileApps" -ErrorAction Stop
            [PSCustomObject]@{
                "Action" = "Restore"
                "Type"   = "Client App"
                "Name"   = $clientapp.FullName
                "Path"   = "Client Apps\$($clientapp.Name)"
            }
        }
        catch {
            Write-Verbose "$($clientapp.FullName) - Failed to restore Client App" -Verbose
            Write-Error $_ -ErrorAction Continue
            $streamReader = [System.IO.StreamReader]::new($_.Exception.Response.GetResponseStream())
        $ErrResp = $streamReader.ReadToEnd() | ConvertFrom-Json
        $streamReader.Close()
        }
        $ErrResp
    }
}