gpt4 book ai didi

powershell - 为什么 Powershell 输出在 5.1 和 Core 7.1.4 之间不同

转载 作者:行者123 更新时间:2023-12-04 14:04:26 25 4
gpt4 key购买 nike

我有一段 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 版本之间的行为不同,但它看起来像 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字符在控制台中打印like spaces,这就是您所看到的。

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

关于powershell - 为什么 Powershell 输出在 5.1 和 Core 7.1.4 之间不同,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/68973687/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com