使用Powershell导入具有中文域的网站 [英] Use Powershell to import website with Chinese domain
问题描述
我正在尝试使用PowerShell从xml文件添加网站.
I’m trying use PowerShell to add website from xml file.
当我只使用CMD命令时
When I just use CMD command
appcmd add site /in < test.xml
网站的绑定是测试.com.tw",一切都很好.
The binding of website is 「測試.com.tw」and everything be fine.
但是当我使用Powershell时
But when I use Powershell
Get-Content test.xml | appcmd add site /in
绑定将是"???.com.tw"
The binding will be 「???.com.tw」
即使我将chcp更改为65001或在获取内容时使用"-encoding utf8",但也没有任何改变,仍然无法正确导入具有中文域的网站.
Even if I change chcp to 65001 or use "-encoding utf8" when get-content, but nothing change and still can’t correct import a website with Chinese domain.
我的服务器是2008 R2和Powershell 1.0
My server is 2008 R2 and Powershell 1.0
有人知道我该如何解决吗?
Does anyone know how I can fix it?
推荐答案
PowerShell uses the $OutputEncoding
preference variable to determine the character encoding to use when sending data to external programs (such as appcmd
) via the pipeline (what the external program receives via stdin, the standard input stream).
在Windows PowerShell中, $ OutputEncoding
默认为ASCII(!),这意味着仅正确发送7位ASCII中的字符,并且该范围之外的所有字符均被替换为 literal ?
字符.
In Windows PowerShell, $OutputEncoding
defaults to ASCII(!), meaning that only characters in the 7-bit ASCII are correctly sent, and all characters outside that range are replaced with literal ?
characters.
您必须设置 $ OutputEncoding
以匹配 appcmd
从标准输入中读取时期望的编码;根据您的反馈,必须使用UTF-8:
You must set $OutputEncoding
to match the encoding that appcmd
expects when reading from stdin; according to your feedback, UTF-8 must be used:
$OutputEncoding = [System.Text.Utf8Encoding]::new($false) # BOM-less UTF-8
Get-Content test.xml | appcmd add site /in
# Consider restoring the previous $OutputEncoding value
注意:以上假设 Get-Content
在读取时正确识别了 test.xml
的编码.如果不是,请使用其 -Encoding
参数显式指定文件的编码.
Note: The above assumes that Get-Content
properly recognizes the encoding of test.xml
when reading from it; if not, use its -Encoding
parameter to specify the file's encoding explicitly.
互补地,当PowerShell 从外部程序读取输出时,存储在 [Console] :: OutputEncoding
中的编码是确定输出的解码方式-请参见此答案.
Complementarily, when PowerShell reads output from external programs, it is the encoding stored in [Console]::OutputEncoding
that determines how the output is decoded - see this answer.
这篇关于使用Powershell导入具有中文域的网站的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!