Hi,
Please help me disconnect particular user whose idle session is 30 mins
## max number of idle minutes for sessions to keep
$intOlderThan = 30
$serviceInstance = Get-View 'ServiceInstance'
## get the session manager object
$sessMgr = Get-View $serviceInstance.Content.sessionManager
## array to hold info about stale sessions
$oldSessions = @()
foreach ($sess in $sessMgr.SessionList){
if (($sess.LastActiveTime).addminutes($intOlderThan) -lt (Get-Date) -and
$sess.Key -ne $sessMgr.CurrentSession.Key){
$oldSessions += $sess.Key
} ## end if
} ## end foreach
## if there are any old sessions, terminate them; else, just write message to the Warning stream
if (($oldSessions | Measure-Object).Count -gt 0) {
## Terminate sessions than are idle for longer than approved ($intOlderThan)
$sessMgr.TerminateSession($oldSessions)
} ## end if
else {Write-Warning "No sessions that have been idle for more than '$intOlderThan' minutes; no action taken"}
So a number of sessions were terminated?
Like I said earlier, it could be that the list maintained in the SessionManager is not refreshed immediately.
And that you might be trying to terminate sessions that are already gone.
The following version of the script checks if the session we are trying to terminate is effectively active.
Perhaps that can avoid the errors you are seeing.
$userName = 'domain\user'
$serviceInstance = Get-View 'ServiceInstance'
$sessMgr = Get-View $serviceInstance.Content.sessionManager
$count = 0
foreach ($sess in $sessMgr.SessionList)
{
if (($sess.LastActiveTime).addminutes($intOlderThan) -lt (Get-Date) -and
$sess.UserName -eq $userName -and
$sess.Key -ne $sessMgr.CurrentSession.Key)
{
if ($sess.Key -ne $sessMgr.CurrentSession.Key)
{
if ($sessMgr.SessionIsActive($sess.Key, $sess.UserName))
{
$sessMgr.TerminateSession($sess.Key)
$count++
}
}
}
}
Write-Host "$count session(s) terminated for user $userName"
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
What exactly is the question here?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD,
My question, the above scripts kills all the users whose idle sessions are 30 mins, but I want to kill only a particular user sessions whose idle time is 30 min.
Along with that, I would like to get the count of sessions of that particular user has been killed.
Try something like this
$intOlderThan = 30
$userName = 'domain\user'
$serviceInstance = Get-View 'ServiceInstance'
$sessMgr = Get-View $serviceInstance.Content.sessionManager
$count = 0
foreach ($sess in $sessMgr.SessionList){
if (($sess.LastActiveTime).addminutes($intOlderThan) -lt (Get-Date) -and
$sess.UserName -eq $userName -and
$sess.Key -ne $sessMgr.CurrentSession.Key)
{
$sessMgr.TerminateSession($sess.Key)
$count++
}
}
Write-Host "$count session(s) terminated for user $userName"
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
I am getting the below error
Exception calling "TerminateSession" with "1" argument(s): "The object or item referred to could not be found."
At D:\sessions_list2.ps1:11 char:4
+ $sessMgr.TerminateSession(@($sess.Key))
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : VimException
That was a typo, I corrected the code above.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Hi LucD, still i am getting the same error
Exception calling "TerminateSession" with "1" argument(s): "The object or item referred to could not be found."
At D:\sessions_list2.ps1:66 char:4
+ $sessMgr.TerminateSession($sess.Key)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : VimException
Strange, seems to be working for.
Did you stop/start your PowerShell/PowerCLI session in the mean time?
Can you run the following and show what it returns?
$serviceInstance = Get-View 'ServiceInstance'
$sessMgr = Get-View $serviceInstance.Content.sessionManager
$sessMgr.SessionList.Count
$sessMgr.SessionList[0..1]
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
No, I didnt start or stop
below is the requested output
1
1313
Key : 52000edd-f8e2-83f7-2402-6b3114d72420
UserName : DOMAIN1\svc.openstack
FullName : svc.openstack
LoginTime : 22-04-2019 06:35:34
LastActiveTime : 22-04-2019 06:35:34
Locale : en
MessageLocale : en
ExtensionSession : False
IpAddress : 172.27.1.251
UserAgent : Ruby
CallCount : 1
Key : 52001536-3402-e6ec-413a-fc430867b2d1
UserName : DOMAIN1\svc.openstack
FullName : svc.openstack
LoginTime : 22-04-2019 06:59:09
LastActiveTime : 22-04-2019 06:59:09
Locale : en
MessageLocale : en
ExtensionSession : False
IpAddress : 172.27.1.251
UserAgent : Ruby
CallCount : 1
Can you do a stop/start, and run the script again?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
I started a new PowerCLI session and here is the output, again I saw 40+ of these errors and the last, I got the output
1
1333
Key : 52001536-3402-e6ec-413a-fc430867b2d1
UserName : ZEOMEGA\svc.openstack
FullName : svc.openstack
LoginTime : 22-04-2019 06:59:09
LastActiveTime : 22-04-2019 06:59:09
Locale : en
MessageLocale : en
ExtensionSession : False
IpAddress : 172.27.1.251
UserAgent : Ruby
CallCount : 1
Key : 5201077f-96c5-3d58-26ce-919b740c5062
UserName : ZEOMEGA\svc.openstack
FullName : svc.openstack
LoginTime : 22-04-2019 07:10:58
LastActiveTime : 22-04-2019 07:10:58
Locale : en
MessageLocale : en
ExtensionSession : False
IpAddress : 172.27.1.251
UserAgent : Ruby
CallCount : 1
PS D:\> .\sessions_list2.ps1
Exception calling "TerminateSession" with "1" argument(s): "The object or item referred to could not be found."
At D:\sessions_list2.ps1:69 char:4
+ $sessMgr.TerminateSession($sess.Key)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : VimException
1291 session(s) terminated for user DOMAIN1\svc.openstack
Ok, so not all calls to the method gave that error?
It could be that the SessionManager doesn't refresh that often, and that there might be some outdated entries in there
Are the number of open sessions less after you ran the script?
Check with (you had 1333 and 1291 should have been terminated)
$sessMgr = Get-View $serviceInstance.Content.sessionManager
$sessMgr.SessionList.Count
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD, that could be because, in the script, I was giving terminate idle session of 30 mins
Yes, 1333 is before and 1291 is after terminating the 30 mins idle sessions
$intOlderThan = 30
$userName = 'DOMAIN1\svc.openstack'
$serviceInstance = Get-View 'ServiceInstance'
$sessMgr = Get-View $serviceInstance.Content.sessionManager
$count = 0
foreach ($sess in $sessMgr.SessionList){
if (($sess.LastActiveTime).addminutes($intOlderThan) -lt (Get-Date) -and
$sess.UserName -eq $userName -and
$sess.Key -ne $sessMgr.CurrentSession.Key)
{
$sessMgr.TerminateSession($sess.Key)
$count++
}
}
Write-Host "$count session(s) terminated for user $userName"
Current output
PS D:\> .\sessions_list3.ps1
1220
So a number of sessions were terminated?
Like I said earlier, it could be that the list maintained in the SessionManager is not refreshed immediately.
And that you might be trying to terminate sessions that are already gone.
The following version of the script checks if the session we are trying to terminate is effectively active.
Perhaps that can avoid the errors you are seeing.
$userName = 'domain\user'
$serviceInstance = Get-View 'ServiceInstance'
$sessMgr = Get-View $serviceInstance.Content.sessionManager
$count = 0
foreach ($sess in $sessMgr.SessionList)
{
if (($sess.LastActiveTime).addminutes($intOlderThan) -lt (Get-Date) -and
$sess.UserName -eq $userName -and
$sess.Key -ne $sessMgr.CurrentSession.Key)
{
if ($sess.Key -ne $sessMgr.CurrentSession.Key)
{
if ($sessMgr.SessionIsActive($sess.Key, $sess.UserName))
{
$sessMgr.TerminateSession($sess.Key)
$count++
}
}
}
}
Write-Host "$count session(s) terminated for user $userName"
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
LucD,
That worked without any error but it look very long time to complete that the previous one. Is this normal ?
That must be due to the additional call to the SessionIsActive method
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thank you very much Champ