When we create/Import an Azure Automation Runbook in PowerShell, and if the Runbook takes an array as a parameter; when we execute the Runbook, passing in a comma-separated list of array values in the Start Runbook UI within the Azure Portal throws an error like below:
Unable to cast object of type 'System.String' to type 'System.Object'
Let’s take for example an Azure Automation Runbook named “Show-VMDetails.ps1” shown in the screenshot below, which takes a Resource Group name and an array of VM Names (within the specified Resource Group) as parameters. It then iterates through all the VM names and displays the name, location, and size of each VM:
As you can see from the code that the Azure Automation Runbook has the $VMNames parameter typed as a string array. This means that we are allowing multiple VM names to be passed to this Runbook, upon which the intended operations will happen.
If this were a normal PowerShell script being executed from the PowerShell console, we would simply have to pass the array values as a comma-separated list as shown below:
.\Show-VMDetails.ps1 – ResourceGroupName rg01 -VMNames vm01,vm02,vm03
However, when we execute an Azure Automation Runbook, we are asked for manually providing the Runbook parameter values in the Start Runbook UI as shown in the screenshot below:
If we try to pass the array values (for $VMNames parameter here) to the Azure Automation’s Start Runbook UI in the form of comma-separated values, just like we passed them in the PowerShell console based example above, we will encounter the error as shown in the next two screenshots below:
This error is thrown because for any array parameter, Azure Automation does not recognize the values passed in the form of a simple comma separated string.
We need to pass the array values as a JSON format string. In this case, it would be
[‘vm01’,vm02’,’vm03’] and is shown in the screenshot below:
You can see that passing the value of the $VMNames array parameter as JSON format string resolved the error for us, and we are able to successfully execute the script as intended.
When I first encountered this Issue some time back, I could not immediately understand why this was happening. There was no clarity given about this behavior/Issue either in the Azure Automation documentation, or anywhere else I looked on the web.
However, I was soon able to find detailed guidance about defining the Runbook Input parameters supported by Azure Automation from an old Microsoft blog post from 2014. You can refer to that post here: Azure Automation: Runbook Input, Output, and Nested Runbooks
Hope you found this blog post useful. If you have any queries/feedback, please feel free to mention in the comments section below.