从 powershell 执行 native cmd 命令时,例如 net view \\192.168.1.x
我得到以下结果,其中非英文字符显示为 ????
:
Share name Type Used as Comment
--------------------------------------------------
AntEater Disk
AntEater_Eab Disk
HP M4345 ????? Print HP M4345 ?????
Letters Disk
SMSCenter Disk
temp Disk
Users Disk
????? ??? ?????? Print ????? ??? ??????
The command completed successfully.
如果我跑
chcp 862
我得到以下结果(在控制台上):Share name Type Used as Comment
--------------------------------------------------
AntEater Disk
AntEater_Eab Disk
HP M4345 הנהחש Print HP M4345 הנהחש
Letters Disk
SMSCenter Disk
temp Disk
Users Disk
מדפסת Print
这很好,但是当我尝试将其保存到变量或将其导出到文件时:
$temp = net view \\192.168.1.x
$temp
Share name Type Used as Comment
--------------------------------------------------
AntEater Disk
AntEater_Eab Disk
HP M4345 ????? Print HP M4345 ?????
Letters Disk
SMSCenter Disk
temp Disk
Users Disk
????? ??? ?????? Print ????? ??? ??????
结果又错了
我已经试过了:
net view \\192.168.1.x | Out-File C:\temp.txt -Encoding Unicode
cmd /c 'chcp 862 && net view \\192.168.1.x' | Out-File C:\temp.txt -Encoding Unicode
cmd /c 'chcp 862 && net view \\192.168.1.x > c:\temp.txt'
cmd /u /c 'chcp 862 && net view \\192.168.1.x > c:\temp.txt'
更真的……
运行
net view \\192.168.1.x | Out-String
时也显示问题相同的结果是我是否从 CMD 或 PowerShell 运行它
你知道这种行为吗?如何将正确的输出保存到变量或文件?
最佳答案
关于powershell - 从 Powershell 使用 native CMD 命令时的编码问题,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34510700/