This is a short article on how to use parameters in Azure Automation PowerShell runbooks. I recently wrote about how to create a simple PowerShell Azure Automation runbook. This article kind of follows on from that, where I used an Azure Runbook to create a simple ‘Hello World’. This was done by creating a PowerShell runbook with the following code:
write "Hello World"
Doesn’t get much simpler than that! So what if we wanted to say ‘Hello Azure’ instead? Well we could simply edit the code, but a better way would be to use a variable,which would allow us to substitute ‘World’ for the value of a defined variable. We can do this easily in a runbook by using parameters. For example:
Param
(
[Parameter (Mandatory= $true)]
[String] $Name
)
## Hello
write "Hello $Name"
Now, when we test the script we are asked for the value of the $Name variable:
If you wish, you can also add a default value, which would be used if no specific value is entered instead. This is achieved by assigning a default value to the variable in the parameter block:
Param
(
[Parameter (Mandatory= $true)]
[String] $Name = "World"
)
## Hello
write "Hello $Name"
This was a simple example, but hopefully it gives you an idea of how you can use runbook parameters to make your Azure runbooks more flexible and scaleable. For example, you could write a runbook to power off a group of virtual machines in a given resource group, using the resource group name as a parameter, therefore allowing you to target difference resource groups easily with the same runbook. Or how about a runbook to power on all VMs that have a tag value that matches your input parameter? Endless possibilities! You can read the Microsoft Azure documentation on Azure runbook parameters here.