windows 来自 System.Net.WebClient 的 powershell 字符编码

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2019251/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 13:42:21  来源:igfitidea点击:

powershell character encoding from System.Net.WebClient

windowsencodingpowershell

提问by elgrego

I am running the following command:

我正在运行以下命令:

([xml](new-object net.webclient).DownloadString(
"http://blogs.msdn.com/powershell/rss.aspx"
)).rss.channel.item | format-table title,link

The output for one of the RSS items contains this weird text:

RSS 项目之一的输出包含以下奇怪的文本:

You Dona?t Have to Be An Administrator to Run Remote PowerShell Commands

So, the question is:

所以,问题是:

  • Why the mix up in characters? What happened to the apostrophe? Why is the output rendered as Dona?twhen it should just render as Don't?
  • How would I get the correct character in the PowerShell standard output?
  • 为什么会混淆字符?撇号怎么了?为什么输出呈现为Dona?t它应该呈现的时候Don't
  • 如何在 PowerShell 标准输出中获得正确的字符?

回答by Filburt

You need to set the encoding property of the webclient:

您需要设置 webclient 的 encoding 属性:

$wc = New-Object System.Net.WebClient
$wc.Encoding = [System.Text.Encoding]::UTF8
([xml]$wc.DownloadString( "http://blogs.msdn.com/powershell/rss.aspx" )).rss.channel.item | format-table title,link