从powershell执行本机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
运行它你知道这个行为吗?如何将正确的输出保存到变量或文件?