VMware PowerCLI

The Best User Interface for your VMware Datacenter!

Allow disconnect-viserver to specifiy a specific session to disconnect

Per the disconnect-viserver documentation:

By default, Disconnect-VIServer closes only the last connection to the specified server.

If you have multiple connections to the same vCenter, not being able to specify which session to disconnect is problematic if you have multiple commands running independently. For example, if you have two connections to a vCenter, and the first connection is running a command or series of steps that finishes before the commands running against the second connection, if you disconnet-viserver, it will actually disconnect the session that is still actively running commands against the vCenter. Allowing disconnect-viserver to designate a specific session will avoid the need to track the sessions with additional code.

  • Zachary Zumbaugh
  • Nov 16 2021
  • Already exists
  • Attach files
  • Zachary Zumbaugh commented
    17 Nov, 2021 04:23pm

    Thanks for the clarification, Kamen. I think that functionality is sufficient to address the concerns I had with disconnecting the wrong session.

  • Kamen Nikolov commented
    17 Nov, 2021 08:26am

    When you connect to the same server several times (assuming that you connect with the same username) no new connection is made, but the initial connection is reused. There is a RefCount property in the connection object that increments every time you connect to the same server again and decrements when you disconnect. The connection is not actually disconnected until you disconnect as many times as you have connected or you disconnect with the force parameter. If you connect with a different user then this is considered a separate connection. Connect-VIServer returns the session as an object and you can save that in a variable and then control which session gets disconnected like this:

    $conn1 = Connect-VIServer <server address> -user <username> -password <password>

    $conn2 = Connect-VIServer <server address> -user <username> -password <password>


    Disconnect-VIServer $conn1