下面的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
型
你能建议一下吗?
1条答案
按热度按时间c9x0cxw01#
您将批处理文件语法与PowerShell语法混合在一起,这是不可行的。
SET
定义变量,用%...%
引用变量。powershell.exe
调用不起作用,原因有几个,但主要是因为当从 * 外部 * 调用PowerShell时,在命令行上将命令包含在'...'
中会导致它们被视为 * 逐字字符串 *,而不是命令。请尝试以下操作:
\
被转义为\\
。'''
Groovy字符串文字,因此 * 不 * 需要转义$
字符。(下面的解决方案中没有)。字符串