jsonpowershellpowercliinvoke-restmethod

Powershell/PowerCLI, Horizon REST-API, invoke-restmethod returns "BAD_REQUEST The input request cannot be parsed"


I'm trying to copy Horizon application pools that exist on a source Horizon connection server (HCS) to another one. In my homelab that works perfectly, in another environment on "invoke-restmethod" I run into the error above.

First I get an auth token and the applications on the source HCS:

$horizonApps_source = Invoke-RestMethod -Method Get -uri "$RESTurl_source/rest/inventory/v2/application-pools" -ContentType "application/json" -Headers (Get-HRHeader -accessToken $accessToken_source)`

Then I loop through them and create the copies on the target HCS (also exporting a .json-File for every app and removing unique values that the POST "inventory/v2/application-pools" will not accept):

If ($horizonApps_source -ne $null) {
    ForEach ($item in $horizonApps_source) {
        $jsonFile = $fileLoc + $item.Display_Name + ".json"
        $item = $item | Select-Object * -ExcludeProperty Id, access_group_id, icon_ids, customized_icon_ids
        $item.farm_id = $farmID_target
        $item | ConvertTo-Json -Depth 100 | Out-File $jsonFile
        $appJson = $item | ConvertTo-Json -Depth 100
        $app_target = Invoke-RestMethod -Method Post -uri "$RESTurl_target/rest/inventory/v2/application-pools" -ContentType "application/json" -Headers (Get-HRHeader -accessToken $accessToken_target) -body $appJson -SkipCertificateCheck
    }
}

In one environment everything works, in another no chance...this is the error I get, .json-files are written and look okay, but no apps are created on the target HCS:

Line |
   8 |  … pp_target = Invoke-RestMethod -Method Post -uri "$RESTurl_target/rest …
     |                ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
     | {"status":"BAD_REQUEST","timestamp":1670869477302,"error_message":"The input request cannot be parsed."}

Powershell 7.3.0, PowerCLI 13, Horizon 2111 everywhere. Any ideas? Any suggestion how I could catch the error in more detail to find the issue's source?

Tried to google problems with invoke-restmethod using a json-body. Sadly I'm not a Powershell pro...


Solution

  • Solved with help of a great colleague!

    In my test enwironment I used two Horizon installations on Windows Server 2022 in an ENGLISH install. The other environment uses a source Horizon installation on a GERMAN server OS, but the target Horizon connection server runs on an ENGLISH OS. on the German based source install the admins made use of German special characters "äÄöÖüÜ".

    In result the character set hast to be fixed to UTF-8 for the REST invocation:

    $app_target = Invoke-RestMethod -Method Post -uri "$RESTurl_target/rest/inventory/v2/application-pools" -ContentType "application/json; charset=utf-8" -Headers (Get-HRHeader -accessToken $accessToken_target) -body $appJson
    

    The part "application/json" has to become "application/json; charset=utf-8".

    And that's it.