Public/Invoke-IntuneBackupConfigurationPolicySP.ps1

function Invoke-IntuneBackupConfigurationPolicySP {
    <#
    .SYNOPSIS
    Backup Intune Settings Catalog Policies
     
    .DESCRIPTION
    Backup Intune Settings Catalog Policies as JSON files per Settings Catalog Policy to the specified Path.
     
    .PARAMETER Path
    Path to store backup files
     
    .EXAMPLE
    Invoke-IntuneBackupConfigurationPolicy -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
}

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

    # Get all Setting Catalogs Policies
    $configurationPolicies = Get-AllPages -Uri "$ApiVersion/deviceManagement/configurationPolicies"

    foreach ($configurationPolicy in $configurationPolicies) {
        $configurationPolicy | Add-Member -MemberType NoteProperty -Name 'settings' -Value @() -Force
        $settings = Get-AllPages -Uri "$apiVersion/deviceManagement/configurationPolicies/$($configurationPolicy.id)/settings"

        if ($settings -isnot [System.Array]) {
            $configurationPolicy.Settings = @($settings)
        } else {
            $configurationPolicy.Settings = $settings
        }
        
        $fileName = ($configurationPolicy.name).Split([IO.Path]::GetInvalidFileNameChars()) -join '_'
        $configurationPolicy | ConvertTo-Json -Depth 100 | Out-File -LiteralPath "$path\Settings Catalog\$fileName.json"

        [PSCustomObject]@{
            "Action" = "Backup"
            "Type"   = "Settings Catalog"
            "Name"   = $configurationPolicy.name
            "Path"   = "Settings Catalog\$fileName.json"
        }
    }
}