Automation account runbook error The given key was not present in the dictionary

Rory_Feng 66 Reputation points
2021-10-24T11:26:36.123+00:00

Dear

when I setup the schedule to run runbooks there was an execution happened.

warning: Unable to set default context 'Microsoft.Azure.Commands.Common.Authentication.Abstractions.AzureContext'.
error: System.Collections.Generic.KeyNotFoundException: The given key was not present in the dictionary. at System.Collections.Concurrent.ConcurrentDictionary2.get_Item(TKey key) at Microsoft.Azure.Commands.Common.Authentication.AzureSession.c_DisplayClass82_01. UnregisterComponent b_0() at Microsoft.Azure.Commands.Profile.ConnectAzureRmAccountCommand.EndProcessing() at System.Management.Automation.CommandProcessorBase.Complete() + CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException + FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException

My runbook source is here:


param(
[Parameter(Mandatory = $true)]
[string] $ResourceGroupName,

[Parameter(Mandatory = $true)]
[string] $VirtualMachineName

)

Disable-AzContextAutosave -Scope Process

try{

$msg = ""
# check the servers    

# connect to Automation
$connectionName = 'AzureRunAsConnection'

$conn = Get-AutomationConnection -Name $connectionName
if (!$conn)
{
    $ErrorMessage = "Connection $connectionName not found."
    throw $ErrorMessage
}

Connect-AzAccount -ServicePrincipal -Tenant $conn.TenantID `
-ApplicationId $conn.ApplicationID -CertificateThumbprint $conn.CertificateThumbprint

$GetAcsessToken = Get-AzAccessToken 
$auth = $GetAcsessToken.token
$authHeader = @{
    'Content-Type'='application/json'
    'Accept' = 'application/json'
    'Authorization'="Bearer $auth"
}

$restUri = 'https://management.azure.com/subscriptions/xxxxxxx/resourceGroups/' + $ResourceGroupName + '/providers/Microsoft.Compute/virtualMachines/' + $VirtualMachineName + '/runCommand?api-version=2019-03-01'


$a_body = @{
    'commandId' = 'RunShellScript'
    'script' = @( 'rsh -l pqp00001 PQXARD01 /app/PQ/scripts/PQBSTXP1.SH')
}

$time = (Get-Date).AddHours(9).ToString("yyyy-MM-dd HH:mm:ss")
Write-Output "$time----Start---- Begin to Start xECM Tomcat Service of VM:$VirtualMachineName ----"

# Rest API
$response = Invoke-webrequest -Uri $restUri -Method post -Headers $authHeader -Body $($a_body | ConvertTo-Json) -UseBasicParsing
$asyncstatus = $($response.headers.'Azure-AsyncOperation')
$status = 'InProgress'

While($status -eq 'InProgress')
{
    Start-Sleep -s 5
    $response = invoke-webrequest -uri $asyncstatus -Headers $authHeader -UseBasicParsing
    $status = $($response.Content | ConvertFrom-Json).status
    Write-Output "Status : $status"
}
# # if Rest API request is "succeeded" then continue to check the process
if($status -eq "succeeded")
{
    #Output the result
    $time = (Get-Date).AddHours(9).ToString("yyyy-MM-dd HH:mm:ss")
    Write-Output $($response.Content | ConvertFrom-Json).properties.output.value[0].message

    Write-Output "$time----End----VM:$VirtualMachineName Start xECM Tomcat Service Successfully----"


#  if Rest API request is not "succeeded" output error
}else
{
    $time = (Get-Date).AddHours(9).ToString("yyyy-MM-dd HH:mm:ss")
    Write-Output "$time----End---- VM:$VirtualMachineName Start xECM Tomcat Service is not succeeded----"
    $msg = $time + $status + " " + $VirtualMachineName + " " + "Start xECM Tomcat Service is not succeeded"
    Write-Error "Error:$msg"
    throw ($msg)
}

}

catch{

Exception Error

Write-Error -Message $.Exception
throw ('Error : ' + $
.Exception.Message)
}


I made many runbooks with the same source for every VM of Azure portal in a automation account .
Then I setup the schedule 0:00 to start to run these runbooks everyday.
The issue was happened .

I did not run the runbooks in my local PC just use automation account.
It were run by the schedule in automation account.

It happens about 3 times once week.

sorry I do not know which command is the cause to this issue in my runbook.

I think I did not use AzureRM command in this runbook. I checked the information in Automation account with command Get-AzcontextAutosaveSetting through runbook.

I found The contextFile was AzureRmtext.json

The result of Get-AzcontextAutosaveSetting command runbook

Mode : CurrentUser

ContextDirectory : C:\Users\Client.Azure

ContextFile : AzureRmContext.json

CacheDirectory : C:\Users\Client\AppData\Local.IdentityService

CacheFile : msal.cache

Settings : {}

Is it the problem of file AzureRmtext.json. Is it file of AzureRM module. How to solve this problem.

Sorry to ask another question. I already asked Azure support japan but they do not give a good solution.

Hope could see your replay soon.
Thank you so much.

Best regards.

Azure Automation
Azure Automation
An Azure service that is used to automate, configure, and install updates across hybrid environments.
1,251 questions
{count} votes

Accepted answer
  1. AnuragSingh-MSFT 21,381 Reputation points
    2021-10-28T14:29:14.243+00:00

    Hi @Rory_Feng ,

    Welcome to Microsoft Q&A! Thanks for posting the question.

    Apologies for the delayed response as I had been trying a few things locally. While I am not sure of the exact cause of the issue, here are 3 suggestions that should help resolve it or at least get more details on the error.

    1. Wrap your 'Connect-AzAccount` in a retry logic for transient network failures, something like below. (Since you do not see the failure consistently, it seems to be a transient issue which resolves on its own after a few seconds. Adding retry logic would help overcome such condition):

    # Wrap authentication in retry logic for transient network failures  
        [System.Int32]$LogonAttempt = 0  
        [System.Int32]$LogonAttemptMax = 3  
        [System.Boolean]$Connected = $false  
        while (($false -eq $Connected) -or ($LogonAttemptMax -le $LogonAttempt))   
        {  
            # Logging in to Azure...  
            $ConnectionResult = Connect-AzAccount `  
                -ServicePrincipal `  
                -Tenant $Connection.TenantID `  
                -ApplicationId $Connection.ApplicationID `  
                -CertificateThumbprint $Connection.CertificateThumbprint `  
                -ErrorAction "SilentlyContinue"  
            if ($ConnectionResult)   
            {  
                Write-Verbose -Message "Connected to Azure."  
                [System.Boolean]$Connected = $true  
            }  
           elseif ($LogonAttemptMax -le $LogonAttempt)   
           {  
                Write-Warning -Message "Not connected to Azure yet. Trying again in 10 seconds. Logon attempt: '$LogonAttempt' of: '$LogonAttemptMax'."  
                Start-Sleep -Seconds 10  
                $LogonAttempt++  
            }  
            else {  
                Write-Error -Message "An error ocurred while trying to connect to Azure after: '$LogonAttemptMax' attempts."  
           
                #exit runbook because we were not able to connect.  
                exit 1  
            }  
        }  
    

    2. To get the exact location where the error is happening, you can use the snippet below in catch{} block:

    catch  
    {  
    	write-error $_.Exception.Message  
      
    	#The scriptStackTrace would contains the line number for faulting code  
    	write-error $_.ScriptStackTrace    
    	throw ($_)  
    }  
    

    3. I see you are starting the runbook at 0:00. Can you please change it to something like 0:05 AM. This is to avoid ambiguous DateTime from schedule, just to be on the safer side.

    Please let me know if you have any questions.

    ---
    Please 'Accept as answer' and ‘Upvote’ if it helped so that it can help others in the community looking for help on similar topics.


0 additional answers

Sort by: Most helpful

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.