How to use PowerShell script variables?

chadlockwood
Kilo Sage

I have a PowerShell MID Server Script file, Out-MyNameIs.ps1:

 

[CmdletBinding()]

Param(

      [string]$name

)

 

Write-Output "%%MyNameIs%%"

Write-Output "$($name)"

Write-Output "%%"

 

I expect the Output of a Run PowerShell workflow activity to look like this:

 

%%MyNameIs%%

Chad

%%

 

In the activity, I have selected Out-MyNameIs.ps1 in the Script file field. Since my script requires the $name parameter, I have created a simple JSON string to put in the PowerShell script variables field:

 

{

        "name" : "Chad"

}

 

I have replaced "name" with "$name" and "powershell_name"(based on comment in Run PowerShell activity definition), and have changed the $name parameter in the script to $powershell_param_name as it appears in the ecc queue:

 

<parameter name="powershell_param_name" value="Chad"/>


So far, all I have seen is:

 

%%MyNameIs%%

 

%%

 

Has anyone any experience with using PowerShell script variables that can help me get this working?

 

Thanks,

Chad

1 ACCEPTED SOLUTION

Chad,


I found solution for this, it looks like below.


In your workflow you should pass name as :


"name": "${your variable here if dynamic}"



and in powershell


$localVar = $env:SNC_name; // this is your workflow var



Thanks,


Kartheek


View solution in original post

24 REPLIES 24

If I had the time... Start building it and invite me


tim_alman
Tera Expert

How to Setup an Orchestration PowerShell Activity in a Workflow



I've uploaded a video to do a quick run-through of setting up a PowerShell activity.


Thanks buddy!


syap
Tera Contributor

Hi Tim,



Thanks for the video you created it was a great help learning how to use PS activity in a workflow. I have a question. I have a requirement from customer to automate the creation of Distribution Group with multiple Owners and Members. I tried creating the DG like this: New-DistributionGroup -Name $groupName -ManagedBy $groupOwners -Members $groupMembers -CopyOwnerToMember



The variables are passed from the Run Powershell Activity of the Workflow.


$groupName = <name of DG>


$groupOwners = 'User 1','User 2','User 3'


$groupMembers = 'Member 1','Member 2','Member 3'



Somehow that did not work so I create 3 PS script files.


1. Create the DG - Was successfully able to create the DG


2. Add Owners - Was not successfully able to add Owners.


3. Add Members + Owners - Was successfully able to add Members + Owners



The syntax I used to add the Owners: Set-DistributionGroup -Identity $groupName -ManagedBy $groupOwners


It gives an error: Couldn't find object "''User 1'',''User 2''". Please make sure that it was spelled correctly.....


Syap,



It's good that you broke the functionality out into different parts.   Keeping the pieces simple and reusable are a key to good programming.



The syntax that you provided looks correct for adding the Owners.   Make sure every user in the Owners list actually resolves to a mailbox.   You could try wrapping the entire Owners list in double quotes while each user is in single quotes.



Tim