Passing a Switch Parameter Value In an Azure Automation Runbook

AzureAutomation
 
Facebooktwittergoogle_pluslinkedinFacebooktwittergoogle_pluslinkedin

Let’s say you have an Azure Automation Runbook, which has a PowerShell Switch type parameter. Now when you execute this Runbook or schedule it for a frequency-based execution, you may need to provide values for one or more Parameters defined for the Runbook. What will you provide as the value for the Switch type parameter?

You cannot pass usual values depicting true/false results viz. $true, $True, $false, $False, 1, 0, 1.0 and the likes because you will get an error as shown below.

Failed
Cannot process argument transformation on parameter ‘DefaultBase’. Cannot convert value “System.String” to type “System.Management.Automation.SwitchParameter”.
Boolean parameters accept only Boolean values and numbers, such as $True, $False, 1 or 0. (Cannot convert value “System.String” to type “System.Management.Automation.SwitchParameter”.
Boolean parameters accept only Boolean values and numbers, such as $True, $False, 1 or 0. (Cannot convert value “System.String” to type “System.Management.Automation.SwitchParameter”.
Boolean parameters accept only Boolean values and numbers, such as $True, $False, 1 or 0.)

When faced with this problem, I searched everywhere on the Web and found literally nothing workable. This Information was unclear even on the most authoritative blog post on the Innards of Azure Automation published by Chris Sanders in August 2014 here: Azure Automation: Runbook Input, Output, and Nested Runbooks

Almost all other links where this particular problem was referenced, only alternative best-practices advice on using a PowerShell bool type was advocated to be used instead of Switch type for the parameter, rather than clearly tell what should be passed as value for the Switch parameter if we were absolutely adamant to keep the same.

I then discovered on my own (the hard way) that we need to simply provide the values true or false (Is case sensitive – won’t work with True or False) without any decorations like double quotes, single quotes, square brackets, parentheses, braces, JSON format string etc..

You can see the successful execution of a sample Azure Automation Runbook with a single Switch type parameter named $DefaultBase in the screenshot below. Look at the true value I passed for the Switch type parameter and the sample Output.

Executing an Azure Runbook with true value for Switch parameter

 

Similarly, see the screenshot when I pass the value false for the Switch type parameter.

Executing an Azure Runbook with false value for Switch parameter

Hope you found this blog post useful. If you have any queries/feedback, please feel free to mention in the comments section below.

Facebooktwittergoogle_pluslinkedinFacebooktwittergoogle_pluslinkedin

Be the first to comment

Leave a Reply

Your email address will not be published.


*