【问题标题】:Why is Powershell output different between 5.1 and Core 7.1.4为什么 Powershell 输出在 5.1 和 Core 7.1.4 之间不同
【发布时间】:2021-08-29 14:03:24
【问题描述】:

我有一个 PowerShell 代码块,它运行一个命令,然后对其进行处理并将其写入一个文本文件。

$commandOutput = &"C:\Program Files (x86)\Dell EMC\Unity\Unisphere CLI\uemcli" -d $frame -noHeader /stor/config/pool/sr show
foreach ($idline in $commandOutput)
{
    if ($idline -match ' ID ')
    {
    $subline = $idline.substring(6,($idline.length - 6))
    $writeline = $subline.trim()
    #$writeline
    }
    elseif ($idline -match 'Total pool space used')
    {
    $Sleft,$Sright = $idline.Split('(',2)
    $writeline = $Sleft.trim()
    #    $writeline
    #    write-host ""
    } 
 
    else {$writeline = $idline.trim()}
    #$writeline
    Add-Content -path $outdir\$frame-intrim.txt -value $writeline -Encoding UTF8
    }

当我在 PowerShell 5.1 中运行它时,我得到:

ID                    = res_2
Name                  = asi-vcva-fs01
Resource type         = VMware NFS
Pool                  = pool_1
Total pool space used = 18025199894528
Health state          = OK (5)

当我在 PowerShell Core 7.1.4 中运行它时,我得到:

1 :          I D                                         =   r e s _ 2 
             N a m e                                     =   a s i - v c v a - f s 0 1 
             R e s o u r c e   t y p e                   =   V M w a r e   N F S 
             P o o l                                     =   p o o l _ 1 
             T o t a l   p o o l   s p a c e   u s e d   =   1 8 0 2 5 2 1 3 2 7 2 0 6 4   ( 1 6 . 3 T ) 
             H e a l t h   s t a t e                     =   O K   ( 5 )

这弄乱了脚本的其余部分。我尝试了 -Encoding UTF8 但没有任何改变。如何使它能够在 5.1 和 Core 7.1 中运行?

更多信息:

它以这种方式写入输出的原因是因为这是从输入中解析数据的方式。如何去除空格,特别是使其在两个版本的代码中都可用?

【问题讨论】:

    标签: powershell powershell-7.0 powershell-5.1


    【解决方案1】:

    我不知道为什么在这种情况下,PowerShell 版本之间的行为会有所不同,但看起来uemcli 输出的是 UTF-16LE(“Unicode”)编码字符串

    为了让 PowerShell 正确识别它们[1],(暂时)设置
    [Console]::OutputEncoding = [Text.Encoding]::Unicode
    在调用之前


    至于诊断特定外部程序使用的字符编码以及PowerShell如何对其进行解码:

    • this answer的底部,它提供了两个辅助函数:

      • Debug-NativeInOutput 帮助您诊断外部程序的输入和输出。

      • Invoke-WithEncoding 使使用给定编码的外部程序调用更容易;例如,在您的情况下,您会调用:

        Invoke-WithEncoding -Encoding Unicode { 
          & "C:\Program Files (x86)\Dell EMC\Unity\Unisphere CLI\uemcli" -d $frame -noHeader /stor/config/pool/sr show
        }
        
    • This answer 包含另一个辅助函数Debug-String,它可以帮助您在一般字符串中查找非打印和非ASCII 字符;例如,从 UTF-16LE 输出中错误解码的 ASCII 范围字符将导致该字符加上一个 NUL (0x0) 字符Debug-String 将显示为 `0

      • 默认情况下,NUL 字符会在控制台中打印类似空格,这就是您所看到的。

    [1] 在后续的处理中;请注意,direct-to-display 输出可能打印 正常,但字符编码问题可能会在 PowerShell 解码 输出时出现,这种情况总是会发生在捕获变量中的输出或通过管道或文件发送输出的过程中。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-09-28
      • 1970-01-01
      • 1970-01-01
      • 2012-07-23
      • 2017-11-09
      相关资源
      最近更新 更多