Friday, 2 June 2017

Service Management Automation: Migrating Simple (single .net activity) Orchestrator runbook to SMA

Hello Readers,
Service Management Automation is an IT process automation solution for Windows Azure Pack for Windows Server. It enables you to automate the creation, monitoring, and deployment of resources in your Windows Azure Pack environment.
This blog is meant for IT teams who are trying to migrate their traditional data center Orchestrator runbook to SMA. I am targeting simple runbooks here which contains single .Net activity (the PowerShell script inside the .net script activity can be complex though πŸ™‚ ), something like shown below.
clip_image003
Before jumping into code details of Orchestrator runbook and SMA runbook, Please make sure you have a working Orchestrator and SMA environment. Below are the main requirements for this scenario
1. Prepare SMA environment
You can install SMA using this article on TechNet. There is also another very simple article present on internet which gives step by step process of installing SMA.
2. System Center Orchestrator 2012 RTM or above
Follow this link to prepare Orchestrator environment.
3. Windows Azure Pack
Follow this link for more details of WAP.
4. System Center Virtual Machine Manager 2012 RTM or above.
Follow this link to prepare SCVMM environment. We need VMM as we are using sample Orchestrator runbook which changes the memory or CPU count of Virtual machines using VMM.

For SMA to Orchestrator integration i.e. invoking a SMA runbook from Orchestrator and Orchestrator runbook from SMA, please refer this article written by Chris Sanders.
We will achieve below in this blog
1. Creating a sample Orchestrator Runbook. (ChangeVM)
2. Step by step process of converting Orchestrator Runbook to SMA (Invoke-ChangeVM)
You can use your already created runbooks also for step 1. You can directly jump to Step 2 to see the conversion steps J
These are the requirements for this scenario:
– VMM Server and SMA Server should be in same domain.
– VMM server must be reachable from SMA server (Connectivity). Please follow below steps to test.
· Ping the VMM Server and confirm it is working. Please refer below image.
· Create PSSession of VMM Server and confirm it is working. Please refer below image.
– ExecutionPolicy should be set as ‘RemoteSigned’ on both SMA and VMM server.
– The VM needs to be in either Power Off or Stored State mode before SCVMM can perform the specified hardware changes.
– Given Memory value in ChangeVM runbook should be in increments of 2MB. Odd values are not accepted while assigning Memory.
clip_image005

 

Step 1:- Creating Sample Orchestrator Runbook (ChangeVM)

