Skip to content

Error 400000 on Getting Text from PowerShell Variable #9

@dborchers-gc

Description

@dborchers-gc

I totally have no idea what's happening here. Hope someone here can help me. That makes no sense and its frustrating...

I tried to reach the text translation API in PowerShell. This are my Global Variables:

$TranslatorKey = "e54902*******" # You need a valid key for Microsoft Translator
  
$global:BaseUri = "https://api.cognitive.microsofttranslator.com/translate?api-version=3.0"
$global:Headers = @{
    'Ocp-Apim-Subscription-Key' = $translatorKey
    'Ocp-Apim-Subscription-Region' = 'germanywestcentral' # added (change if needed)
    'Content-type' = 'application/json; charset=UTF-8'
}

When i use the Text directly inside my Code it works:

$LanguageCode = "de"
$textJson = @{
    "Text" = "Users may see some meetings scheduled in Mexico time zones are displaying with the incorrect time."
} | ConvertTo-Json
$Body = "[$textJson]"
$Uri = "$($global:BaseUri)&to=$($LanguageCode)"
$Status = Invoke-RestMethod -Method Post -Uri $uri -Headers $global: headers -Body $Body

But if i want to use a Variable with the Text inside, it throws the Error in the Title:

$temp = "Users may see some meetings scheduled in Mexico time zones are displaying with the incorrect time."
$LanguageCode = "de"
$textJson = @{
    "Text" = $temp
} | ConvertTo-Json
$Body = "[$textJson]"

Ive checked absolutely everything and have no idea what's the difference. I really Hope someone can help me.

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions