VMware Cloud Community
Varozza
Contributor
Contributor

Need script to log off all sessions for particular pool

I feel like this should be easy but for some reason I cannot figure this one out. I need a simple script that will identify all open Horizon sessions from a given pool and then logoff each of those sessions. I would like to schedule this to run once daily at the time which that pools users should be out of office to ensure no sessions remain running.

Thanks in advance,

Tags (1)
Reply
0 Kudos
4 Replies
LucD
Leadership
Leadership

Reply
0 Kudos
Magneet
Hot Shot
Hot Shot

^^ this, mainly the second post has a good script for this. Make sure to force the logoff otherwise disconnected sessions won't be logged off.

Reply
0 Kudos
Varozza
Contributor
Contributor

Thanks for replies. Sorry, I really just cant seem to grasp the API calls for Horizon. I do this kind of stuff daily for AD with PS commands such as get-aduser, set-aduser, etc. For some reason this one is just evading my understanding.

Thanks to the references given above I have almost got what I need. The script below is working perfectly for identifying the VDI sessions I wish to log off. It seems all I am missing is a one liner to log each of the sessions off as they are found? Any advice how to do this?

$sessions=(get-hvlocalsession).namesdata

foreach ($session in $sessions){

if($session.DesktopName -eq "TESTPOOL1"){

     Write-Host "Connection to TESTPOOL1 found: "  $session.Username 

     #NEED HELP HERE.... NEED TO ADD LOGOFF COMMAND HERE TO LOG OFF THE SESSION IN QUESTION

}

   }

Reply
0 Kudos
Magneet
Hot Shot
Hot Shot

$hvserver=connect-hvserver servername

$hvservice=$hvserver.extensiondata

$hvservice.session.session_logoff($sessionid)

if you have more sessions.Session_LogoffSessions($arrayodsessionids)

or when they are disconnected/screen locked use Session_LogoffSessionsForced or Session_LogoffForced

or see: https://www.retouw.nl/2019/07/15/apihow-to-successfully-logoff-users/

Reply
0 Kudos