You can follow this free ebook written by Andreas Rynes on Designing Orchestrator runbooks. This ebook provides a framework for designing Runbooks and IT process automation to help the IT Pro get the most out of their System Center 2012 Orchestrator implementation and help them to utilize Orchestrator in a very modular approach, focusing on small, focused pieces of automation.
Let’s focus on the runbook we are using as an example in this blog:-
‘Change VM’ Orchestrator Runbook is responsible for changing configuration of an existing VM (Memory). The runbook accepts below parameters:-
1. $VMAction – Action to be performed on existing VM e.g. IncreaseMemory, DecreaseMemory.
2. $VMValue – Value of Memory to be increased or decreased, VMValue should be in increments of 2MB
3. $VMMServerName – Name of the SCVMM Server.
4. $VMName – Name of the VM that needs to be configured.
This runbook creates remote session to SCVMM server and uses Set-VM cmdlet to set the memory. The values of VMName, VMMServerName, VMAction and VMValue will be read from ‘Initialize Data’ activity
The runbook script is pasted in below box:-
   1: # Get Input parameters
   2: $VMName="\`d.T.~Ed/{3128930E-D53C-4129-BBA2-16EBC4A724D4}.{A389F8BA-E2E4-465E-9470-00F85094E07C}\`d.T.~Ed/"
   3: $VMMServerName="\`d.T.~Ed/{3128930E-D53C-4129-BBA2-16EBC4A724D4}.{D7D3C839-97AE-4940-88C7-09FE4C3212BB}\`d.T.~Ed/"
   4: $VMAction="\`d.T.~Ed/{3128930E-D53C-4129-BBA2-16EBC4A724D4}.{CCEF9043-CE8B-4D4E-9276-85CD768C77BE}\`d.T.~Ed/"
   5: $VMValue="\`d.T.~Ed/{3128930E-D53C-4129-BBA2-16EBC4A724D4}.{05AF33B3-CE05-444A-A09D-0E2D2B7B608A}\`d.T.~Ed/"
   6:  
   7: try
   8: {
   9:     Write-Output 'Create Session to the Remote Computer';
  10:  
  11:     $Session = New-PSSession -ComputerName $VMMServerName 
  12:     $Parameters = @{
  13:             VMMServerName = $VMMServerName
  14:             VMName = $VMName
  15:             VMAction = $VMAction
  16:             VMValue = $VMValue
  17:     }
  18:     $ReturnArray = Invoke-Command -Session $Session -Argumentlist $Parameters -ScriptBlock {
  19:         Param ( $Parameters )
  20:          
  21:         try
  22:         {
  23:             Write-Output "Beginning action : Change VM `r`n" 
  24:       
  25:             Import-Module VirtualMachineManager | out-null
  26:  
  27:             $VMMServerName=$Parameters.VMMServerName
  28:             $VMName=$Parameters.VMName
  29:             $VMAction=$Parameters.VMAction
  30:             $VMValue=$Parameters.VMValue
  31:             Get-VMMServer -Computername $VMMServerName | out-null
  32:             $vm = Get-SCVirtualMachine -Name $VMName
  33:             if ($vm -eq $null)
  34:             {
  35:                 Throw "Error: VM Name is invalid "
  36:             }
  37:                 [int]$memoryMB = $vm.Memory
  38:                 $Trace += "Current Memory : '$memoryMB' `r`n" 
  39:                     
  40:                 if($VMAction -eq "IncreaseMemory")
  41:                 { 
  42:                     [int]$newMemoryMB = $memoryMB+$VMValue;
  43:                 }
  44:                 elseif($VMAction -eq "DecreaseMemory")
  45:                 {
  46:                     [int]$newMemoryMB = $memoryMB-$VMValue;
  47:                 }
  48:                 Set-VM $vm -MemoryMB $newMemoryMB | Out-Null
  49:         }
  50:         catch
  51:         {
  52:             Write-Output "Exception caught in remote Action" 
  53:             Throw $Error[0].Exception.ToString()
  54:         }  
  55:  }
  56:  Remove-PSSession -Session $Session 
  57: }
  58: catch
  59: {
  60:     Write-Output "Error changing VM"  
  61: }

 

Step 2:- Converting Orchestrator Runbook into SMA Workflow (Invoke-ChangeVM)

This section contains all details of step by step process of converting the Orchestrator Runbook ‘ChangeVM’ into SMA workflow.

High Level Steps

Following are the generalized steps that can be followed by any existing Orchestrator runbook that has to be converted into SMA workflow:-
1. Create an empty PowerShell Workflow container.
2. Add the existing PowerShell script inside Workflow container.
3. Convert the Initialize Data Parameters read by script, if any, to workflow parameters.
4. Add InlineScript activity inside the workflow after the parameters and move the code inside the InlineScript activity.
5. Reassign the parameters read in workflow inside InlineScript using ‘$Using:’ as the scope is different and parameters are not directly available.
6. If you are creating a PSSession inside the script, make sure you invoke the session with required credentials, if current SMA user is not having access to the remote machine.
7. Import the Workflow in SMA server.
8. Run the Workflow and verify the output.
Detail of each step is shown with the help of example in below section of this document.

“Converting ‘Change VM’ Orchestrator Runbook to SMA Worklfow”

