当文件 Powershell 中的 运行 脚本时对特殊字符进行编码
Special characters is encoded when run script inside a file Powershell
你好我有一个脚本可以自动执行一些任务,运行在 powershell core v.7.+.
中
在其中一个脚本中,当我 运行 ps1 文件中的命令和特殊字符 returned 被编码时,我无法解码为正确的格式,如下所示用于执行此操作的命令和 return:
// 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"
*/
但是当我 运行 直接在终端中执行相同的命令时,字符串 returned 是:
/*
* "nächste",
* "nächstgelegene"
*/
我想知道如何在不对 return 进行编码的情况下 运行 文件中的命令。
我已经尝试了一些东西:
$encoding = [System.Text.Encoding]::Unicode
$model = pwsh -Command $script
Write-Output $model
$model = $encoding.GetBytes($model)
$model = $encoding.GetString($model)
但是没有按预期工作,我不知道该怎么办,如果有人能帮助我,我将不胜感激。
尝试以字节形式返回字符串,然后从调用函数 pwsh 的位置对其进行解码。这将使它免受任何更改。您正在做的是在接收到它之后将其转换为字节,然后将其返回为字符串。
下面是我的脚本:
(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!!!'
(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!!!'
经过多次研究,我找到了更简单的方法来解决我的问题。
默认情况下,Powershell 在这些情况下使用不同的输出编码器,我唯一需要做的就是更改它。
我使用了命令:
$OutputEncoding = [console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding
我发现这个问题解释了它是如何工作的,这很有帮助,如需更多问题,请查看此 。
你好我有一个脚本可以自动执行一些任务,运行在 powershell core v.7.+.
中在其中一个脚本中,当我 运行 ps1 文件中的命令和特殊字符 returned 被编码时,我无法解码为正确的格式,如下所示用于执行此操作的命令和 return:
// 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"
*/
但是当我 运行 直接在终端中执行相同的命令时,字符串 returned 是:
/*
* "nächste",
* "nächstgelegene"
*/
我想知道如何在不对 return 进行编码的情况下 运行 文件中的命令。 我已经尝试了一些东西:
$encoding = [System.Text.Encoding]::Unicode
$model = pwsh -Command $script
Write-Output $model
$model = $encoding.GetBytes($model)
$model = $encoding.GetString($model)
但是没有按预期工作,我不知道该怎么办,如果有人能帮助我,我将不胜感激。
尝试以字节形式返回字符串,然后从调用函数 pwsh 的位置对其进行解码。这将使它免受任何更改。您正在做的是在接收到它之后将其转换为字节,然后将其返回为字符串。
下面是我的脚本:
(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!!!'
(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!!!'
经过多次研究,我找到了更简单的方法来解决我的问题。 默认情况下,Powershell 在这些情况下使用不同的输出编码器,我唯一需要做的就是更改它。
我使用了命令:
$OutputEncoding = [console]::InputEncoding = [console]::OutputEncoding = New-Object System.Text.UTF8Encoding
我发现这个问题解释了它是如何工作的,这很有帮助,如需更多问题,请查看此