Get and Disconnect VCenter sessions w/PowerCLi

Cover

Are you sure you know who connected to your VCenter(s)? From where and when the sessions were established? How long sessions have been idle? What user agents are used (Web Client/PowerCLi, etc.) to connect and many other usable info?

This time we will look at just two functions Get-ViSession/Disconnect-ViSession from my PowerCLi Vi-Module module.

Many years ago Alan Renouf already wrote these functions, but it was POC only to show us the technology. My versions more robust and contain many improvements (multiple VCenter support and [ViSession] data type are only some of them).

Get-ViSession

  • All you need is connect to your VC and run the Get-ViSession function.
Connect-VIServer VC1, PSC1, VCSA2 -wa SilentlyContinue
Get-ViSession
Get-ViSession | ft
Get-ViSession | ft –au

01.Get-ViSession_FormatOutput

Returned objects

  • By default, the function returns necessary properties only. If you want to get the full object, format the output with Format-List cmdlet or use Select-Object.
Get-ViSession | fl
Get-ViSession | select *

02.Get-ViSession_GetMember

  • Pay attention to the Client property. The function tries to resolve client IP address by nslookup and if the address cannot be resolved, the property will contain IP instead of hostname.

  • The ClientType property is what user agent used to establish connection. The Legacy is C# client. The Internal client usually used by different VMware solutions like vROPs or vRA. The VDP sessions will have Unknown client type.

Parameters

There are three options to filter the function output by one of available parameters.

  • Get sessions for users that match the -UserName parameter’s value. This is positional parameter and may be omitted.
Get-ViSession -UserName admin
Get-ViSession $env:USERNAME
  • Exclude system users’ sessions. The -exs is an alias of ExcludeServiceAccount parameter.
Get-ViSession -ExcludeServiceAccount:$true
Get-ViSession -exs:$true
  • Get sessions that have been idle more than -IdleTime parameter’s value. 10000 minutes is just about one week Emoj.
Get-ViSession -IdleTime 10000

03.Get-ViSession_Parameters

Disconnect-ViSession

  • The Disconnect-ViSession is working in pair with Get-ViSession function.
Get-ViSession | Disconnect-ViSession
  • You will be prompted for every session.

04.Disconnect-ViSession

Parameters

  • The Disconnect-ViSession has two optional parameters -Confirm and -Verbose.
Get-ViSession | Disconnect-ViSession -Confirm:$false -Verbose

05.Disconnect-ViSession_Confirm

  • Don’t worry about your current and system sessions, the Disconnect-ViSession is intelligent enough to exclude them Emoj.

Output

  • The Disconnect-ViSession by default returns no output. Use -Verbose parameter to add report about every terminated session.
Get-ViSession | Disconnect-ViSession -Verbose
  • To review retained sessions, just run the Get-ViSession again.
Get-ViSession | Disconnect-ViSession
Get-ViSession

Summary

  • For more details about the functions, please take a look at the content based help and examples.
Get-Help Get-ViSession -Full
Get-Help Disconnect-ViSession -Examples
Get-Help Get-ViSession -Parameter IdleTime

You might also like

Set-PowerCLiTitle – Connect-VIServer deep dive
Get-Version – Get any VMware object version
Get ESXi hosts Peripheral devices
VMware VAMI PowerCLi module

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s