VMware Cloud Community
sujeetkp
Contributor
Contributor
Jump to solution

PowerCLI Session Connect and Disconnect

I have a below queries on PowerCLI Sessions.

1. What happens if I open a sesison with Connect-VIServer and dont close the session explicitly before exiting out of the script ?

    a. Does it close the session as soon as the PowerShell Sesison ends ? (Which I believe, it doesn't because I can see my idle/active session in the vsphere client)

    b. Session remains open. If yes, then till what time period and how can we control it.

        Which leads to another query, how can we control no.of sessions per user ?

2. "Disconnect-VIServer" --> Does it close only the active session in that particular PowerShell Session ? Or We can close any other Session with the help of SessionID remotely.

3. If we are connecting to a Session with the help of Session ID (From the Same or a Different Server), is this counted as another session for that user.

    Is there a limit on how many extra sessions we can open using this command for a user ? If yes how can we control it ?

   Connect-VIServer -Session "xxxxxxxxxxxxxxxxxxxxxxxxxx" -Server "xxxxxxxxx"

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Let me clarify that a bit.
There are 2 sessions:

- session A which was created with a full Connect-VIServer

- session B which was created with a Connect-VIServer with the Session parameter

You can do a Disconnect-VIServer in session B.

The $global:defaultVIServer variable will be empty.
Session A is still active and you are able to run PowerCLI cmdlets.

When you, again starting with both open, close session A with a Disconnect-VIServer, the session on A will be closed.

And the $global:defaultVIServer is empty.

Session B, in that case, will still have values in its $global:defaultVIServer, but there is no session behind it.

When you run a PowerCLI cmdlet, like for example Get-VM, nothing will be returned.

In theory, it is sufficient to close session A, which will also close session B.

But the $global:defaultVIServer variable in session B will contain values that might make you think the session is still open and working.

You can always close a 'pending' session (PS session closed without doing a Disconnect-VIServer), by calling the TerminateSession method.

See for example Re: Terminate Idle VI Client Sessions with PowerCLI Script

But to close one specific session, you have to make sure to close the correct one.

There are also a number of internal and Web Client session that you better not close this way.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

View solution in original post

5 Replies
sujeetkp
Contributor
Contributor
Jump to solution

LucD​ : Can you please check this, If you have sometime.

0 Kudos
LucD
Leadership
Leadership
Jump to solution

1. On the vCenter that session stays active, until the timeout occurs.

Since vSphere 6.* the maximum concurrent sessions is 2000. Before vSphere 6.* is used to be 500.

Afaik you can't control the number of sessions per user.
A session times out after 120 minutes.

The timeout value can be configured, see Configure the vSphere Web Client Timeout Value

2. Yes, only the session you opened in that PS session.
With the exception for sessions that were opened with a Session parameter.

If you close the original, the ones opened with the Session key will also be (somewhat) closed.

Meaning, no PowerCLI cmdlet that requires a session will work anymore. Although the $global:defaultviserver variable will still show a session.

3. No, that is the same session.

Afaik there is no configurable limit for this.

You can easily list the current sessions, see Get Complete VCenter Session Info

Be aware that the Get-VIEvent used in that function might run for a long time.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

sujeetkp
Contributor
Contributor
Jump to solution

LucD​ : Just to clarify... With respect to your answer for Point 2

A session opened with "Session" parameter can't be closed with "Disconnect-VIServer" command.

Original Session needs to be closed inorder to close all the sessions opened with "Session" parameter for that particular session.

In case the PS session is closed without disconnecting, where the original session was alive, is there a mechanism to close the Original session explicitly

1. From another New PS Session with New PowerCLI session

2. From the PS session connected to the Original Session with "Session" Parameter

0 Kudos
LucD
Leadership
Leadership
Jump to solution

Let me clarify that a bit.
There are 2 sessions:

- session A which was created with a full Connect-VIServer

- session B which was created with a Connect-VIServer with the Session parameter

You can do a Disconnect-VIServer in session B.

The $global:defaultVIServer variable will be empty.
Session A is still active and you are able to run PowerCLI cmdlets.

When you, again starting with both open, close session A with a Disconnect-VIServer, the session on A will be closed.

And the $global:defaultVIServer is empty.

Session B, in that case, will still have values in its $global:defaultVIServer, but there is no session behind it.

When you run a PowerCLI cmdlet, like for example Get-VM, nothing will be returned.

In theory, it is sufficient to close session A, which will also close session B.

But the $global:defaultVIServer variable in session B will contain values that might make you think the session is still open and working.

You can always close a 'pending' session (PS session closed without doing a Disconnect-VIServer), by calling the TerminateSession method.

See for example Re: Terminate Idle VI Client Sessions with PowerCLI Script

But to close one specific session, you have to make sure to close the correct one.

There are also a number of internal and Web Client session that you better not close this way.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

sujeetkp
Contributor
Contributor
Jump to solution

Thanks a lot @LucD for the quick response. It is really helpful information. Smiley Happy

0 Kudos