This article outlines tips for writing runbooks in Service Management Automation and Azure Automation.
Most of these tips apply to both services. If the tip doesn’t apply to both, the tip will explicitly call out SMA or Azure Automation. When you see the term Automation used, the tip refers to both. These tips do not go into detail about why we suggest you follow this guidance. For more detail on PowerShell Workflow and runbook, look at our runbook authoring documentation and PowerShell Workflow introduction guide.
Also check out this PowerShell Workflow Gotchas wiki that highlights some of the most frequently problems that SMA and Azure Automation users run into.
DO
CONSIDER
AVOID
DO NOT
Prefix your runbooks with an explanatory comment block following MSFT guidance
Make sure to include the following labels:
Example: $VerbosePreference = [System.Management.Automation.ActionPreference]$Using:VerbosePreference $ErrorActionPreference = [System.Management.Automation.ActionPreference]$Using:ErrorActionPreference
$VerbosePreference = [System.Management.Automation.ActionPreference]$Using:VerbosePreference
$ErrorActionPreference = [System.Management.Automation.ActionPreference]$Using:ErrorActionPreference
Write-Error -Message $_.Message -ErrorAction Continue
$JobOutput = Get-SmaJobOutput -Id $JobId -WebServiceEndpoint $WebServiceEndpoint OR $JobOutput = Get-AzureAutomationJob –Id $JobId –Account $MyAccount
OR
$JobOutput = Get-AzureAutomationJob –Id $JobId –Account $MyAccount
Beth, suggestion to put the emphasis on the >content< not on the do's and don't keywords themselves,
Sorry, added content back in again, while editing. (Pretty interesting stuff!) Feel free to kick the content again...
Thanks Peter! The new formatting does help highlight the material. Appreciate the updates!
Hey guys -- great stuff. Thanks for the effort. It makes it much easier to catch up to best practices when learning. ;-)
Thanks for article. It is useful. However, I didn't get one point. Why one should not use 'switch parameters'?[Do NOT use switch parameters on PowerShell workflows (use a Boolean parameter type instead]. Do you mind giving details?
Regards.
Ohh, is it because switch parameter is not primitive type (couldn't edit my previous comment)? I thought as long as "complex type" objects are not very large, just used as pure data (or, on other words just contains data properties) which can serialized/de-serialized it should be ok (as long as one doesn't expect to invoke methods on complex types) - or am I missing something?
Regards..
We put switch as a "do not" because there are syntax differences in PS Workflow which leads to user error. Perhaps this is a little strong and we should move this to "Avoid" as switch statements will still work. For more info, Jim wrote a good blog explaining how to use switch statements in SMA: www.powershellmagazine.com/.../using-powershell-switch-vs-boolean-parameters-in-sma-runbooks. PowerShell's docs also have a little blurb on this: technet.microsoft.com/.../dn151046.aspx
Also, you are right about complex objects. You can use the properties as these can be serialized/deserialized. This is probably the biggest point of confusion for our users as they move to PS Workflow from PowerShell which is why we warn users about using the pipeline with complex objects.
We've found that even when leveraging the above considerations we have a considerable compilation issue. We use SMA to automate various responses to SCOM alerts.
What I dont understand: Why does SMA need to recompile the same runbook everytime it executes? If the runbook hasnt been edited since the last time it has run it should simply re-execute unless it doesnt not exist ont he worker that is running it.
That being said, since we are living in a world where we have to compile each time we run the same runbook, why is the compilation directory not configurable? It would make sense to give a setup option to point to a compilation volume such as a RAM drive or something with high IO capabilities. We've resorted to creating a symbolic link fromt he programdata folder to a higher IO drive for the sandboxes working directory as well as moving the temp/temp folders for the service account that stores the xml files post compilation.
The root issue is the grossly inefficient method in which compiling. Nothing should be redone unless something has changes or it is the first time the runbook is executing on the worker.