Public/Invoke-IntuneBackupDeviceHealthScriptSP.ps1

function Invoke-IntuneBackupDeviceHealthScriptSP {
    <#
    .SYNOPSIS
    Backup Intune Health Scripts (Remediation scripts)
 
    .DESCRIPTION
    Backup Intune Health Scripts (Remediation scripts) as JSON files per Health Script to the specified Path.
 
    .PARAMETER Path
    Path to store backup files
 
    .EXAMPLE
    Invoke-IntuneBackupDeviceHealthScript -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\Device Health Scripts")) {
        $null = New-Item -Path "$Path\Device Health Scripts" -ItemType Directory
    }

    # Get all Health Scripts
    $healthScripts = (Invoke-MGGraphRequest -method GET -Uri "https://graph.microsoft.com/$ApiVersion/deviceManagement/deviceHealthScripts").value 

    foreach ($healthScript in $healthScripts) {
        $fileName = ($healthScript.displayName).Split([IO.Path]::GetInvalidFileNameChars()) -join '_'

        # Export the Health script profile
        $healthScript | ConvertTo-Json -Depth 100 | Out-File -LiteralPath "$path\Device Health Scripts\$fileName.json"

        # Create folder if not exists
        if (-not (Test-Path "$Path\Device Health Scripts\Script Content")) {
            $null = New-Item -Path "$Path\Device Health Scripts\Script Content" -ItemType Directory
        }

        $healthScriptObject = Invoke-MGGraphRequest -Method GET -Uri "https://graph.microsoft.com/$ApiVersion/deviceManagement/deviceHealthScripts/$($healthScript.id)"
        $healthScriptDetectionContent = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($healthScriptObject.detectionScriptContent))
        $healthScriptDetectionContent | Out-File -LiteralPath "$path\Device Health Scripts\Script Content\$fileName`_detection.ps1"
        $healthScriptRemediationContent = [System.Text.Encoding]::UTF8.GetString([System.Convert]::FromBase64String($healthScriptObject.remediationScriptContent))
        $healthScriptRemediationContent | Out-File -LiteralPath "$path\Device Health Scripts\Script Content\$fileName`_remediation.ps1"

        [PSCustomObject]@{
            "Action" = "Backup"
            "Type"   = "Device Health Scripts"
            "Name"   = $healthScript.displayName
            "Path"   = "Device Health Scripts\$fileName.json"
        }
    }
}