You might want to do some housekeeping of the backup.
Replace the script in the runbook with this script.
Here, you will have the option to define how old your backup can be.
Just edit the number of days in $days.
# IntuneManagement
# Intune Backup and Documentation
# Version 2.0
# With Clean-up
###### 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"
# how many days to keep
$days = 90
####### 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"
$now = get-date
$lastWrite = $now.AddDays(-$days)
# ===== 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)"
}
# Storage Clean-up
Log "Cleaning up Storage folders"
try {
Get-ChildItem -Path $driveDocs -Recurse -File | Where CreationTime -LT $lastWrite | Remove-Item -Force -ErrorAction SilentlyContinue
Get-ChildItem -Path $driveBackup -Recurse | where CreationTime -LT $lastWrite | Remove-Item -Recurse -Force -ErrorAction SilentlyContinue
Log "Storage cleanup completed"
} catch {
Log "Storage 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"
}