Powershell 命令 - 用多行值替换 [英] Powershell command -replace with multi-line value

查看:71
本文介绍了Powershell 命令 - 用多行值替换的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在努力使用 powershell 用多行值替换字符串.该值来自 Jenkins 输入文本参数.所以这个值是一个多行字符串.

我使用 powershell 将 {{BUILD_INFO_CHANGES}} 替换为 %BUILD_INFO_CHANGES%.

%BUILD_INFO_CHANGES% 值为

-bug1

-错误 2

脚本如下:

powershell -Command "(gc %JOB_BUILD_DIR%\ThisBuildInfo.md) -replace '{{BUILD_INFO_CHANGES}}', '%BUILD_INFO_CHANGES%' | Out-File %JOB_BUILD_DIR%\ThisBuildInfo.md"

但是,我收到了 Jenkins 的错误响应.

<块引用>

'{{BUILD_INFO_FIXED_BUGS}}',<<<<'- bug1 缺少终止符:'.... + FullQualifiedErrorId : TerminatorExpectedAtEndOfString

然后我更改了脚本并使用 @ 来包装值.这是更改后的脚本.

powershell -Command "(gc %JOB_BUILD_DIR%\ThisBuildInfo.md) -replace '{{BUILD_INFO_CHANGES}}', @'%BUILD_INFO_CHANGES%'@ | Out-File %JOB_BUILD_DIR%\ThisBuildInfo.md"

我又犯了一个错误.

<块引用>

FullyQualifiedErrorId : UnrecognizedToken

有没有人有解决方案?

谢谢!

解决方案

问题在于将 -Command 参数用于除简单脚本之外的任何其他内容时,您会遇到诸如花括号之类的字符的问题并且引号在传递给 PowerShell 之前会被命令提示符误解.您可以通过添加几层转义将自己打结,或者有一种更简单的方法 - 改用 -EncodedCommand 参数.

对于 -EncodedCommand,您只需要对您的命令进行 Base64 编码,您可以使用以下 PowerShell 脚本执行此操作:

$command = @'# 在此处输入包含大括号和引号的命令#只要你喜欢'@$bytes = [System.Text.Encoding]::Unicode.GetBytes($command)$encodedCommand = [Convert]::ToBase64String($bytes) |剪辑程序

这会将编码的命令复制到剪贴板,然后使用命令所需要做的就是输入:

powershell.exe -EncodedCommand

.. 然后粘贴你的命令,这样你最终会得到如下内容:

<预> <代码> powershell.exe -EncodedCommand IAAgACMAIABFAG4AdABlAHIAIAB5AG8AdQByACAAcwBjAHIAaQBwAHQAIABjAG8AbgB0AGEAaQBuAGcAIABjAHUAcgBsAHkAIABiAHIAYQBjAGUAcwAgAGEAbgBkACAAcQB1AG8AdABlAHMACgAgACAAIwAgAEEAcwAgAGwAbwBuAGcAIABhAHMAIAB5AG8AdQAgAGwAaQBrAGUAIAAgAA ==

你现在有一些命令提示符安全的东西.

I'm struggling to use powershell to replace a string with multi-line value. The value is from Jenkins input text parameter. So this value is a multi-line string.

I use powershell to replace {{BUILD_INFO_CHANGES}} with %BUILD_INFO_CHANGES%.

The %BUILD_INFO_CHANGES% value is

-bug1

-bug 2

Here is the script:

powershell -Command "(gc %JOB_BUILD_DIR%\ThisBuildInfo.md) -replace '{{BUILD_INFO_CHANGES}}', '%BUILD_INFO_CHANGES%' | Out-File %JOB_BUILD_DIR%\ThisBuildInfo.md"

However, I got the error response from Jenkins.

'{{BUILD_INFO_FIXED_BUGS}}', <<<< '- bug1 is missing the terminator: '. ... + FullyQualifiedErrorId : TerminatorExpectedAtEndOfString d

And I change the script and use @ to wrap the value. Here is the changed script.

powershell -Command "(gc %JOB_BUILD_DIR%\ThisBuildInfo.md) -replace '{{BUILD_INFO_CHANGES}}', @'%BUILD_INFO_CHANGES%'@ | Out-File %JOB_BUILD_DIR%\ThisBuildInfo.md"

I got another error.

FullyQualifiedErrorId : UnrecognizedToken

Does anyone have a solution for this?

thanks!

解决方案

The problem is using the -Command parameter for anything other than a simple script you will run into issues where characters such as curly braces and quotes will be misinterpreted by the command prompt before the are they are passed to PowerShell. You could could tie yourself in knots by adding several layers of escaping or there is a simpler way - use the -EncodedCommand parameter instead.

For the -EncodedCommand you just need to Base64 encode your command which you can do with the following PowerShell script:

$command = @'
  # Enter your commands containg curly braces and quotes here
  # As long as you like  
'@
$bytes = [System.Text.Encoding]::Unicode.GetBytes($command)
$encodedCommand = [Convert]::ToBase64String($bytes) | clip.exe

This will copy the encoded command to the clipboard, then all you need to do to use your command is type:

powershell.exe -EncodedCommand

.. and then paste in your command so that you end up with something like the following:

powershell.exe -EncodedCommand IAAgACMAIABFAG4AdABlAHIAIAB5AG8AdQByACAAcwBjAHIAaQBwAHQAIABjAG8AbgB0AGEAaQBuAGcAIABjAHUAcgBsAHkAIABiAHIAYQBjAGUAcwAgAGEAbgBkACAAcQB1AG8AdABlAHMACgAgACAAIwAgAEEAcwAgAGwAbwBuAGcAIABhAHMAIAB5AG8AdQAgAGwAaQBrAGUAIAAgAA==

You now have something that is command prompt safe.

这篇关于Powershell 命令 - 用多行值替换的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