vbs 调用的 Powershell 中的德语变音符号
German Umlauts in Powershell called by vbs
我有一个 ps1 文件,它创建了一个 Link
创建-link.ps1
$path = $env:HOMESHARE + "\My Projects\"
If(!(test-path $path))
{
New-Item -ItemType Directory -Force -Path $path
}
$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut($env:HOMESHARE + "\My Projects\" + "linkname.lnk")
$Shortcut.TargetPath = "\path\for\link"
$Shortcut.Description = "äöüß"
$Shortcut.IconLocation = $env:SYSTEMROOT + "\system32\shell32.dll,3"
$Shortcut.Save()
我也有一个调用 ps1
的 vbs 文件
创建-link.vbs
command = "powershell.exe Get-Content ""C:\path\to\file\create-link.ps1"" | PowerShell.exe -noprofile"
set shell = CreateObject("WScript.Shell")
shell.Run command,0
两个文件都使用 utf-8 编码保存。
这个构造是必要的,因为 ps1 需要 运行 完全无头,用户不会注意到任何东西。通过 vbs 调用 ps1 解决了这个问题,如果有更好的方法,请告诉我,我会很高兴。
如果我直接调用 powershell 脚本或使用“powershell.exe Get-Content ""C:\path\to\file\create-link.ps1"" | PowerShell.exe -noprofile" (通过使用 cmd)一切正常。
但是,如果我调用 vbs 来完成它的工作,它通常会工作,但是来自 'Description' 的德语变音符号只是问号,所以不知何故编码被扰乱了。有什么办法可以解决这个问题吗?
tl;博士:
将您的 *.ps1
文件保存为 UTF-8 with BOM.
使用 PowerShell CLI 的 -File
参数简化您的命令:
command = "powershell.exe -NoProfile -File ""C:\path\to\file\create-link.ps1"""
另请参阅:GitHub issue #3028, which requests the ability to launch PowerShell itself completely hidden - obviating the need for an aux. VBScript script - which a future version may support(但不会向后移植到 Windows PowerShell)。
如果您使用 Windows PowerShell(最高版本 v5.1),您必须保存 *.ps1
文件作为具有 BOM 的 UTF-8 ,以便针对 ASCII(7 位)范围之外的字符正确解释它们,例如 äöüß
.
这在 PowerShell [Core] v6+ 中不再是必需的,它始终默认为 UTF-8,但是如果您的脚本需要 运行 在 both 版本,您应该始终使用 UTF-8 和 BOM.
如果给定的 *.ps1
没有 BOM,Windows PowerShell 会解释作为 UTF-8 编码一部分的每个字节 sequence (所有非 ASCII 字符都编码为 2-4 个字节)单独 作为一个字符,基于系统的活动 ANSI 代码页(单字节编码,例如 Windows- 1252).
在美国英语系统中,活动 ANSI 代码页为 Windows-1252,因此上述示例字符串显示为垃圾字符串 äöüß
请注意,问号,或者更准确地说,�
(替换字符,U+FFFD
)的实例,只会出现在 反转 场景中: 当 ANSI 编码的文本被误解为 UTF-8 时。
顺便说一句,重新介绍一下通过管道 (stdin):
向 PowerShell CLI 提供源代码的方法
由于您的脚本显然 运行 是隐藏的,因此它不会对您的情况产生影响,但请注意,此技术展示了伪交互模式,也不支持传递 arguments 通过 stdin 提供的脚本 - 见 GitHub issue #3223
我有一个 ps1 文件,它创建了一个 Link
创建-link.ps1
$path = $env:HOMESHARE + "\My Projects\"
If(!(test-path $path))
{
New-Item -ItemType Directory -Force -Path $path
}
$WshShell = New-Object -comObject WScript.Shell
$Shortcut = $WshShell.CreateShortcut($env:HOMESHARE + "\My Projects\" + "linkname.lnk")
$Shortcut.TargetPath = "\path\for\link"
$Shortcut.Description = "äöüß"
$Shortcut.IconLocation = $env:SYSTEMROOT + "\system32\shell32.dll,3"
$Shortcut.Save()
我也有一个调用 ps1
的 vbs 文件创建-link.vbs
command = "powershell.exe Get-Content ""C:\path\to\file\create-link.ps1"" | PowerShell.exe -noprofile"
set shell = CreateObject("WScript.Shell")
shell.Run command,0
两个文件都使用 utf-8 编码保存。
这个构造是必要的,因为 ps1 需要 运行 完全无头,用户不会注意到任何东西。通过 vbs 调用 ps1 解决了这个问题,如果有更好的方法,请告诉我,我会很高兴。
如果我直接调用 powershell 脚本或使用“powershell.exe Get-Content ""C:\path\to\file\create-link.ps1"" | PowerShell.exe -noprofile" (通过使用 cmd)一切正常。 但是,如果我调用 vbs 来完成它的工作,它通常会工作,但是来自 'Description' 的德语变音符号只是问号,所以不知何故编码被扰乱了。有什么办法可以解决这个问题吗?
tl;博士:
将您的
*.ps1
文件保存为 UTF-8 with BOM.使用 PowerShell CLI 的
-File
参数简化您的命令:
command = "powershell.exe -NoProfile -File ""C:\path\to\file\create-link.ps1"""
另请参阅:GitHub issue #3028, which requests the ability to launch PowerShell itself completely hidden - obviating the need for an aux. VBScript script - which a future version may support(但不会向后移植到 Windows PowerShell)。
如果您使用 Windows PowerShell(最高版本 v5.1),您必须保存 *.ps1
文件作为具有 BOM 的 UTF-8 ,以便针对 ASCII(7 位)范围之外的字符正确解释它们,例如 äöüß
.
这在 PowerShell [Core] v6+ 中不再是必需的,它始终默认为 UTF-8,但是如果您的脚本需要 运行 在 both 版本,您应该始终使用 UTF-8 和 BOM.
如果给定的 *.ps1
没有 BOM,Windows PowerShell 会解释作为 UTF-8 编码一部分的每个字节 sequence (所有非 ASCII 字符都编码为 2-4 个字节)单独 作为一个字符,基于系统的活动 ANSI 代码页(单字节编码,例如 Windows- 1252).
在美国英语系统中,活动 ANSI 代码页为 Windows-1252,因此上述示例字符串显示为垃圾字符串 äöüß
请注意,问号,或者更准确地说,�
(替换字符,U+FFFD
)的实例,只会出现在 反转 场景中: 当 ANSI 编码的文本被误解为 UTF-8 时。
顺便说一句,重新介绍一下通过管道 (stdin):
向 PowerShell CLI 提供源代码的方法由于您的脚本显然 运行 是隐藏的,因此它不会对您的情况产生影响,但请注意,此技术展示了伪交互模式,也不支持传递 arguments 通过 stdin 提供的脚本 - 见 GitHub issue #3223