Quantcast
Channel: TechNet Blogs
Viewing all articles
Browse latest Browse all 34890

Automation–Orchestrator Tip/Trick–Run .Net Script Activity + PowerShell + Published Data Arrays

$
0
0

Hello Readers/Viewers!

It is time for another Tip/Trick – and this time we will take a look at the Orchestrator Run .Net Script Activity + PowerShell + Published Data Arrays…

You may have noticed a change in behavior for the Run .Net Script Activity in the release of System Center 2012 – Orchestrator. Specifically, how it handles array or object data. Regardless how it used to act, the current behavior requires a little bit more effort to see similar results. This post will walk you through two Published Data options for handling PowerShell arrays within the Run .Net Script Activity. As a bonus, these two examples will be illustrated in two different levels of complexity.


Simple Example

Handling PowerShell Array Data for Delimited Published Data

The results of this example will look very similar to the “Flatten” functionality, but instead of using “Flatten” the delimitation is handled from within the PowerShell.

Run .Net Script Configuration – Details Tab

image

NOTE: Explicitly calling the $DelimProcessList at the end of the script from within the Run .Net Script Activity is optional.

Run .Net Script Configuration – Published Data Tab

image

Run .Net Script Results – Delimited List (Single-Value Published Data)

image

Example PowerShell (for copy/paste)

001
002
003
004
005
006
007
008
009
010
$DelimProcessList = ""
$Processes = Get-Process

foreach ($Process in $Processes)
{
    $DelimProcessList += $Process.Name + ";"
}

$DelimProcessList = $DelimProcessList.Substring(0,$DelimProcessList.Length-1)
$DelimProcessList

 

Handling PowerShell Array Data for Array Published Data

The results of this example will generate Multi-Value Published Data. This means that all subsequent (downstream) Runbook Activities will consume each piece of data individually. If this is not the desired output, you can use the example above or leverage the built in “Flatten” functionality for this example’s Activity.

Run .Net Script Configuration – Details Tab

image

NOTE: Explicitly calling the $ArrayProcessList at the end of the script from within the Run .Net Script Activity is optional.

Run .Net Script Configuration – Published Data Tab

image

Run .Net Script Results – Array List (Multi-Value Published Data)

image

Example PowerShell (for copy/paste)

001
002
003
004
005
006
007
008
009
$ArrayProcessList = @()
$Processes = Get-Process

foreach ($Process in $Processes)
{
    $ArrayProcessList += $Process.Name
}

$ArrayProcessList

Advanced Example

Handling PowerShell Array Data for Delimited Published Data

Here we go again, same concept for Delimited Published Data (much like “Flatten”), just a bit more complexity on the PowerShell side.

Run .Net Script Configuration – Details Tab

image

NOTE: Explicitly calling the $Variables at the end of the script from within the Run .Net Script Activity is optional.

Run .Net Script Configuration – Published Data Tab

image

Run .Net Script Results – Delimited List (Single-Value Published Data for each Variable)

image

image

image

Example PowerShell (for copy/paste)

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
023
024
025
026
$Username = "domain\username"
$Password = "password"
$ServerName = "servername"
$NodeID = ""
$NodeName = ""
$NodeState = ""

$securePassword = ConvertTo-SecureString $Password -AsPlainText -Force
$credential = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $Username,
$securePassword
$NodeData
 = Invoke-Command -ComputerName $ServerName -credential $credential -ScriptBlock { Get-ClusterNode }

foreach ($Node in $NodeData) {

    $NodeID += $Node.ID + ";"
    $NodeName += $Node.Name + ";"
    $NodeState += $Node.State + ";"

}

$NodeID = $NodeID.Substring(0,$NodeID.Length-1)
$NodeName = $NodeName.Substring(0,$NodeName.Length-1)
$NodeState = $NodeState.Substring(0,$NodeState.Length-1)

$NodeID
$NodeName
$NodeState

 

Handling PowerShell Array Data for Array Published Data – Advanced Example

You know the drill, here is the same concept, just leveraging a bit more of an advanced example. Again, if Multi-Value Data is not what you are looking for, you can either use the example directly above or leverage the built in “Flatten” functionality for this example’s Activity.

Run .Net Script Configuration – Details Tab

image

NOTE: Explicitly calling the $Variables at the end of the script from within the Run .Net Script Activity is optional.

Run .Net Script Configuration – Published Data Tab

image

Run .Net Script Results – Array List (Multi-Value Published Data for each Variable)

image

Example PowerShell (for copy/paste)

001
002
003
004
005
006
007
008
009
010
011
012
013
014
015
016
017
018
019
020
021
022
$Username = "domain\username"
$Password = "password"
$ServerName = "servername"
$NodeID = @()
$NodeName = @()
$NodeState = @()

$securePassword = ConvertTo-SecureString $Password -AsPlainText -Force
$credential = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $Username,
$securePassword
$NodeData
 = Invoke-Command -ComputerName $ServerName -credential $credential -ScriptBlock { Get-ClusterNode }

foreach ($Node in $NodeData) {

    $NodeID += $Node.ID
    $NodeName += $Node.Name
    $NodeState += $Node.State

}

$NodeID
$NodeName
$NodeState

So, what is the difference?

Well, besides the output, there are only a two main differences:

  1. Variable Declaration
  2. How the data within the different variables is handled (because of how they were declared)

Which one should you use? Well, that all depends on how you want to use the output – either one piece of Published Data at a time from an Array List, or all at once in a Delimited List. The choice is yours!

NOTE: These differences span both “Simple” and “Advanced” Examples.

 

Variable Declaration

Delimited List Example:

001
002
003
$NodeID = ""
$NodeName = ""
$NodeState = ""

Array List Example:

001
002
003
$NodeID = @()
$NodeName = @()
$NodeState = @()

 

Handling the Different Variable Data

Delimited List Example:

001
002
003
004
005
006
foreach ($Process in $Processes)
{
    $DelimProcessList += $Process.Name + ";"
}

$DelimProcessList = $DelimProcessList.Substring(0,$DelimProcessList.Length-1)

Array List Example:

001
002
003
004
foreach ($Process in $Processes)
{
    $ArrayProcessList += $Process.Name
}

 


For more information and tips/tricks for Orchestrator, be sure to watch for blog posts in the Automation Track!

enJOY!



Viewing all articles
Browse latest Browse all 34890

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>