0

Hi I have a script to automate some tasks, running in powershell core v.7.+.

In one of these scripts when I run the command inside the ps1 file and the special characters returned is encoded and I can't decode to right format, below the command used to do this and the 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"
*/

But when I run the same command directly in the terminal the strings returned are:

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

I would like to know how can I run the command inside the file without encode the return. I already tried some things as:

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

    $model = pwsh -Command $script

    Write-Output $model

    $model = $encoding.GetBytes($model)

    $model = $encoding.GetString($model)

But don't work as expected, I don't know more how I can to this, if someone could help me with this I appreciate too much.

4 Answers4

0

Try returning the string as bytes and then decode it from the place you are calling the function pwsh. This would preserve it from any changes. What you're doing is converting it into bytes after receiving it then returning it to string.

0

Below my script:

(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!!!'
0
(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!!!'
  • Please try this –  May 27 '20 at 18:00
  • Please fix this line: $model = Text.Encoding::Unicode.GetString([Convert]::FromBase64String($model)).. Add square brackets to [Text.Encoding]. Forgot them by mistake –  May 27 '20 at 18:25
  • I tried run this command as you sent to me but always return an error because this `Invoke-Expression `$command`, this need be inside a brackets, but even inside brackets the result is not the expected. I tried use your example in different ways but until now I not have much success. But thank you a lot for you availability to help me with this, if you know some other way I'm open to try :) – CuriousGuyFromFarWayGalaxy May 27 '20 at 23:30
0

After many researches, I could find something more easiest to solve my problem. Powershell has by default a different output encoder used in these cases, and the only thing I need to do it's change it.

I used the command:

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

I find this question explaining how this work and this help a lot, for more question please check this answer.