Home > Blogs > VMware PowerCLI Blog > Monthly Archives: September 2011

Monthly Archives: September 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:


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 100 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 {
Lists vCenter Sessions.

Lists all connected vCenter Sessions.

PS C:\> Get-VISession

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


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


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


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 {
Disconnects a connected vCenter Session.

Disconnects a open connected vCenter Session.

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

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

PS C:\> Get-VISession | Where { $_.Username -eq “User19” } | Disconnect-ViSession
Param (
Process {
$SessionMgr = Get-View $DefaultViserver.ExtensionData.Client.ServiceContent.SessionManager
$SessionList | Foreach {
Write “Disconnecting Session for $($_.Username) which has been active since $($_.LoginTime)”

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


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

Post updated: 3/6/2015 to correct number of supported sessions to 100.

PowerCLI 5.0 – Better than ever before

With the recent release of PowerCLI 5.0 there have been some great new enhancements, not only to the core vSphere management snap-in but also to other areas of the 5.0 release. As you would expect PowerCLI has been updated to work with the latest 5.0 version of vSphere whilst still maintaining its support for previous versions – Now you can automate the build of your Monster VMs !

Core enhancements

As with previous releases various performance benefits have been added to the latest release of PowerCLI and additional functionality has been enabled to further expand the reach of PowerCLI, some of these include:

  • Cloning templates
  • Copying files and folders from and to the guest OS
  • Joining domains
  • Joining ESX hosts (version 4.1 and later) into an active directory
  • Retrieving vCenter Server users and groups
  • Support for vCenter Servers in linked mode
  • Moving virtual appliances
  • Importing compressed and chunked files
  • Importing and exporting virtual appliances in OVA format
  • Support for the SSPI passthrough feature of VIX
  • Support for storage DRS
  • Support for creating VMs and hard disks on datastore clusters
  • Support for defining anti-affinity rules

For more information on the new core cmdlets please refer to the PowerCLI Change Log located here.

Additional Snap-ins

image_thumb12Additional to the core snap-in which allows you to manage vSphere, when you install PowerCLI you will now see multiple new snap-ins, these include the ability to use PowerShell to manage the following:

VMware Licensing

A snap-in has created which will add addition cmdlets to PowerCLI, enabling you to find information on the current license information.

Cmdlet Overview:

Name Description
Get-LicenseDataManager Returns the vSphere LicenseDataManager objects for the specified vSphere servers.

VMware Image BuilderSNAGHTML66b56a4_thumb2

This snap-in can be used to manipulate the core ESXi Image, it can be used to add drivers, CIM providers and Other components to the base ESXi image and then export this as a custom built ISO ready for creating the installable media or also export to a Software Depot ready for use with VMware Auto Deploy.

Cmdlet Overview:

Name Description
Add-EsxSoftwareDepot Adds an ESX software depot or offline depot ZIP file to the current PowerCLI session.
Add-EsxSoftwarePackage Adds new VIBs to an image profile or updates existing VIBs in an image profile.
Compare-EsxImageProfile Returns an ImageProfileDiff object that specifies whether two profiles have the same VIB list and acceptance level.
Export-EsxImageProfile Exports an Image Profile object as either an ESXi ISO image that can be booted up and used as an ESXi installer, or
an offline depot ZIP file that contains metadata plus the VIB packages.
Get-EsxImageProfile Lists the image profiles from software depots as well as image profiles created by the user.
Get-EsxSoftwarePackage Returns a list of SoftwarePackage (VIB) objects from all the connected depots.
New-EsxImageProfile Creates an image profile on the client machine, either by cloning or from scratch.
Remove-EsxSoftwareDepot Disconnects the current PowerCLI session from the specified software depots.
Remove-EsxSoftwarePackage Removes existing packages from an image profile.
Set-EsxImageProfile Used to modify a user-created image profile.

VMware Auto DeploySNAGHTML6415f5a_thumb4

This snap-in gives us over 15 new cmdlets to work with VMware Auto Deploy,

The VMware Auto Deploy snap-in allows VMware customers to use a rule based engine to deploy ESXi to bare metal hosts.

Cmdlet Overview:

Name Synopsis
Add-DeployRule Adds one or more rules to the rule set.
Apply-ESXImageProfile Associates the specified image profile with the specified ESXi system.
Copy-DeployRule Clones an existing rule.
Get-DeployRule Gets a DeployRule object.
Get-DeployRuleSet Gets the current working rule set or the current active rule set.
Get-VMHostAttributes Gets the identifying attributes of a host.
Get-VMHostImageProfile Gets the image profile associated with the specified ESXi host.
Get-VMHostMatchingRules Retrieves the list of rules in the rule set that match a specified host.
New-DeployRule Creates a new rule.
Remove-DeployRule Removes a rule from the working rule set.
Repair-DeployImageCache Repairs the image cache. Use this cmdlet only when working with
VMware Technical Support.
Repair-DeployRuleSetCompliance Remediate any non-compliant associations discovered by
Set-DeployRule Updates an existing rule.
Set-DeployRuleSet Sets the list of rules in the working rule set.
Switch-ActiveDeployRuleSet Activates a rule set.
Test-DeployRuleSetCompliance Checks whether hosts are compliant with rules.


The latest version of PowerCLI can be downloaded from here:


PowerCLI Poster 5.0

PowerCLi SnapinsWith the release of vSphere 5.0 and also the introduction of new PowerCLI snap-ins it is time for an updated poster.

If you attended VMworld 2011 Las Vegas then you may have already been given one of these at the many PowerCLI sessions or during the hands on labs.

If you were not lucky enough to get one then please head over to the PowerCLI Community and download it from this post here in PDF format.

The new poster adds to the original vSphere PowerCLI core cmdlets and allow you to quickly reference cmdlets from the following :

  • vSphere
  • Image Builder
  • Auto Deploy
  • Update Manager
  • Licensing
  • View
  • vCloud

Make sure you download the poster from here.