KBEC-00444 - Execute PowerShell scripts with parameters through EC-PowerShell plugin

Description:

When running PowerShell scripts through EC-PowerShell script you can execute the methods runPowerShell or runPowerShellBroadcast, but these two procedures only offer the parameters:

  • Path to a PowerShell file > Execute a PowerShell script, but doesn´t accept parameters.
  • Command > PowerShell commands to be executed.

Problem:

How can we execute an existing PowerShell script that accepts parameters

Resolution:

We can define a PowerShell script using the parameter “command” with an instruction similar to:

Invoke-Expression 'c:\yourPs1Path\yourPs1File.ps1 "yourParam"'

In our example, we want to execute a PowerShell script located in the path C:\powerShell Examples\example 2.ps1, passing down a parameter defined in our pipeline parameter section called “test”

We have to consider that PowerShell command Invoke-Expression doesn´t work properly with paths that contain spaces, so you will need to use the short path of the folder that contains the ps1 file.

To get this, you can create in the folder that contains the ps1 file, the file shortNamePath.cmd and add the content:

@ECHO OFF
if '%1'=='' (%0 .) else echo %~s1

You can download the file shortNamePath.cmd here

If you run this command inside a folder it will return the short path of this folder.

You can also use it passing as a parameter the path you want to know the short path and it will return it.

For example:

C:\powerShell Examples> shortNamePath.cmd
C:\POWERS~1

C:\powerShell Examples>shortNamePath.cmd "c:\Program Files\Common Files\microsoft shared"
c:\PROGRA~1\COMMON~1\MICROS~1

Once in this folder, if your ps1 file name contains spaces, you can find the short file name using dir /x

C:\powerShell Examples>dir /x
 Volume in drive C is Windows 2016
 Volume Serial Number is 26DF-55D2

 Directory of C:\powerShell Examples

05/06/2020  01:47 PM    <DIR>                       .
05/06/2020  01:47 PM    <DIR>                       ..
05/06/2020  12:14 PM                42 EXAMPL~1.PS1      example 2.ps1
05/06/2020  12:14 PM                42                                 example.ps1
05/06/2020  01:14 PM               106                                launcher.ps1
05/06/2020  12:16 PM                34                                 ps.bat
05/06/2020  01:49 PM                44 SHORTN~1.CMD   shortNamePath.cmd
               5 File(s)            268 bytes
               2 Dir(s)  49,624,276,992 bytes free

So, in our case, if we want to run the PowerShell file C:\powerShell Examples\example 2.ps1passing the pipeline parameter “test” value as parameter, we would need to define the command as:

Invoke-Expression 'C:\powers~1\EXAMPL~1.PS1 "$[/myPipelineRuntime/test]"'

In case that after running your script you receive an issue similar to:

File X:\job_5788_20200506140332\temp5ed4ce5b-8fa2-11ea-8ba1-080027ddb162.ps1 cannot be loaded. The file 
X:\job_5788_20200506140332\temp5ed4ce5b-8fa2-11ea-8ba1-080027ddb162.ps1 is not digitally signed. You cannot run this 
script on the current system. For more information about running scripts and setting execution policy, see 
about_Execution_Policies at http://go.microsoft.com/fwlink/?LinkID=135170.
    + CategoryInfo          : SecurityError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnauthorizedAccess

or

AuthorizationManager check failed.
    + CategoryInfo          : SecurityError: (:) [], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : UnauthorizedAccess

This is due to you are executing this command through a file generated in the workspace folder (in our example the workspace path is x:\).

As this is a network folder, we will need to add it as a trusted site in the Internet Properties in the Agent that execute this ps1 file.

You can open the Internet Properties running inetcpl.cpl > Security > Trusted Sites > Sites and add your workspace network path:

Add Trusted Site

Have more questions?

0 Comments

Please sign in to leave a comment.