如何在 powerShellScript 中使用特殊字符

问题描述 投票:0回答:1

所以我尝试使用下面的脚本动态地将不同的值放入属性文件中 但 powershell 不允许我使用特殊字符,例如 !

@echo off
setlocal enabledelayedexpansion

rem Set the properties file path
set "PROPERTIES_FILE=src\main\resources\config.properties"

rem Replace placeholders with Jenkins parameters in the properties file
powershell -Command "(Get-Content '%PROPERTIES_FILE%') | ForEach-Object { $_ -replace 'USERNAME=(.*)', 'USERNAME=%Username%' } | Set-Content '%PROPERTIES_FILE%'"
powershell -Command "(Get-Content '%PROPERTIES_FILE%') | ForEach-Object { $_ -replace 'PASSWORD=(.*)', ('PASSWORD=' + [regex]::escape('%Password%')) } | Set-Content '%PROPERTIES_FILE%'"


rem Optionally, print the updated file contents for verification
type "%PROPERTIES_FILE%"

rem Execute your Maven command
mvn clean test -Dtest=ScriptTest

我正在尝试使用 Jenkins 参数来更新 config.properties 文件。

powershell jenkins
1个回答
0
投票

从您的

enabledelayedexpansion
声明中删除
setlocal

它“吃掉”了

!
字符 - 即使是静态引用变量的 values 中存在的字符,例如
%Password%

© www.soinside.com 2019 - 2024. All rights reserved.