azurepowershellazure-automationpowershell-cmdlet

$DebugPreference = "Continue" not working when running a powersehll runbook in azure automation account's test pane


Earlier when I used to use command $DebugPreference="Continue" within the powershell script, this used to print the output with all the debugging lines. Now I am not able to see debug results in the test pane. And this is required to check where exactlty the powershell code failing and to know the exact error.

Import-Module Az.Accounts 
Import-Module Az.Resources 
Import-Module Az.Compute 
Import-Module Az.Automation 
Import-Module Az.Storage 
Import-Module Az.KeyVault 
Import-Module Az.RecoveryServices  
$DebugPreference = 'continue' 
Connect-AzAccount -Identity  
$context = Get-AzContext -ListAvailable write-output "context" $context Set-AzContext -Subscription "<<subscriptionID>>" 
$vaultName = "" 
$vaultResourcegroup = ""  
$containerCtx = Get-AzRecoveryServicesVault -Name $vaultName -ResourceGroupName $vaultResourcegroup   
write-output "containerCtx" $containerCtx

Reference: https://learn.microsoft.com/en-us/troubleshoot/azure/general/capture-debug-stream-automation-runbook


Solution

  • As clearly given in MSDoc of configuring automation runbook output and message streams, debug logs will not be displayed in the test pane instead all the logs can be clearly seen after you publish the runbook.

    Note: But $VerbosePreference="continue"functionality will display all the output verbose information in the test pane.

    After checking the above doc, I tried executing your code in my automation PowerShell runbook (7.1) environment, published my runbook and then started executing a job as shown below.

    enter image description here

    enter image description here

    $GLOBAL:DebugPreference="Continue"
    Connect-AzAccount -Identity
    $vaultName = "newvmjvault"
    $vaultResourcegroup = "xxxx"
    $containerCtx = Get-AzRecoveryServicesVault -Name $vaultName -ResourceGroupName $vaultResourcegroup
    write-output "containerCtx"  $containerCtx
    

    Debug logs are displayed after publishing a runbook:

    enter image description here