This example will show the steps followed while converting the Component Runbook ‘Change VM’ to SMA Runbook ‘Invoke-ChangeVM’.
Steps followed
1. Create an empty PowerShell Workflow container named ‘Invoke-ChangeVM’ as shown in image below.
clip_image007
2. Add Orchestrator .Net Script PowerShell code inside the PowerShell workflow as shown in image below.
clip_image009
3. Convert Input Parameters from Initialize Data Activity to SMA Workflow Parameters as shown in lines 3-11 below. Two more parameters ‘UserName’ and ‘Password’ are added in this workflow which are used while creating session to VMM server.
clip_image011
4. Add the whole code written in workflow except parameters to InlineScript as shown in line 13 below.
clip_image013
The InlineScript activity runs commands in a shared Windows PowerShell session. You can include it in a workflow to run commands that share data and commands that are not otherwise valid in a workflow.
The InlineScript script block can include all valid Windows PowerShell commands and expressions. Because the commands and expressions in an InlineScript script block run in the same session, they share all state and data, including imported modules and the values of variables. More details for InlineScript can be found at http://technet.microsoft.com/en-us/library/jj649082.aspx
5. Reassign the parameter values inside InlineScript using ‘$Using:’ keyword as shown in lines 15-19 below. PowerShell workflow parameters are not available inside InlineScript scope. More details on using variables in PowerShell workflows can be read at http://technet.microsoft.com/en-us/library/jj574187.aspx
clip_image015
6. Add credentials while creating PSSession to remote SCVMM server as current logged in user in SMA server might not have access to remote VMM server as shown in lines 29-32 below. This step is optional incase the logged in user of SMA has got required access on SCVMM server.
clip_image017
7. The final SMA Workflow script will look like below
   1: workflow Invoke-ChangeVM 
   2: {    
   3:     param
   4:     (
   5:     [String]$VMAction,
   6:     [String]$VMValue,
   7:     [String]$VMMServerName,
   8:     [String]$VMName,
   9:     [String]$UserName,
  10:     [String]$Password
  11:     ) 
  12:     
  13:     InlineScript 
  14:     {    
  15:         $VMAction = $Using:VMAction
  16:         $VMValue = $Using:VMValue
  17:         $VMMServerName = $Using:VMMServerName
  18:         $VMName = $Using:VMName
  19:         $UserName = $Using:UserName
  20:         $Password = $Using:Password
  21:     
  22:         try
  23:         {
  24:             Write-Output 'Create Session to the Remote Computer';
  25:     
  26:             $pass = ConvertTo-SecureString $Password -AsPlainText -Force
  27:             $cred = New-Object System.Management.Automation.PSCredential ($UserName, $pass)
  28:             $Session = New-PSSession -ComputerName $VMMServerName -Credential $cred        
  29:             
  30:             $Parameters = @{
  31:                     VMMServerName = $VMMServerName
  32:                     VMName = $VMName
  33:                     VMAction = $VMAction
  34:                     VMValue = $VMValue
  35:             }
  36:     
  37:             $ReturnArray = Invoke-Command -Session $Session -Argumentlist $Parameters -ScriptBlock {            
  38:                 Param ( $Parameters )
  39:                 
  40:                 try
  41:                 {
  42:                     Write-Output "Beginning remote action `r`n"    
  43:                        
  44:                     Import-Module VirtualMachineManager | out-null
  45:      
  46:                     $VMMServerName=$Parameters.VMMServerName
  47:                     $VMName=$Parameters.VMName
  48:                     $VMAction=$Parameters.VMAction
  49:                     $VMValue=$Parameters.VMValue
  50:     
  51:                     Get-VMMServer -Computername $VMMServerName | out-null
  52:                     $vm = Get-SCVirtualMachine -Name $VMName
  53:     
  54:                     if ($vm -eq $null)
  55:                     {
  56:                         Throw "Error: VM Name is invalid..`r`n"
  57:                     }
  58:                     
  59:                     Write-Output 'Change current memory setting';
  60:                  
  61:                     [int]$memoryMB = $vm.Memory
  62:                         
  63:                     if($VMAction -eq "IncreaseMemory")
  64:                     { 
  65:                         [int]$newMemoryMB = $memoryMB+$VMValue;
  66:                     }
  67:                     elseif($VMAction -eq "DecreaseMemory")
  68:                     {
  69:                         [int]$newMemoryMB = $memoryMB-$VMValue;
  70:                     }
  71:     
  72:                     Set-VM $vm -MemoryMB $newMemoryMB | Out-Null
  73:                     #Getting new updated Memory
  74:                     $vm = Get-SCVirtualMachine -Name $VMName
  75:                     [int]$memoryMB = $vm.Memory
  76:                     Write-Output "Updated Memory : '$memoryMB' `r`n"
  77:                        
  78:                     Write-Output " Successfully completed change VM...`r`n"
  79:                 }
  80:                 catch
  81:                 {
  82:                     Write-Output $Error[0].Exception.ToString()
  83:                 }
  84:             }
  85:             Remove-PSSession -Session $Session
  86:             
  87:         }
  88:         catch
  89:         {
  90:             Write-Output $Error[0].Exception.ToString() 
  91:         }
  92:     }
  93: }
8. Import the runbook on SMA server
clip_image019
9. Run the SMA workflow. Enter Parameters values.
clip_image021
10. The output of the SMA Runbook can be seen in Output pane.
clip_image023

 

Summary

I hope the above content will be useful to you and has given you a first step to start working on converting traditional Orchestrator runbooks to SMA runbooks.
In my next blog, I will discuss scenario on how you can move complex runbooks to SMA.
Till then, happy automating! πŸ™‚

No comments:

Post a Comment