0

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...

  • Is the server the same machine? Most cases like this the code works as localhost but fails on a server like IIS. The issue is you do not have access to the machine resources on the IIS server (or equivalent). A client connects default connection is Guest where you do not have access to the file system on IIS. Solution is to do storage on a Network drive where client have access. To get more info check the Event Viewer on Server to see errors. – jdweng Dec 13 '22 at 11:21
  • No, not the same server and auth is working (checked). – ChrisTheTerror Dec 14 '22 at 08:47

1 Answers1

0

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.

  • This is just another case where the HTTP Header(s) have to change. The server has default HTTP headers and in this case you had to modify the character set to be UTF-8. When you have a German Client everything would work but if you have an English client it doesn't work. – jdweng Dec 14 '22 at 11:09