German Umlauts in Powershell called by vbs

后端 未结 1 864
刺人心
刺人心 2021-01-15 05:09

i do have a ps1 file, which create a Link

create-link.ps1

$path = $env:HOMESHARE + "\\My Projects\\"
If(!(test-path $path))
         


        
相关标签:
1条回答
  • 2021-01-15 05:44

    tl;dr:

    • Save your *.ps1 file as UTF-8 with BOM.

    • Simplify your command by using the PowerShell CLI's -File parameter:

    command = "powershell.exe -NoProfile -File ""C:\path\to\file\create-link.ps1"""
    

    See also: 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 (but it won't be back-ported to Windows PowerShell).


    If you're using Windows PowerShell (versions up to v5.1), you must save your *.ps1 files as UTF-8 with a BOM in order for them to be interpreted correctly with respect to characters outside the ASCII (7-bit) range, such as äöüß.
    This is no longer necessary in PowerShell [Core] v6+, which consistently defaults to UTF-8, but if your scripts need to run in both editions, you should always use UTF-8 with BOM.

    If a given *.ps1 doesn't have a BOM, Windows PowerShell interprets each byte that is part of an UTF-8 encoding sequence (all non-ASCII characters are encoded as 2-4 bytes) individually as a character, based on the system's active ANSI code page (a single-byte encoding such as Windows-1252).

    On a US-English system, where the active ANSI code page is Windows-1252, the above sample string therefore surfaces as garbage string äöüß

    Note that question marks, or, more accurately, instances of (REPLACEMENT CHARACTER, U+FFFD), would only surface in the reverse scenario: when ANSI-encoded text is misinterpreted as UTF-8.


    As an aside, re your approach of providing the source code to the PowerShell CLI via the pipeline (stdin):

    Since your script apparently runs hidden, it won't make a difference in your case, but note that this technique exhibits pseudo-interactive mode and also doesn't support passing arguments to the script being provided via stdin - see GitHub issue #3223

    0 讨论(0)
提交回复
热议问题