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

Hello everyone,



I would like to execute a Powershell script over the Run Powershell workflow acticity.



But I don't really know who does it work. Could somebody explain me who I can define PowerShell script variables for the command field?


Tom,


I would recommend that you first create your PowerShell script as a MID Server > Script File. In the Name field you will give a PowerShell file name, such as Do-Something.ps1 so that the file is saved on the MID server as a PowerShell script file. At the beginning of the script you want to include a Param block:


Param(


      [string]$variable_name = $env:SNC_workflow_variable


      )


This will give you a variable called $variable_name that will be equal to the value of <workflow_variable> from the workflow. The "$env:SNC_" is a prefix to the workflow variable to tell PowerShell what is coming. Every variable that you pass from the workflow to the PowerShell code will have this prefix. The rest of your PowerShell code will be whatever you need.



In your Run PowerShell activity, you will use the Script file field to select your Do-Something.ps1 script. Then, in the PowerShell script variables field, you will create a JSON string of variables that you will pass to your PowerShell script. It would look something like:


{


    "workflow_variable" : "${workflow.scratchpad.data_value}"


}



If you needed to pass more than one variable, you would need to add a comma to the end of the line.



If you need to pass data from the PowerShell code back to the workflow you would need to do this"


For each variable or value that you need to pass, create a block like this:



Write-Output "%%out_data%%"


Write-Output "$($variable_name)"


Write-Output "%%"



This will create a "tag" called out_data. You can use any text you need in the first line, but it must come between the two sets of percent signs. The variable or value is the second line and the final line of two percent signs closes the tag. You can create any number of these tag blocks.


In the Sensor script field of the Run PowerShell activity you will call the tag like this:


tags.out_data


I would normally use the following code to assign the tag value to a scratchpad variable:


if(tags.out_data){


    workflow.scratchpad.variable_name = tags.out_data;


}



Hope this helps.


Regards,


Chad


Hi Chad,



thanks for the detailed answer.


Hi Chad,



for another task I need values from the powershell script to the workflow.




So I did the following points:



1. Run Script activity


workflow.scratchpad.test = tags.out_data;



2. Run Powershell activity


command field:


$out_data = hostname;


Write-Output "%%out_data%%"


Write-Output "$($out_data)"


Write-Output "%%"



sensor script field:


tags.out_data



Could you tell me what is wrong?


Tom,


In your sensor script put:


workflow.scratchpad.test = tags.out_data;



You might want something like:


If(tags.out_data){


workflow.scratchpad.test = tags.out_data;


}




Regards,



Chad Lockwood | ServiceNow Solutions Developer



D 206.272.7979 M 206.280.2299



f5.com<https://www.f5.com> | synthesis.f5.com<https://synthesis.f5.com>



><https://twitter.com/f5networks>[LinkedIn]<http://www.linkedin.com/company/f5-networks>[Facebook]<https://www.facebook.com/f5networksinc>[YouTube]<http://www.youtube.com/f5networksinc>[DevCentral]<https://devcentral.f5.com>


nd Tagline




From: Tom Paulin


Reply-To: "servicenow-pub@phx1.jivehosted.com<mailto:servicenow-pub@phx1.jivehosted.com>"