在jenkins groovy中使用powershell替换远程文件中的字符串

cqoc49vn  于 2023-08-02  发布在  Jenkins
关注(0)|答案(1)|浏览(124)

下面的powershell命令可以正常工作,并替换远程主机remotehost7上的文件C:\test1\.env中的字符串

echo y | plink -ssh -l deploydevuser -pw psww -P 22 remotehost7 -t "powershell -Command '(Get-Content -path C:\test1\.env ) -replace \`http://oldstring.com\`,\`https://newstring.com\` | Set-Content -Path C:\test1\.env'"

字符串
我希望使用Jenkins groovy脚本触发相同的命令。下面是代码:

bat '''
\$stringToReplace = "http://oldstring.com"
\$replaceWith = "https://newstring.com"
\$plinkCmd = "plink -ssh -l deploydevuser -pw psww -P 22 remotehost7"
\$updatedRemotePackagePathCyg = "C:\\test1\\.env"

echo "****************starting"

echo "echo y | ${plinkCmd} -t '(Get-Content -path \"\$updatedRemotePackagePathCyg\") -replace \"\$stringToReplace\",\"\$replaceWith\" | Set-Content -Path \"\$updatedRemotePackagePathCyg\"'"

echo "************\$updatedRemotePackagePathCyg****starting"

powershell -Command "echo y | ${plinkCmd} -t '(Get-Content -path \"\$updatedRemotePackagePathCyg\") -replace \"\$stringToReplace\",\"\$replaceWith\" | Set-Content -Path \"\$updatedRemotePackagePathCyg\"'"
    '''


但是,我得到以下错误:

D:\Jenkins\workspace\ityD_feature_imp_exp_url>echo "****************starting"

"****************starting"


D:\Jenkins\workspace\ityD_feature_imp_exp_url>echo "echo y | ${plinkCmd} -t '(Get-Content -path "$updatedRemotePackagePathCyg") -replace "$stringToReplace","$replaceWith" | Set-Content -Path "$updatedRemotePackagePathCyg"'"

"echo y | ${plinkCmd} -t '(Get-Content -path "$updatedRemotePackagePathCyg") -replace "$stringToReplace","$replaceWith" | Set-Content -Path "$updatedRemotePackagePathCyg"'"


D:\Jenkins\workspace\ityD_feature_imp_exp_url>echo "************$updatedRemotePackagePathCyg****starting"

"************$updatedRemotePackagePathCyg****starting"


D:\Jenkins\workspace\ityD_feature_imp_exp_url>powershell -Command "echo y | ${plinkCmd} -t '(Get-Content -path "$updatedRemotePackagePathCyg") -replace "$stringToReplace","$replaceWith" | Set-Content -Path "$updatedRemotePackagePathCyg"'"

At line:1 char:22

+ echo y | ${plinkCmd} -t '(Get-Content -path $updatedRemotePackagePath ...

+                      ~~

Unexpected token '-t' in expression or statement.

At line:1 char:10

+ echo y | ${plinkCmd} -t '(Get-Content -path $updatedRemotePackagePath ...

+          ~~~~~~~~~~~~~~

Expressions are only allowed as the first element of a pipeline.

At line:1 char:25

+ ... linkCmd} -t '(Get-Content -path $updatedRemotePackagePathCyg) -replac ...

+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

Unexpected token ''(Get-Content -path $updatedRemotePackagePathCyg) -replace $stringToReplace,$replaceWith |

Set-Content -Path $updatedRemotePackagePathCyg'' in expression or statement.

    + CategoryInfo          : ParserError: (:) [], ParentContainsErrorRecordException

    + FullyQualifiedErrorId : UnexpectedToken


你能建议一下吗?

c9x0cxw0

c9x0cxw01#

您将批处理文件语法与PowerShell语法混合在一起,这是不可行的。

  • 使用一致的批处理文件语法,特别是用SET定义变量,用%...%引用变量。
  • 问题中第一个片段所示的powershell.exe调用不起作用,原因有几个,但主要是因为当从 * 外部 * 调用PowerShell时,在命令行上将命令包含在'...'中会导致它们被视为 * 逐字字符串 *,而不是命令。

请尝试以下操作:

  • 在您的尝试中,为了Groovy,\被转义为\\
  • 通常,由于使用''' Groovy字符串文字,因此 * 不 * 需要转义$字符。(下面的解决方案中没有)。
bat '''\
@echo off

set "stringToReplace=http://oldstring.com"
set "replaceWith=https://newstring.com"
set "updatedRemotePackagePathCyg=C:\\test1\\.env"
set "plinkCmd=plink -ssh -l deploydevuser -pw psww -P 22 remotehost7"

:: Construct the full command line.
:: Note: The assumption is that the variable values do not 
::       contain embedded ' (single quotes).
::       To cmd.exe, what is between \\"...\\" is *unquoted*, hence
::       the need to ^-escape the second | too.
set cmdLine=echo y^| %plinkCmd% -t "powershell -Command \\"(Get-Content '%updatedRemotePackagePathCyg%') -replace '%stringToReplace%', '%replaceWith%' ^| Set-Content '%updatedRemotePackagePathCyg%'\\""

echo "****************starting"

:: Echo the command line. Note the need to escape the "|" chars.
echo %cmdLine:|=^|%

echo "************ %updatedRemotePackagePathCyg% ****starting"

:: Invoke the command line.
%cmdLine%
'''

字符串

相关问题