簡體   English   中英

在不使用反引號的情況下運行具有包含Jenkins Pipeline中空格的路徑的PowerShell腳本文件

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

我想從Jenkins Pipeline運行以下PowerShell腳本文件:

".\Folder With Spaces\script.ps1"

我已經可以使用以下步驟定義來做到這一點:

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

因此,我必須記住:

  • 使用雙反斜杠轉義反斜杠(Groovy語法)
  • 使用反引號轉義空間(PowerShell語法)

我希望至少避免這種情況。 例如,是否可以避免使用反引號轉義? (由於某種原因,將其放在“”之間似乎無效。)

我發現可以使用&或調用運算符,如下所示:

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

這就消除了反引號的轉義,應該使生活稍微容易一些。

為了避免轉義反斜杠,可以按如下所示使用斜杠字符串美元斜杠字符串 但是,不能將反斜杠用作斜杠字符串中的最后一個字符,因為它會轉義/ 當然,使用斜線字符串時也必須轉義斜線。

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 \\'

當然,您將失去使用\\在字符串中包含換行符\\n和其他特殊字符的可能性。 但是,由於斜線和美元斜線字符串都具有多行支持,因此至少可以包含換行符,例如:

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'

如果您將其與自己的答案結合在一起,則不需要兩個轉義。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM