VMware

PowerCLI 5.0 – Better than ever before | Main | PowerCLI at VMworld 2011 Copenhagen

September 27, 2011

List and Disconnect vCenter Sessions

One of the first cmdlets people learn when starting out in PowerCLI is Connect-VIServer, this as you probably know allows you to connect to a vCenter server or a ESX/ESXi Host directly, this creates a session on the vCenter server until it has been disconnected using the Disconnect-VIServer cmdlet.

But who uses the Disconnect-VIServer cmdlet ? Come on truthfully, do you use it at the end of each script or PowerCLI session you use ?

This is obviously best practice but in my travels and from the code examples I have seen on the internet people seam to miss this out, on the face of it this is not an issue, it has “no impact” that you can see, but wait……

If you are in vCenter and go to the Sessions screen you will see that our sessions which were opened are now in an idle state:

image_thumb4

So why do we care ?

Did you know there is actually a limit on the amount of sessions which can connect to vCenter, this is set at 500 concurrent session, both Idle and active sessions count.

You would be surprised how quickly this can fill up in a corporate environment, especially if you have multiple scripts which run as scheduled tasks and do not disconnect correctly.

This was the case at a recent customer site, they needed a quick way out of this situation to enable them the time to go back and adjust the multiple scripts they had connecting and not disconnecting and the multiple staff they had who would RDP to a server and leave the vCenter client running and connected.

The following two PowerShell Advanced Functions were written to help with this situation:

List all sessions with PowerCLI

Function Get-ViSession {
    <#
        .SYNOPSIS
            Lists vCenter Sessions.

        .DESCRIPTION
            Lists all connected vCenter Sessions.

        .EXAMPLE
            PS C:\> Get-VISession

        .EXAMPLE
            PS C:\> Get-VISession | Where { $_.IdleMinutes -gt 5 }
    #>
    $SessionMgr = Get-View $DefaultViserver.ExtensionData.Client.ServiceContent.SessionManager
    $AllSessions = @()
    $SessionMgr.SessionList | Foreach {   
        $Session = New-Object -TypeName PSObject -Property @{
            Key = $_.Key
            UserName = $_.UserName
            FullName = $_.FullName
            LoginTime = ($_.LoginTime).ToLocalTime()
            LastActiveTime = ($_.LastActiveTime).ToLocalTime()
           
        }
            If ($_.Key -eq $SessionMgr.CurrentSession.Key) {
                $Session | Add-Member -MemberType NoteProperty -Name Status -Value "Current Session"
            } Else {
                $Session | Add-Member -MemberType NoteProperty -Name Status -Value "Idle"
            }
            $Session | Add-Member -MemberType NoteProperty -Name IdleMinutes -Value ([Math]::Round(((Get-Date) - ($_.LastActiveTime).ToLocalTime()).TotalMinutes))
    $AllSessions += $Session
    }
    $AllSessions
}

 

When this function is run it will give you a list of the connected sessions and various useful properties:

SNAGHTML366d1b1_thumb2

This can obviously be filtered using the standard PowerShell cmdlets to list any sessions which have been idle for over 30 minutes:

SNAGHTML367dc9a_thumb2

A further function can be used to take the input from Get-VISession and disconnect these sessions where needed:

Disconnect sessions with PowerCLI

Function Disconnect-ViSession {
    <#
        .SYNOPSIS
            Disconnects a connected vCenter Session.

        .DESCRIPTION
            Disconnects a open connected vCenter Session.

        .PARAMETER  SessionList
            A session or a list of sessions to disconnect.

        .EXAMPLE
            PS C:\> Get-VISession | Where { $_.IdleMinutes -gt 5 } | Disconnect-ViSession

        .EXAMPLE
            PS C:\> Get-VISession | Where { $_.Username -eq "User19" } | Disconnect-ViSession
    #>
    [CmdletBinding()]
    Param (
        [Parameter(ValueFromPipeline=$true)]
        $SessionList
    )
    Process {
        $SessionMgr = Get-View $DefaultViserver.ExtensionData.Client.ServiceContent.SessionManager
        $SessionList | Foreach {
            Write "Disconnecting Session for $($_.Username) which has been active since $($_.LoginTime)"
            $SessionMgr.TerminateSession($_.Key)
        }
    }
}

The following example shows how to use these functions to disconnect any sessions with over 30 minutes of idle time:

SNAGHTML36c8019_thumb2

Both of these functions show that even if there is not a built in cmdlet for a certain feature in PowerCLI there is always an easy way to create your own cmdlet like functions to work the way you need them.

Alan Renouf

Technical Marketing

TrackBack

TrackBack URL for this entry:
http://www.typepad.com/services/trackback/6a00d8341c328153ef014e8bdb102e970d

Listed below are links to weblogs that reference List and Disconnect vCenter Sessions:

Comments

Jake

Nice post Alan!

You could simplify getting your session with (Get-View -id SessionManager)

Cheers,

Jake

Jake

Well, I guess that doesn't take into account multi VI sessions. Disregard. :D

Robert van den Nieuwendijk

It would be nice if PowerCLI would disconnect all the sessions, if you close the console. Can I put this as a feature request for a next PowerCLI version?

Daniel Pfuhl

I'm looking for a possibility to get to know the source IPs where the clients are connecting from. Any idea?

BTW: Does that function also run on 4.1U1? I have some trouble...

Post a comment

If you have a TypeKey or TypePad account, please Sign In.

About this Blog

This blog provides insight and highlights for VMware PowerCLI, the best tool for automating management and configuration of VMware vSphere.

Subscribe via RSS  

Recommended Reading

Pimp your PowerCLI

Twitter


Facebook

    VMware Blogs