First published on TECHNET on Apr 28, 2016
Update 10/12/2016:
This feature is available generally in System Center 2016
In
part 1
of this series, we announced the support for PowerShell script runbooks in System Center Service Management Automation (SMA) 2016 and looked at creating and running PowerShell script runbooks. In this part, we will look at using SMA assets and creating nested PowerShell script runbooks.
Note:
In the examples shown in this post, SMA web service endpoint is assumed to be “https://TAPVMWS” and the working folder is assumed to be “C:\SMA”.
Using SMA Assets
PowerShell script runbooks can use SMA assets (like variables and credentials). Let’s see how to use SMA variables in a PowerShell script runbook. First, create a SMA variable ‘NumberOfIterations’ with value 2 and another variable ‘SampleMessage’ with value “Hello”. Use the following commands to create the two variables
Set-SmaVariable -Name NumberOfIterations -value 2 -WebServiceEndpoint https://TAPVMWS -AuthenticationType Windows
Set-SmaVariable -Name SampleMessage -value “Hello” -WebServiceEndpoint https://TAPVMWS -AuthenticationType Windows
Once the variables are created, use Get-AutomationVariable to retrieve the variable value in a PowerShell script. Following script makes use of the above created SMA variables. Follow the steps shown in
part 1
of this series to create a runbook named ‘SamplePsScriptRunbook_UsingVariables’ with the following content:
[int] $NumberOfIterations = Get-AutomationVariable -Name 'NumberOfIterations'
[string] $SampleMessage = Get-AutomationVariable -Name 'SampleMessage'
# Prints out 'SampleMessage' 'NumberOfIterations' times.
for ($i = 1; $i -le $NumberOfIterations; $i++) {
Write-Output "$i`: $SampleMessage"
}
Use the commands shown below to start the runbook and see its output:
Start-SmaRunbook -WebServiceEndpoint https://TAPVMWS -AuthenticationType Windows -Name SamplePsScriptRunbook_UsingVariables
Get-SmaJob -WebServiceEndpoint https://TAPVMWS -AuthenticationType Windows -Id <Job ID>
Get-SmaJobOutput -Id <Job ID> -Stream Any -WebServiceEndpoint https://TAPVMWS -AuthenticationType Windows
Using Nested Runbooks
PowerShell script runbooks support nesting of runbooks. One PowerShell script runbook can call another published PowerShell script runbook. Use the following syntax in caller PowerShell script runbook to call another published PowerShell script runbook:
.\<called runbook name>.ps1
Follow the steps shown
part 1
of this series to create and publish a runbook named “SamplePsScriptRunbook_Parent” with the following content:
.\ SamplePsScriptRunbook_GetDate.ps1
This new runbook will invoke the “SamplePsScriptRunbook_GetDate” runbook, which was created and published in
part 1
of this series.
Import-SmaRunbook -Path C:\sma\SamplePsScriptRunbook_Parent.ps1 -WebServiceEndpoint https://TAPVMWS -AuthenticationType Windows
Publish-SmaRunbook -WebServiceEndpoint https://TAPVMWS -AuthenticationType Windows -Name SamplePsScriptRunbook_Parent
Use the following commands to start the runbook and see its output:
Start-SmaRunbook -WebServiceEndpoint https://TAPVMWS -AuthenticationType Windows -Name SamplePsScriptRunbook_Parent
Get-SmaJob -WebServiceEndpoint https://TAPVMWS -AuthenticationType Windows -Id <Job ID>
Get-SmaJobOutput -Id <Job ID> -Stream Any -WebServiceEndpoint https://TAPVMWS -AuthenticationType Windows
You can read about all System Center 2016 TP5 features from
here
and can download Service Management Automation bits from
download page
.
Please feel free to use the comments section below for any queries or to share your feedback. We look forward to hearing from you.