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 = "10.3.3.15"
$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

    10.3.3.12 MYLAB\Administrator 12/24/2018 09:39:07 VMware vSphere Client/6.0.0 520564b0-3480-5858-676f-3b04b1515b25
    10.10.10.2 VSPHERE.LOCAL\Administrator 12/27/2018 06:10:25 PowerCLI/11.0.0.10380590 5223490f-a7ea-c0e2-d424-9fe2ff2d464d
    10.3.3.12 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
    10.3.3.12 MAYLAB\Administrator 12/24/2018 15:47:17 PowerCLI/6.3.0.3737840 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 10.3.3.12 and kill them
$SessionManager.SessionList | ? {$_.IpAddress -like "10.3.3.12"} |% {$SessionManager.TerminateSession($_.Key)}
  1. You can also Terminate the Active Session via vCenter Web GUI

PSCore-DisconnectVISession-01.jpg

Leave a Comment

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

Scroll to Top