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).
- All you need is connect to your VC and run the
Connect-VIServer VC1, PSC1, VCSA2 -wa SilentlyContinue Get-ViSession Get-ViSession | ft Get-ViSession | ft –au
- By default, the function returns necessary properties only. If you want to get the full object, format the output with
Format-Listcmdlet or use
Get-ViSession | fl Get-ViSession | select *
- Pay attention to the
Clientproperty. The function tries to resolve client IP address by
nslookupand if the address cannot be resolved, the property will contain IP instead of hostname.
ClientTypeproperty is what user agent used to establish connection. The
Legacyis C# client. The
Internalclient usually used by different VMware solutions like vROPs or vRA. The VDP sessions will have
There are three options to filter the function output by one of available parameters.
- Get sessions for users that match the
-UserNameparameter’s value. This is positional parameter and may be omitted.
Get-ViSession -UserName admin Get-ViSession $env:USERNAME
- Exclude system users’ sessions. The
-exsis an alias of
Get-ViSession -ExcludeServiceAccount:$true Get-ViSession -exs:$true
- Get sessions that have been idle more than
-IdleTimeparameter’s value. 10000 minutes is just about one week .
Get-ViSession -IdleTime 10000
Disconnect-ViSessionis working in pair with
Get-ViSession | Disconnect-ViSession
- You will be prompted for every session.
Disconnect-ViSessionhas two optional parameters
Get-ViSession | Disconnect-ViSession -Confirm:$false -Verbose
- Don’t worry about your current and system sessions, the
Disconnect-ViSessionis intelligent enough to exclude them .
Disconnect-ViSessionby default returns no output. Use
-Verboseparameter to add report about every terminated session.
Get-ViSession | Disconnect-ViSession -Verbose
- To review retained sessions, just run the
Get-ViSession | Disconnect-ViSession Get-ViSession
- 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