【问题标题】:Special characters is encoded when run script inside a file Powershell在文件 Powershell 中运行脚本时对特殊字符进行编码
【发布时间】:2020-05-29 01:57:14
【问题描述】:

您好,我有一个脚本可以自动执行一些任务,在 powershell core v.7.+ 中运行。

在其中一个脚本中,当我在 ps1 文件中运行命令并且返回的特殊字符被编码并且我无法解码为正确的格式时,在用于执行此操作的命令下方并返回:

// the variable $script is my command its a ask-cli command to work in alexa
$model = pwsh -Command $script
/* 
* when I running the script from here the special characters returned is these:
* "nächste",
* "nächstgelegene"
*/

但是当我直接在终端中运行相同的命令时,返回的字符串是:

/*
* "nächste",
* "nächstgelegene"
*/

我想知道如何在不编码返回的情况下在文件中运行命令。 我已经尝试了一些方法:

   $encoding = [System.Text.Encoding]::Unicode

    $model = pwsh -Command $script

    Write-Output $model

    $model = $encoding.GetBytes($model)

    $model = $encoding.GetString($model)

但是没有按预期工作,我不知道该怎么做,如果有人可以帮助我,我非常感激。

【问题讨论】:

    标签: powershell character-encoding special-characters powershell-core


    【解决方案1】:

    尝试将字符串作为字节返回,然后从调用函数 pwsh 的位置对其进行解码。这将使其免受任何更改。您正在做的是在接收到它之后将其转换为字节,然后将其返回为字符串。

    【讨论】:

    • 谢谢,我试试看。
    • 我正在这样做,$model = $encoding.GetBytes((pwsh -Command $script)) \ $model = $encoding.GetString($model) 但也不起作用。
    • 这与您在问题上发布的脚本中所做的相同,我的意思是在返回之前将 ($script) 中的字符串转换为字节
    • $script 是要运行的命令,而不是结果本身。运行之后就可以得到结果了。
    • 是的,我明白,我的意思是在运行它之后,它的结果会将其转换为字节。示例: $script = '[Text.Encoding]::Unicode.GetBytes("您想要返回的结果")';在此之后,您将结果解码为 $script
    【解决方案2】:

    在我的脚本下面:

    (Get-Content "$currentPath\skill-package\skill.json" -Raw | ConvertFrom-Json).manifest.publishingInformation.locales.PSObject.Properties | ForEach-Object {
            $lang = $_.Name
            Write-Output "Profile: $profile skill-id: $skillId language: $lang"
    
            $script = "ask smapi get-interaction-model -l $lang -s $skillId -p $profile -g $env"
    
            Write-Output 'Running script'
    
            Write-Warning $script
    
            # $encoding = [System.Text.Encoding]::ASCII
    
            $model = pwsh -Command $script
    
            Write-Output $model
    
            $model = $model
            | ConvertFrom-Json -Depth 100 
            | Select-Object * -ExcludeProperty version 
            | ConvertTo-Json -Depth 100
    
            # out-file "$file$lang.json" -InputObject $model -Encoding ascii
    
            Write-Output "New model saved locally $file$lang.json"
        }
        Write-Warning 'Finished!!!'
    

    【讨论】:

      【解决方案3】:
      (Get-Content "$currentPath\skill-package\skill.json" -Raw | ConvertFrom-Json).manifest.publishingInformation.locales.PSObject.Properties | ForEach-Object {
          $lang = $_.Name
          Write-Output "Profile: $profile skill-id: $skillId language: $lang"
      
          $script = "`$command = ask smapi get-interaction-model -l $lang -s $skillId -p $profile -g $env;`$command = [Convert]::ToBase64String([Text.Encoding]::Unicode.GetBytes(Invoke-Expression `$command));`$command"
      
          Write-Output 'Running script'
      
          Write-Warning $script
      
          # $encoding = [System.Text.Encoding]::ASCII
      
          $model = pwsh -Command $script
      
          $model = Text.Encoding::Unicode.GetString([Convert]::FromBase64String($model))
      
          Write-Output $model
      
          $model = $model
          | ConvertFrom-Json -Depth 100 
          | Select-Object * -ExcludeProperty version 
          | ConvertTo-Json -Depth 100
      
          # out-file "$file$lang.json" -InputObject $model -Encoding ascii
      
          Write-Output "New model saved locally $file$lang.json"
      }
      Write-Warning 'Finished!!!'
      

      【讨论】:

      • 请试试这个
      • 请修正这一行:$model = Text.Encoding::Unicode.GetString([Convert]::FromBase64String($model)).. 将方括号添加到 [Text.Encoding]。不小心忘记了
      • 我尝试按照您发送给我的方式运行此命令,但始终返回错误,因为此 Invoke-Expression $command` 需要在括号内,但即使在括号内,结果也不是预期的。我尝试以不同的方式使用您的示例,但直到现在我还没有取得太大的成功。但是非常感谢你能帮我解决这个问题,如果你知道我愿意尝试的其他方式:)
      【解决方案4】:

      经过多次研究,我可以找到更容易解决我的问题的方法。 Powershell 在这些情况下默认使用不同的输出编码器,我唯一需要做的就是改变它。

      我使用了命令:

      $OutputEncoding = [console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding
      

      我发现这个问题解释了它是如何工作的,这很有帮助,更多问题请查看answer

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-12-31
        • 1970-01-01
        • 2011-12-20
        • 1970-01-01
        • 2012-08-06
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多