简体   繁体   中英

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:

  • escape the backslash with a double backslash (Groovy syntax)
  • escape the space with backtick (PowerShell syntax)

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.)

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM