次の方法で共有


Using Verbose messages in Azure Automation Runbooks

While developing PowerShell scripts I like to add Verbose messages to my scripts. If you want to find more information about Write-Verbose check this TechNet webpage.

But how can use Write-Verbose messages in your Azure Automation Runbooks? There are two ways to have Verbose messages being shown when running your Azure Automation Runbooks:

  1. Change the default setting of the $VerbosePreference build-in variable from ‘SilentlyContinue’ to ‘Continue’
    image
  2. Change the Logging and Tracing settings from the Azure Automation Runbook for Log verbose records from Off to On.
    image

 

Let’s now see how this works. First let us change the $VerbosePreference variable. I have configured the $VerbosePreference to ‘Continue’ in my stsHelloWorld runbook.

image

Let’s test this using the (Azure Automation PowerShell ISE Add-On) https://github.com/azureautomation/azure-automation-ise-addon.

image

Here you see the verbose messages being shown in the Test pane of the Azure Automation PowerShell ISE Add-On..

But how do we use the second method of showing the verbose messages without changing the $VerbosePreference setting?

I use the following PowerShell script to enable and disable the verbose logging setting in Azure Automation Runbooks.

Code Snippet

  1. #requires -Version 3 -Modules AzureRM.Automation, AzureRM.profile, AzureRM.Resources
  2.  
  3. # ---------------------------------------------------
  4. # Script: C:\Users\stefstr\OneDrive - Microsoft\Scripts\PS\NN\RunbookAutomation.ps1
  5. # Version: 0.1
  6. # Author: Stefan Stranger
  7. # Date: 06/10/2016 13:01:29
  8. # Description: Script to test Runbooks
  9. # Comments:
  10. # Changes:  
  11. # Disclaimer:
  12. # This example is provided “AS IS” with no warranty expressed or implied. Run at your own risk.
  13. # **Always test in your lab first**  Do this at your own risk!!
  14. # The author will not be held responsible for any damage you incur when making these changes!
  15. # ---------------------------------------------------
  16.  
  17. #Login to Azure
  18. Add-AzureRmAccount
  19.  
  20. #Select Azure Subscription
  21. $subscriptionId =
  22. (Get-AzureRmSubscription |
  23.     Out-GridView `
  24.     -Title 'Select an Azure Subscription ...' `
  25. -PassThru).SubscriptionId
  26.  
  27. Set-AzureRmContext -SubscriptionId $subscriptionId
  28.  
  29. #Select ResourceGroup
  30. $ResourceGroup = Get-AzureRmResourceGroup | Out-GridView -PassThru
  31.  
  32.  
  33. #Select AutomationAccount
  34. $AutomationAccount = Get-AzureRmAutomationAccount -ResourceGroupName $ResourceGroup.ResourceGroupName | Out-GridView -PassThru
  35.  
  36. #Retrieve Runbooks
  37. $runbook = Get-AzureRmAutomationRunbook -ResourceGroupName $ResourceGroup.ResourceGroupName -AutomationAccountName $AutomationAccount.AutomationAccountName |
  38. Out-GridView -Title 'Select Runbook' -PassThru
  39.  
  40.  
  41. #Show Runbook info
  42. $runbook
  43.  
  44.  
  45. #Enable Verbose logging
  46. $runbook |
  47.     Set-AzureRmAutomationRunbook -LogVerbose $true
  48.  
  49.  
  50. #Start Runbook
  51. $runbook |
  52.     Start-AzureRmAutomationRunbook -Wait
  53.  
  54.  
  55. #Check status Runbook
  56. $Result = (Get-AzureRMAutomationJob -ResourceGroupName $ResourceGroup.ResourceGroupName -AutomationAccountName $AutomationAccount.AutomationAccountName)[0]
  57. $Result
  58.  
  59. #Get All Output
  60. Get-AzureRmAutomationJob -id $Result.JobId $ResourceGroup.ResourceGroupName -AutomationAccountName $AutomationAccount.AutomationAccountName |
  61.         Get-AzureRmAutomationJobOutput |
  62.             Select-Object -Property Summary, Type, Time
  63.  
  64. #Disable Verbose Output
  65. $runbook | Set-AzureRmAutomationRunbook -LogVerbose $false

Here is how it works in action:

AAVerboseLogging

I prefer the last option but it’s up to you.

P.S. I changed roles within Microsoft I’m now Consultant for the Data Platform Smile