首页 > 解决方案 > 如何将包含单引号的输出放入 Powershell 中的字符串中?

问题描述

我很难将包含单引号 ('') 的输出转换为变量。输出是这样的:

 Warning: Invalid character is found in given range. A specified range MUST 
 Warning: have only digits in 'start'-'stop'. The server's response to this 
 Warning: request is uncertain.
 % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                             Dload  Upload   Total   Spent    Left  Speed
0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     
0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0{
"total": 2,
"subtotal": 2,
"page": 1,
 ...

我认为这个问题出在第一个单引号中。我已经尝试使用@''@、@('') 创建带有单引号的变量,但是这些形式都不起作用。我怎样才能把它放在一个变量中?

标签: stringpowershellvariables

解决方案


使用here-string,引号按字面意思解释,参见:Here-strings

$x = @'
 Warning: Invalid character is found in given range. A specified range MUST 
 Warning: have only digits in 'start'-'stop'. The server's response to this 
 Warning: request is uncertain.
 % Total    % Received % Xferd  Average Speed   Time    Time     Time  Current
                             Dload  Upload   Total   Spent    Left  Speed
0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     
0     0    0     0    0     0      0      0 --:--:-- --:--:-- --:--:--     0{
"total": 2,
"subtotal": 2,
"page": 1,
 ...
'@

推荐阅读