new1234.jpg
Home > VMware > VMware: Disconnect idling vCenter sessions with PowerCLI

VMware: Disconnect idling vCenter sessions with PowerCLI

Today I was browsing my vCenter server and saw multiple vCenter idle sessions connected to the vCenter server. I was thinking.. there must be a PowerCLI script made by a CLI freak that will disconnect my old sessions.. and there is ๐Ÿ™‚

 image

With this Powershell script you get an outputh of the connected vCenter users:

$svcRef = new-object VMware.Vim.ManagedObjectReference
$svcRef.Type = "ServiceInstance"
$svcRef.Value = "ServiceInstance"
$serviceInstance = get-view $svcRef
$sessMgr = get-view $serviceInstance.Content.sessionManager
foreach ($sess in $sessMgr.SessionList){if (($sess.LastActiveTime).addminutes(60) -lt (Get-Date)){write "$($sess.UserName)"}
}

 

image

^ Credits and thanks for this script goto Anders Mikkelsen

To disconnect the idling sessions (12h +) you can run the following script:

  

$VCServerName = โ€œYourVCServerNameโ€
$HoursOld = 12        # Modify value at your pleasure
$VC = Connect-VIServer $VCServerName
$ServiceInstance = Get-View ServiceInstance
$SessionManager = Get-View $ServiceInstance.Content.SessionManager
$SessionManager.SessionList |
   Where {$_.LastActiveTime -lt (Get-Date).AddHours(-$HoursOld)} |
   % {$SessionManager.TerminateSession($_.Key)}
Disconnect-VIServer -Confirm:$False

$VCServerName = FQDN

$HoursOld = Max hours to kill the idling sessions ( I prefer 12 hours)

image

^ Credits and thanks for this script goto Hugo Peeters

 

Now my connected sessions are:

image

Time to schedule the disconnect script every day and slap some colleagues to close their sessions ๐Ÿ™‚

sanderdaems

Sander Daems is founder and author of this blog and working as a Sr. Infrastructure Consultant by IT-Value. Sander has over 10 years experience in IT, primary focus: virtualization, storage and SBC

More Posts - Website

Follow Me:
TwitterLinkedIn

  1. October 4th, 2010  (Quote) at 07:53  (Quote) | #1

    Glad to help ๐Ÿ™‚

  2. October 14th, 2010  (Quote) at 09:31  (Quote) | #2

    I am sorry, that I interfere, but, in my opinion, there is other way of the decision of a question.

    Greetings from the Speedy DNS

  3. Sketch
    December 29th, 2011  (Quote) at 13:48  (Quote) | #3

    of course, I don’t find this until 29-Dec:

    FYI: http://communities.vmware.com/thread/294736?tstart=0

    “The bug is time related and only appears in December. In result, it will completely disappear on 1st of January and will reappear on 1st of December, 2011. However we expect that by December 2011 new, better release(s) will be out and 4.1.1 will not be widely used.”

  4. Mitrajeet
    May 2nd, 2013  (Quote) at 06:40  (Quote) | #4

    it is disconnecting all my Active/IDLE sessions …
    i want to disconnect only IDLE one ..

  5. Jonathan
    February 10th, 2016  (Quote) at 02:49  (Quote) | #5

    Nice one, also scheduled ๐Ÿ™‚

  1. at | #1
  2. at | #2
  3. at | #3