skip to Main Content

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


  1. Chosen as BEST ANSWER

    I 'fixed' the problem.

    I had to perform the following steps.

    Import the modules I needed.

    Import-Module Az.Accounts
    Import-Module Az.ApiManagement
    Import-Module Az.Storage
    

    Disable Context Save

    Disable-AzContextAutosave -Scope Process
    

    Set Context

    $AzureContext = (Connect-AzAccount -Identity).context
    
    $AzureContext = Set-AzContext -SubscriptionName $AzureContext.Subscription -DefaultProfile $AzureContext
    

    This is using system-managed identity


  2. You are missing an opening " where you define storageAccountName

    $storageAccountName=xxxxstorage"
    

    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.

    Login or Signup to reply.
Please signup or login to give your own answer.
Back To Top
Search