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 🙂
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)"} }
^ 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)
^ Credits and thanks for this script goto Hugo Peeters
Now my connected sessions are:
Time to schedule the disconnect script every day and slap some colleagues to close their sessions 🙂
Glad to help 🙂
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
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.”
it is disconnecting all my Active/IDLE sessions …
i want to disconnect only IDLE one ..
Nice one, also scheduled 🙂