I’m attempting to run a backup of an API Management service. I can do this in PS from my local machine without issue using the code below under my sub owner login context.
When I attempt to run it as a runbook, I only get back:
Suspended
The runbook job was attempted 3 times, but it failed each time. Common reasons that runbook jobs fail can be found here: https://docs.microsoft.com/en-us/azure/automation/automation-troubleshooting-automation-errors
There are no errors, only the above.
I’ve tried to run under user and system assigned identity, without any difference in outcome.
$method="UA"
$apiManagementName="xxxx-Prod";
$apiManagementResourceGroup="xxxx-Prod";
$storageAccountName=xxxxstorage";
$storageResourceGroup="xxxations";
$containerName="xxxxbackups";
$blobName="prodnpapim.apimbackup"
$date = (Get-Date).ToString("yyyyMMdd")
$outputBlobName = "$($date)$($blobName)"
$identityName = "xxxx-OPS-MID";
$identityResourceGroup = "xxxxtions";
try
{
"Logging in to Azure..."
Connect-AzAccount -Identity
}
catch {
Write-Error -Message $_.Exception
throw $_.Exception
}
$storageKey = (Get-AzStorageAccountKey -ResourceGroupName $storageResourceGroup -StorageAccountName $storageAccountName)[0].Value
$storageContext = New-AzStorageContext -StorageAccountName $storageAccountName -StorageAccountKey $storageKey
"Starting backup"
try{
Backup-AzApiManagement -ResourceGroupName $apiManagementResourceGroup -Name $apiManagementName `
-StorageContext $storageContext -TargetContainerName $containerName -TargetBlobName $outputBlobName
}
catch{
"Backup-AzApiManagement error"
exit
}
Without any errors, I’m stuck – given it works fine in local PS.
Where am I going wrong?
2
Answers
I 'fixed' the problem.
I had to perform the following steps.
Import the modules I needed.
Disable Context Save
Set Context
This is using system-managed identity
You are missing an opening
"
where you definestorageAccountName
Not sure if that’s a syntax error pasting to SO or if that would be the same within your actual runbook.
If it fails after that still then you would have to use more Write-Output statements and verbose logging in your runbook job to identify where the problem is between each step.