PowerCLI in an API Endpoint

Product: PowerShell Universal
Version: 2.4.1

Is anyone using PowerCLI in API endpoints? I have another user that is running into a problem where if they call the APIs at the same time, PowerCLI complains that it is no longer connected. I assume it’s because it maintains the connections at a process level and not a runspace level.

So the API psuedocode is something like:

Perform PowerCLI Operations

But what happens is that if 2 APIs are running at the same time one will connect, finish it’s operation and disconnect and then the second API will fail to perform operations because everything disconnected.

Anyone have experience with this?

To rephrase the last part.

Both connect connect to the same server, but when one of them disconnect, both disconnects and the “slowest” running one will fail because it’s now disconnected?

If it’s not the same server. Then Disconnect-VIServer has a -server parameter that I’d guess only disconnects that one server. One might need to configure power lines for multiple sessions though (Set-PowerCLIConfiguration -DefaultVIServerMode Multiple)

And, since my middle name is “workaround”, if it where a limitation of PowerCLI connecting simultaneously to the same server, one may be able to work around it by setting up some cname records for the same server :sweat_smile: one per endpoint maybe?

I have a colleague testing PSU with PowerCLI, I’ll check if I can learn something from it tomorrow.

Yep. That’s correct.

That’s for checking in with them. There are a lot of vCenters involved here so it might take some “management” to work around it.

Hello, I am the one working on this :slight_smile: so we have 13 vcenters and these API’s are running powercli command on 400+ vm across all datacenters. so the powershell connects to all the vcenter (13) searches for the VM finds and different API’s do different tasks. So if one API finishes first then it closes the vcenter connection and kills all the other running API’s as it just disconnected all the vcenters. because of the type of jobs and environment need to connect to all the vcenters at the sametime.

I’ve also opened an issue to track something that might make this easier but definitely not getting in the next version: Isolated API Endpoints · Issue #632 · ironmansoftware/issues · GitHub

Aside from running PS jobs with Start-Job and Wait-Job, you could do similar with Invoke-UAJob -Wait. The bulk of the script would run in a PSU job that would run in its own process.

1 Like

Thanks :blush: !!

Sean Li