Ola Eldøy
Ola Eldøy

Reputation: 5936

Running a PowerShell script file with path containing spaces from Jenkins Pipeline without using backtick

I want to run the following PowerShell script file from Jenkins Pipeline:

".\Folder With Spaces\script.ps1"

I have been able to do it with the following step definition:

powershell(script: '.\\Folder` With` Spaces\\script.ps1')

So I have to remember to:

I would prefer to avoid at least some of this. Is it possible to avoid using the backtick escaping, for example? (Putting it between "" does not seem to work, for some reason.)

Upvotes: 0

Views: 2920

Answers (2)

Joerg S
Joerg S

Reputation: 5129

To avoid escaping the backslashes you could use slashy strings or dollar slashy strings as follows. However you cannot use a backslash as the very last character in slashy strings as it would escape the /. Of course slashes as well would have to be escaped when using slashy strings.

String slashy = /String with \ /
echo slashy
assert slashy == 'String with \\ '

// won't work
// String slashy = /String with \/

String dollarSlashy = $/String with / and \/$
echo dollarSlashy
assert dollarSlashy == 'String with / and \\'

And of course you'll lose the possibility to include newlines \n and other special characters in the string using the \. However as both slashy and dollar slashy strings have multi line support at least newlines can be included like:

String slashyWithNewline = /String with \/ and \ 
with newline/
echo slashyWithNewline
assert slashyWithNewline == 'String with / and \\ \nwith newline'

String dollarSlashyWithNewline = $/String with / and \ 
with newline/$
echo dollarSlashyWithNewline
assert dollarSlashyWithNewline == 'String with / and \\ \nwith newline'

If you combine that with your very own answer you won't need both of the escaping.

Upvotes: 1

Ola Eldøy
Ola Eldøy

Reputation: 5936

I found that it's possible to use the ampersand, or invoke, operator, like this:

powershell(script: "& '.\\Folder With Spaces\\script.ps1'")

That gets rid of the backtick escaping, and should make life a tiny bit easier.

Upvotes: 1

Related Questions