PS Core | Disconnect VI Session

Steps to Disconnect VI Session with PowerCLI on PowerShell Core

  1. Login to vCenter with valid Username & Password
#PowerCLI - PowerShell Core on Windows 10
#First Time Only
Set-PowerCLIConfiguration -InvalidCertificateAction Ignore -Confirm:$false
Set-PowerCLIConfiguration -ParticipateInCeip $true

#Import VMware Module
Import-Module VMware.VimAutomation.Core

#Connect to vCenter
$VC = ""
$Username = "[email protected]"
$PAssword = "P@ssw0rd01"

Connect-VIServer -Server $VC -User $Username -Password $PAssword -WarningAction SilentlyContinue
  1. List the Active Session
#List PowerCLI Session
$svcRef = new-object VMware.Vim.ManagedObjectReference
$svcRef.Type = "ServiceInstance"
$svcRef.Value = "ServiceInstance"
$ServiceInstance = get-view $svcRef
$SessionManager = get-view $ServiceInstance.Content.SessionManager
foreach ($Session in $SessionManager.SessionList) {
    #Filter the VI Session for VPXd, and only list with valid users' login
    if ($Session.UserName -notlike "*vpxd*") {
        #if (($sess.LastActiveTime).addminutes(60) -lt (Get-Date)){
        Write-Output "$($Session.IpAddress) $($Session.UserName) $($Session.LoginTime) $($Session.UserAgent) $($Session.Key) "

Output of the above script with Username, IPAdress, LoginTime, UserAgent and Key show MYLAB\Administrator 12/24/2018 09:39:07 VMware vSphere Client/6.0.0 520564b0-3480-5858-676f-3b04b1515b25 VSPHERE.LOCAL\Administrator 12/27/2018 06:10:25 PowerCLI/ 5223490f-a7ea-c0e2-d424-9fe2ff2d464d VSPHERE.LOCAL\Administrator 12/24/2018 04:21:08 Mozilla/4.0 (compatible; MSIE 6.0; MS Web Services Client Protocol 4.0.30319.36366) 5227c22e-4147-0f61-7ef3-c08e4aeb583c MAYLAB\Administrator 12/24/2018 15:47:17 PowerCLI/ 523ba008-bae2-e2b1-b197-2db1f6350f9b
  1. Search the session based on IP, Username, LoginTime, UserAgent or Key and kill it
#Search the sessions that are idle for 12 Hours and kill it
$IdleTime = "12"
$SessionManager.SessionList | ? {$_.LastActiveTime -lt (Get-Date).AddHours(-$IdleTime)} |% {$SessionManager.TerminateSession($_.Key)}

#Search the session that are from IP Address of and kill them
$SessionManager.SessionList | ? {$_.IpAddress -like ""} |% {$SessionManager.TerminateSession($_.Key)}
  1. You can also Terminate the Active Session via vCenter Web GUI


Leave a Comment

Your email address will not be published. Required fields are marked *

Scroll to Top