New-PSUApiResponse unexpected behavior (with VMware PowerCLI)

Product: PowerShell Universal
Version: 1.5.11

I have written a short endpoint, which gets some data from virtual machines in vCenter (using PowerCLI).

Connect-VIServer vcenter.domain.local 
$result = get-VM vms* | Select Name,NumCPU,MemoryGB | ConvertTo-Json -Compress
New-PSUApiResponse -Body $result -StatusCode 200 -ContentType 'application/json'

On the client side I call the endpoint by Invoke-RestMethod …

The result has 2 elements. The first seems to come from Connect-VIServer, the second is the expected result.

I’ve found a workaround:

Connect-viserver vcenter.domain.local  | Out-Null

Is this behavior expected? Is there any documentation what objects are passed by New-PSUApiResponse - in addition to the Body-parameter?

Thanks
Roland

This is expected behavior. APIs will return whatever is written to the pipeline. If Connect-VIServer returns an object, that object will be returned along with the New-PSUAPIResponse. New-PSUAPIResponse will return an object and if it’s the only object returned it will use that for customizing the response.

You will encounter more issues, this is because of runspace.

What I have done is that I invoke-script instead from the dashboard and run the vcenter in the script sections.

This don’t effect the horizon module.

Here is my thread about it.

Also include links to VMware

It is not related to a Dashboard, but an API endpoint instead. Adam reported back, that the behavior is expected - and so I will continue to use | Out-Null to get rid of the output of Connect-VIServer.

Thanks

So New-PSUApiResponse doesn’t actually return the data immediatly, but “only” builds the object, that will be returned at the end of the endpoint? And because the result of New-PSUApiResponse is not written to a variable or Out-Null the result gets to the output pipeline. Have I got it right, now?

Thanks
Roland

Correct. We don’t exit early from the script block to return a result since only a single response can be returned. We wait until the scriptblock has executed entirely.