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,
Did you try the code Wouter showed in Pulling horizon session information using PowerCLI and [API]How to successfully logoff users in Horizon?
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
^^ 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.
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
}
}
$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/