To Disconnect(-VIServer) or not to Disconnect
With the release of VI Toolkit v1.0, the bèta-cmdlet Get-VIServer has been renamed to Connect-VIServer, which more accurately describes the action. The opposite action is now also available in the form of Disconnect-VIServer. Every script you start with Connect-VIServer should end with Disconnect-VIServer if you’d like to keep the amount of open sessions to your Virtual Center Server to a minimum. And you do if you like having any sort of performance from your VC Server.
Here’s three ways you can make sure you don’t end up with unnecessary sessions:
1. Find scripts that lack the Disconnect-VIServer command:
$Path = “D:\Scripts”
$ConnectMatches = Get-ChildItem $Path -Recurse |
Where {$_.PSIsContainer -eq $False} |
Select-String “Connect-VIServer” |
Where {$_.Line -notmatch “Disconnect”}
$DisconnectMatches = Get-ChildItem $Path -Recurse |
Where {$_.PSIsContainer -eq $False} |
Select-String “Disconnect-VIServer”
Compare-Object -ReferenceObject $ConnectMatches`
-DifferenceObject $DisconnectMatches -Property Path
2. Disconnect old sessions through the VI Client GUI
On the Administration page of the VI Client, click on the Sessions tab. Sort by Status, Select the sessions that have been idle for a long time and click Terminate.
3. Use a script to disconnect old sessions
That’s more like it! Now who would want to go clicking and sorting about in the VI Client?
Thank goodness we have the VI Toolkit. Here’s the code:
$VCServerName = “YourVCServerName”
$HoursOld = 24 # 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
Write-Host “The VI Toolkit ROCKS!”
Enjoy!
4 Responses to To Disconnect(-VIServer) or not to Disconnect
Tags
Active Directory API bind order cleanup cluster CPU Custom Fields datastores description device management directory tree errors Event Log file name filter Fun function HA IT known issues License Server LUN multipath NIC objects Oneliner portgroups PowerCLI PowerShell profile recursive Registry Scripts security session share snapshots SQL Stat VI Toolkit VMware vSphere WMI WSUS ZenArchives
- July 2012
- July 2011
- February 2011
- January 2011
- December 2010
- May 2010
- October 2009
- September 2009
- August 2009
- July 2009
- June 2009
- May 2009
- April 2009
- March 2009
- February 2009
- January 2009
- December 2008
- November 2008
- October 2008
- September 2008
- August 2008
- July 2008
- June 2008
- May 2008
- April 2008
- March 2008





I see the VI Toolkit and PowerShell are starting to take over your life as with me
Great work, keep it up.
I tried incorporating a session check in the vCheck script from Alan and noticed that the LastActiveTime is in UTC. You can use the ToLocalTime function to convert to the local time.
[...] ^ Credits and thanks for this script goto Hugo Peeters [...]
[...] ^ Credits and thanks for this script goto Hugo Peeters [...]