-
1. Re: Disconnect particular user whose idle session is 30 mins
LucD Apr 19, 2019 4:04 AM (in response to ganapa2000)What exactly is the question here?
-
2. Re: Disconnect particular user whose idle session is 30 mins
ganapa2000 Apr 19, 2019 4:07 AM (in response to LucD)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.
-
3. Re: Disconnect particular user whose idle session is 30 mins
LucD Apr 20, 2019 4:33 AM (in response to ganapa2000)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"
-
4. Re: Disconnect particular user whose idle session is 30 mins
ganapa2000 Apr 20, 2019 4:06 AM (in response to LucD)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
-
5. Re: Disconnect particular user whose idle session is 30 mins
LucD Apr 20, 2019 4:35 AM (in response to ganapa2000)That was a typo, I corrected the code above.
-
6. Re: Disconnect particular user whose idle session is 30 mins
ganapa2000 Apr 21, 2019 9:57 PM (in response to LucD)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
-
7. Re: Disconnect particular user whose idle session is 30 mins
LucD Apr 21, 2019 11:29 PM (in response to ganapa2000)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?
$global:DefaultVIServers.Count$serviceInstance = Get-View 'ServiceInstance'
$sessMgr = Get-View $serviceInstance.Content.sessionManager
$sessMgr.SessionList.Count
$sessMgr.SessionList[0..1]
-
8. Re: Disconnect particular user whose idle session is 30 mins
ganapa2000 Apr 22, 2019 12:04 AM (in response to LucD)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
-
9. Re: Disconnect particular user whose idle session is 30 mins
LucD Apr 22, 2019 12:07 AM (in response to ganapa2000)Can you do a stop/start, and run the script again?
-
10. Re: Disconnect particular user whose idle session is 30 mins
ganapa2000 Apr 22, 2019 12:25 AM (in response to LucD)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
-
11. Re: Disconnect particular user whose idle session is 30 mins
LucD Apr 22, 2019 12:34 AM (in response to ganapa2000)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)
$serviceInstance = Get-View 'ServiceInstance'$sessMgr = Get-View $serviceInstance.Content.sessionManager
$sessMgr.SessionList.Count
-
12. Re: Disconnect particular user whose idle session is 30 mins
ganapa2000 Apr 22, 2019 12:45 AM (in response to LucD)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
-
13. Re: Disconnect particular user whose idle session is 30 mins
LucD Apr 22, 2019 12:54 AM (in response to ganapa2000)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.$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)
{
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"
-
14. Re: Disconnect particular user whose idle session is 30 mins
ganapa2000 Apr 22, 2019 1:30 AM (in response to LucD)LucD,
That worked without any error but it look very long time to complete that the previous one. Is this normal ?