16 July 2026

Intune - documentation and backup. Part 5

 Automation Account


Next step is to create an Automation Account.
I will use the following in the creation:

Configuration

Value

Resource group

RG-BBJ-IntuneMgmt

Automation account name

AA-BBJ-IntuneMgmt

Region

West Europe

System assigned

Selected

Connectivity configuration

Public access

Go to Process Automation, select Runbooks and click “Create a runbook”.





When created, we land in “Edit PowerShell Runbook”.

Now copy this script into Runbook:

# IntuneManagement 
# Intune Backup and Documentation
# Version 1.0
# Customer settings
$RGname = "RG-BBJ-IntuneMgmt"
$AutoAcc = "AA-BBJ-IntuneMgmt"
$StorAcc = "sabbjintunemgmt"

$TenantId = "94801ede-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
$AppId = "61b18e96-xxxx-xxxx-xxxx-xxxxxxxxxxxx"
$TheSecret = "xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx"
$DocJson = "BulkDocumentation.json"
$BUJson = "BulkExport.json"

# Don't change below this point....
# Share names
$shareIntune    = "IntuneManagement"   # I:
$shareDocs      = "documentation"      # D:
$shareBackup    = "backup"             # B:

# Drive letters
$driveIntune = "I:"
$driveDocs   = "D:"
$driveBackup = "B:"

# Local paths (no date subfolders locally as requested)
$localRoot        = "C:\Intune\IntuneManagement"
$localToolDir     = Join-Path $localRoot "IntuneManagement"
$localDocFolder   = Join-Path $localRoot "Documentation"
$localBackupDir   = Join-Path $localRoot "Backup"

# ===== Helper functions =====
function Log([string]$msg) {
    $t = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
    Write-Output "$t  $msg"
}

function MapDrive($drive$unc) {
    Log "Mapping $drive -> $unc"
    $cmd = "net use $drive `"$unc`" /user:Azure\$storAcc `"$SAKey`""
    cmd.exe /c $cmd > $null 2>&1
    Start-Sleep -Seconds 1
    if (-not (Test-Path "$drive\")) { throw "Failed to map $drive to $unc" }
    Log "Mapped $drive"
}

function UnmapDrive($drive) {
    Log "Unmapping $drive"
    cmd.exe /c "net use $drive /delete /y" > $null 2>&1
    Start-Sleep -Seconds 1
    Log "Unmapped $drive"
}

function EnsureDir($path) {
    if (-not (Test-Path $path)) {
        New-Item -Path $path -ItemType Directory -Force | Out-Null
        Log "Created folder: $path"
    }
}

# Ensures you do not inherit an AzContext in your runbook
Disable-AzContextAutosave -Scope Process

# Connect to Azure with system-assigned managed identity
$AzureContext = (Connect-AzAccount -identity).context

# set and store context
$AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription -DefaultProfile $AzureContext

# access file share
$SAkey = (Get-AzStorageAccountKey -resourcegroupname $RGname -name $StorAcc)[0].value
$context = New-AzStorageContext -StorageAccountName $StorAcc -StorageAccountKey $SAkey

# ===== Start =====
Log "Run start - creating local structure and dummy files"

try {
    # Build UNC paths
    $uncIntune = "\\$StorAcc.file.core.windows.net\$shareIntune"
    $uncDocs   = "\\$StorAcc.file.core.windows.net\$shareDocs"
    $uncBackup = "\\$StorAcc.file.core.windows.net\$shareBackup"

    # Map drives
    MapDrive $driveIntune $uncIntune
    MapDrive $driveDocs $uncDocs
    MapDrive $driveBackup $uncBackup

    # Ensure local directories exist
    EnsureDir $localRoot
    EnsureDir $localToolDir
    EnsureDir $localDocFolder
    EnsureDir $localBackupDir

    # Copy IntuneManagement to local folder
    Log "Copying content from $driveIntune to $localToolDir"
    Copy-Item -Path "$driveIntune\*" -Destination $localToolDir -Recurse -Force -ErrorAction SilentlyContinue
    Log "Copy from I: completed"

    cd $localToolDir
    Log "Starting Documentation..." 
    Start-Process powershell -ArgumentList ".\Start-IntuneManagement.ps1 -silent -tenantId $TenantId -appid $AppId -secret $TheSecret -SilentBatchFile $DocJson" -wait    
    Log "End of Documentation..." 

    Log "Starting BackUp..." 
    Start-Process powershell -ArgumentList ".\Start-IntuneManagement.ps1 -silent -tenantId $TenantId -appid $AppId -secret $TheSecret -SilentBatchFile $BUJson" -wait
    Log "End of Backup..." 

    # Copy documentation file back to D:
    Log "Copying documentation file to $driveDocs"
    Copy-Item -Path (Join-Path $localDocFolder "*") -Destination $driveDocs -Force -ErrorAction Stop
    Log "Documentation file copied to share D:"

    # Copy entire local Backup folder to B:
    Log "Copying local backup folder to $driveBackup"
    Copy-Item -Path (Join-Path $localBackupDir "*") -Destination $driveBackup -Recurse -Force -ErrorAction Stop
    Log "Backup folder copied to share B:"

    # Local cleanup: remove local Tool, Documentation and Backup contents
    Log "Cleaning up local folders"
    try {
        Remove-Item -Path $localToolDir -Recurse -Force -ErrorAction SilentlyContinue
        Remove-Item -Path $localDocFolder -Recurse -Force -ErrorAction SilentlyContinue
        Remove-Item -Path $localBackupDir -Recurse -Force -ErrorAction SilentlyContinue
        Log "Local cleanup completed"
    } catch {
        Log "Local cleanup error: $($_.Exception.Message)"
    }

catch {
    Log ("ERROR: " + $_.Exception.Message)
finally {
    # Unmap drives
    try { UnmapDrive $driveDocs } catch { Log ("Unmap D: failed: " + $_.Exception.Message) }
    try { UnmapDrive $driveBackup } catch { Log ("Unmap B: failed: " + $_.Exception.Message) }
    try { UnmapDrive $driveIntune } catch { Log ("Unmap I: failed: " + $_.Exception.Message) }
    Log "Run completed"
}

In the script, there is a customer settings part (line 4 to 13). Settings here, should be changed according to you environment:

Variable

Description

My Values

$Rgname

Resource group

RG-BBJ-IntuneMgmt

$AutoAcc

Automation Account

AA-BBJ-IntuneMgmt

$StorAcc

Storage Accunt

sabbjintunemgmt

$TenantId

Tenant ID


$AppId    

Application (client) ID


$Secret

Client secret


$DocJson

File for running documentation (located on IntuneManagement share)

BulkDocumentation.json

$BUJson

Json file for running backup (located on IntuneManagement share)

BulkExport.json

You can do a test-run by choosing “Test pane”.

When the script runs without any errors, press “Publish”.

Now we will create a schedule to run this job automatically. Go to “Shared Resources”  > “Schedules” and hit “+ Add a schedule”.



Fill out with schedule name, time and recurrence, then hit create.


Now we have automized our backup and documentation of Intune.

As it is now, we will fill up space with backups and documentation files. We could do some clean-up....
In next part I will take the script and put in some house-cleaning.

No comments:

Post a Comment