Advertisement
Old-Lost

runbook ps code template

Jul 10th, 2017
127
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Set script parameters from runbook data bus and Orchestrator global variables
  2. # Define any inputs here and then add to the $argsArray and script block parameters below
  3.  
  4. $DataBusInput1 = "{Parameter 1 from Initialize Data}"
  5. $DataBusInput2 = "{Global Variable 1}"
  6.  
  7.  
  8. #-----------------------------------------------------------------------
  9.  
  10. ## Initialize result and trace variables
  11. # $ResultStatus provides basic success/failed indicator
  12. # $ErrorMessage captures any error text generated by script
  13. # $Trace is used to record a running log of actions
  14. $ResultStatus = ""
  15. $ErrorMessage = ""
  16. $Trace = (Get-Date).ToString() + "`t" + "Runbook activity script started" + " `r`n"
  17.        
  18. # Create argument array for passing data bus inputs to the external script session
  19. $argsArray = @($DataBusInput1, $DataBusInput2)
  20.  
  21. # Establish an external session (to localhost) to ensure 64bit PowerShell runtime using the latest version of PowerShell installed on the runbook server
  22. # Use this session to perform all work to ensure latest PowerShell features and behavior available
  23. $Session = New-PSSession -ComputerName localhost
  24.  
  25. # Invoke-Command used to start the script in the external session. Variables returned by script are then stored in the $ReturnArray variable
  26. $ReturnArray = Invoke-Command -Session $Session -Argumentlist $argsArray -ScriptBlock {
  27.     # Define a parameter to accept each data bus input value. Recommend matching names of parameters and data bus input variables above
  28.     Param(
  29.         [ValidateNotNullOrEmpty()]
  30.         [string]$DataBusInput1,
  31.  
  32.         [ValidateNotNullOrEmpty()]
  33.         [string]$DataBusInput2
  34.     )
  35.  
  36.     # Define function to add entry to trace log variable
  37.     function AppendLog ([string]$Message) {
  38.         $script:CurrentAction = $Message
  39.         $script:TraceLog += ((Get-Date).ToString() + "`t" + $Message + " `r`n")
  40.     }
  41.  
  42.     # Set external session trace and status variables to defaults
  43.     $ResultStatus = ""
  44.     $ErrorMessage = ""
  45.     $script:CurrentAction = ""
  46.     $script:TraceLog = ""
  47.  
  48.     try {
  49.         # Add startup details to trace log
  50.         AppendLog "Script now executing in external PowerShell version [$($PSVersionTable.PSVersion.ToString())] session in a [$([IntPtr]::Size * 8)] bit process"
  51.         AppendLog "Running as user [$([Environment]::UserDomainName)\$([Environment]::UserName)] on host [$($env:COMPUTERNAME)]"
  52.         AppendLog "Parameter values received: DataBusInput1=[$DataBusInput1]; DataBusInput2=[$DataBusInput2]"
  53.  
  54.         # The actual work the script does goes here
  55.         AppendLog "Doing first action"
  56.         # Do-Stuff -Value $DataBusInput1
  57.  
  58.         AppendLog "Doing second action"
  59.         # Do-MoreStuff -Value $DataBusInput2
  60.  
  61.         # Simulate a possible error
  62.         if ($DataBusInput1 -ilike "*bad stuff*") {
  63.             throw "ERROR: Encountered bad stuff in the parameter input"
  64.         }
  65.  
  66.         # Example of custom result value
  67.         $myCustomVariable = "Something I want to publish back to the runbook data bus"
  68.  
  69.         # Validate results and set return status
  70.         AppendLog "Finished work, determining result"
  71.         $EverythingWorked = $true
  72.         if ($EverythingWorked -eq $true) {
  73.             $ResultStatus = "Success"
  74.         } else {
  75.             $ResultStatus = "Failed"
  76.         }
  77.     } catch {
  78.         # Catch any errors thrown above here, setting the result status and recording the error message to return to the activity for data bus publishing
  79.         $ResultStatus = "Failed"
  80.         $ErrorMessage = $error[0].Exception.Message
  81.         AppendLog "Exception caught during action [$script:CurrentAction]: $ErrorMessage"
  82.     } finally {
  83.         # Always do whatever is in the finally block. In this case, adding some additional detail about the outcome to the trace log for return
  84.         if ($ErrorMessage.Length -gt 0) {
  85.             AppendLog "Exiting external session with result [$ResultStatus] and error message [$ErrorMessage]"
  86.         } else {
  87.             AppendLog "Exiting external session with result [$ResultStatus]"
  88.         }
  89.     }
  90.     # Return an array of the results. Additional variables like "myCustomVariable" can be returned by adding them onto the array
  91.     @($ResultStatus, $ErrorMessage, $script:TraceLog, $myCustomVariable)
  92. }#End Invoke-Command
  93.  
  94. # Get the values returned from script session for publishing to data bus
  95. $ResultStatus = $ReturnArray[0]
  96. $ErrorMessage = $ReturnArray[1]
  97. $Trace += $ReturnArray[2]
  98. $MyCustomVariable = $ReturnArray[3]
  99.  
  100. # Record end of activity script process
  101. $Trace += (Get-Date).ToString() + "`t" + "Script finished" + " `r`n"
  102.  
  103. # Close the external session
  104. Remove-PSSession $Session
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement