【问题标题】:How can I put an output that contains single quotes into a string in Powershell?如何将包含单引号的输出放入 Powershell 中的字符串中?
【发布时间】:2021-11-22 02:47:11
【问题描述】:

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

 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,
 ...

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

【问题讨论】:

    标签: string powershell variables


    【解决方案1】:

    使用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,
     ...
    '@
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2013-05-29
      • 1970-01-01
      • 2011-12-07
      • 1970-01-01
      • 1970-01-01
      • 2011-03-28
      相关资源
      最近更新 更多