所有我需要的是复制文本作为text/html到剪贴板,然后粘贴到富文本编辑器上的utf-8网页。但是,尽管使用了chcp来更改编码,但是使用环状字符还是存在编码问题。
这是.ps1文件的样子(当然文件本身也有UTF8编码):
chcp 65001
$args[0] | Set-Clipboard -AsHtml使用chcp 65001(UTF-8)和西里尔文参数从cmd运行脚本:
chcp 65001
powershell C:\Users\admin\go.ps1 "КИРИЛЛИЦА IS CYRILLIC"命令输出:

当我粘贴到富文本编辑器中时,问题出现了:

发布于 2017-11-29 05:07:33
BUG: Set-Clipboard -AsHtml puts invalid CF_HTML on clipboard with non-ASCII text
Blake Coverett的变通方法,适用于任何其他受此影响的人
Function StringToEntities ($aux) {
## https://windowsserver.uservoice.com/users/307215574-blake-coverett
($aux.ToCharArray() | ForEach-Object {
$codePoint = [int]$_
if ($codePoint -ge 127) {
"&#$codePoint;"
} else {
$_
}
}) -join ''
}
'buggy'
$args[0] | Set-Clipboard -AsHtml
Get-Clipboard -TextFormatType Html | Where-Object {$_ -match 'body'}
'workaround'
StringToEntities( $args[0]) | Set-Clipboard -AsHtml
Get-Clipboard -TextFormatType Html | Where-Object {$_ -match 'body'}示例输出(仅用于调试;显示cmd代码页无关紧要):
==> chcp
Active code page: 852
==> powershell -noprofile D:\PShell\SO\47474346.ps1 "'КИРИЛЛИЦА IS CYRILLIC, ελληνικά is Greek'"
buggy
<html><body><!--StartFragment-->????????? IS CYRILLIC, ???????? is Greek<!--EndFragment--></body
></html>
workaround
<html><body><!--StartFragment-->КИРИЛЛИЦА
IS CYRILLIC, ελληνικά is Greek<!--EndFragment--></body><
/html>
==>在https://html-online.com/editor/进行了测试

https://stackoverflow.com/questions/47474346
复制相似问题