Automation

 View Only
Expand all | Collapse all

How disconnect horizon session?

  • 1.  How disconnect horizon session?

    Posted Mar 24, 2021 07:31 PM

    Hi,
    I can't for the life of me find anyway to disconnect a Horizon Session for a specific user. I have search all the powecli cmdlet I think but I can't find it. I figure it must ber there somewere as it's something that I guess many people want to use.

    I have extracted many information in the following way so I can see if the user has a session, but as I was telling above I can't figure out how to disconnect it.

    So just to bee clear, I do know the username of the user that I want to disconnect the session for. I just need the command for it but I can't find it.

     

         
    #First code that are running to collect the data
    
    $HVUserSessions = foreach ($HVSrv in $HVServers) {
        Connect-HVServer -Server $HVSrv -Credential $VMWareAuth
        (Get-HVLocalSession).namesdata
        Disconnect-HVServer -Server $HVSrv -Force -Confirm:$false
    }  
    
    ## Second code that are writing out the data.
    
                                  $VDISessionData = $HVData | Foreach-Object { 
                                            if ($null -ne ($vdi = $_)) {
                                                [PSCustomObject]@{
                                         Machine       = $vdi.MachineOrRDSServerName
                                                    Pool          = $vdi.DesktopPoolCN
                                                    Desktop       = $vdi.DesktopName
                                                    ClientVersion = $vdi.ClientVersion
                                                    ClientOS      = $vdi.ClientType
                                                }
                                            }
                                        }

     



  • 2.  RE: How disconnect horizon session?

    Posted Mar 24, 2021 09:46 PM


  • 3.  RE: How disconnect horizon session?

    Posted Mar 25, 2021 07:10 AM

    Hi,
    I did look at it but I can't for the life of me understand how to extract the sessionID for one specifik user and then disconnect that session.



  • 4.  RE: How disconnect horizon session?

    Posted Mar 25, 2021 08:24 AM

    That should be via the Get-HvLocalsession function.



  • 5.  RE: How disconnect horizon session?

    Posted Mar 26, 2021 11:50 AM

    Can you give me a example how to disconnect a session with the Get-HVLocalsession?

    When I look in the sessiondata I can see for example;
    SessionType : DESKTOP
    SessionProtocol :
    SessionState : DISCONNECTED
    StartTime : 2021-03-13 08:29:07
    DisconnectTime : 2021-03-13 13:29:44
    LastSessionDurationMS : 14704282
    BrokeredRemotely :
    ResourcedRemotely : False
    Unauthenticated : False
    IdleDuration : 0

     

    I have figure out how to match username with sessiondata so that's solved.

     



  • 6.  RE: How disconnect horizon session?

    Posted Mar 26, 2021 12:03 PM

    There should be an Id property in there.
    See Wouter's post [API]How to successfully logoff users in Horizon

    Can't test since I currently do not have an active Horizon environment



  • 7.  RE: How disconnect horizon session?

    Posted Mar 26, 2021 12:09 PM

    Yes it is, it's under (Get-hvlocalsession).id

    So I guess that if I filter out the id I can do something like this after;
    (Get-hvlocalsession).id.Session_LogoffForced

    But What I'm wondering here is how can I past the ID that I want to that line of code that are disconnecting them?

    $SessionID = (Get-hvlocalsession).id
    (Get-hvlocalsession).id.Session_LogoffForced.$SessionID

    or is it wrong? This is what I can't understand.

     

     

    Like for this script example, I don't want it to ask Q or similare just disconnect the user and get the information from Variable.

    $Username and $Session

     

    $hvserver1=connect-hvserver servername -user user -domain domain -password passwords
    $Services1= $hvServer1.ExtensionData
     
    $username= Read-Host "Which user do you want to logoff? (no wildcards needed, part of the name is enough)"
     
    $queryService = New-Object VMware.Hv.QueryServiceService
    $userdefn = New-Object VMware.Hv.QueryDefinition
    $userdefn.queryEntityType = 'ADUserOrGroupSummaryView'
    $userfilter1= New-Object VMware.Hv.QueryFilterContains
    $userfilter1.membername='base.name'
    $userfilter1.value=$username
    $userfilter2= New-Object VMware.Hv.QueryFilterEquals
    $userfilter2.membername='base.group'
    $userfilter2.value=$False
    $userfilter=new-object vmware.hv.QueryFilterAnd
    $userfilter.filters=@($userfilter1, $userfilter2)
    $userdefn.filter=$userfilter
    $users=($queryService.QueryService_Create($Services1, $userdefn)).results
     
    $menu = @{}
    for ($i=1;$i -le $users.count; $i++){
    Write-Host "$i. $($users[$i-1].base.name)"
    $menu.Add($i,($users[$i-1].id))
    }
    [int]$ans = read-host "Please select the correct user"
    $user=$menu.Item($ans)
     
    $GlobalSessionQueryService = new-object VMware.Hv.GlobalSessionQueryServiceService
    $sessionfilterspec=new-object vmware.hv.GlobalSessionQueryServiceQuerySpec
    $sessionfilterspec.user=$user
    $sessions=($GlobalSessionQueryService.GlobalSessionQueryService_QueryWithSpec($services1, $sessionfilterspec)).results
     
    $menu = @{}
    for ($i=1;$i -le $sessions.count; $i++){
    Write-Host "$i. $($sessions[$i-1].namesdata.basenames.MachineOrRDSServerName)"
    $menu.Add($i,($sessions[$i-1].id))
    }
    [int]$ans = read-host "Please select the correct VDI Desktop"
    $session=$menu.Item($ans)
     
    $Services1.Session.Session_Logoffforced($session)
    $queryService.QueryService_DeleteAll($services1)

     



  • 8.  RE: How disconnect horizon session?

    Posted Mar 26, 2021 12:37 PM

    The sessionId is a parameter to the LogoffForced method.
    Wouter posted a full script at the end of the post [API]How to successfully logoff users in Horizon



  • 9.  RE: How disconnect horizon session?

    Posted Mar 26, 2021 02:05 PM

    Hi again,

    Yes I know maybe my english is not as good.

    What I want from that script is instead of getting quastions regarding the username and the desktop/machine name I want to preoplate them with variables.



  • 10.  RE: How disconnect horizon session?

    Posted Mar 26, 2021 02:16 PM

    You could replace that part with something like this

    $user = 'AnyUser'
    $station = 'AnyStation'
    
    $GlobalSessionQueryService = New-Object VMware.Hv.GlobalSessionQueryServiceService
    $sessionfilterspec = New-Object vmware.hv.GlobalSessionQueryServiceQuerySpec
    $sessionfilterspec.user = $user
    $sessions = ($GlobalSessionQueryService.GlobalSessionQueryService_QueryWithSpec($services1, $sessionfilterspec)).results
    
    $session = $sessions | where{$_.namesdata.basenames.MachineOrRDSServerName -eq $station}
    if($session){
        $Services1.Session.Session_Logoffforced($session)
    }
     


  • 11.  RE: How disconnect horizon session?

    Posted Mar 26, 2021 03:13 PM

    That was what I did try and I get this error when I do that.

     

     

    Exception setting "user": "Cannot convert the "xxxx" value of type "System.String" to type "VMware.Hv.UserOrGroupId"."
    At C:\tmp\Untitled-1.ps1:8 char:1
    PS C:\Temp> c:\tmp\Untitled-1.ps1
    Exception setting "user": "Cannot convert the "xxxx" value of type "System.String" to type "VMware.Hv.UserOrGroupId"."
    At C:\tmp\Untitled-1.ps1:8 char:1
    + $sessionfilterspec.user = $user
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], SetValueInvocationException
        + FullyQualifiedErrorId : ExceptionWhenSetting
     
    Exception calling "GlobalSessionQueryService_QueryWithSpec" with "2" argument(s): "ExceptionType : VMware.Hv.InvalidQuery
    ErrorMessage : Session query resolved to an invalid scope for filters: [UserId: null] [GeId: null[GaeId: null] [BrokeringPodId: null] [hostingPodId: null] [poolId: null]"
    At C:\tmp\Untitled-1.ps1:9 char:1
    + $sessions = ($GlobalSessionQueryService.GlobalSessionQueryService_Que ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
        + FullyQualifiedErrorId : VimException
     
    Cannot convert argument "id", with value: "VMware.Hv.SessionGlobalSummaryView", for "Session_LogoffForced" to type "VMware.Hv.SessionId": "Cannot convert the "VMware.Hv.SessionGlobalSummaryView" value of type "VMware.Hv.SessionGlobalSummaryView" to type "VMware.Hv.SessionId"."
    At C:\tmp\Untitled-1.ps1:13 char:5
    +     $Services1.Session.Session_Logoffforced($session)
    +     ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: (:) [], MethodException
        + FullyQualifiedErrorId : MethodArgumentConversionInvalidCastArgument

     

     

    My code looks like this;


     

    $user = 'xxxx'
    $station = "xxx"

    $hvserver1 = Connect-HVServer -Server 'xxxxx' -User "xxxxx" -password "xxxx"
    $Services1 = $hvServer1.ExtensionData
    $GlobalSessionQueryService = New-Object VMware.Hv.GlobalSessionQueryServiceService
    $sessionfilterspec = New-Object vmware.hv.GlobalSessionQueryServiceQuerySpec
    $sessionfilterspec.user = $user
    $sessions = ($GlobalSessionQueryService.GlobalSessionQueryService_QueryWithSpec($services1, $sessionfilterspec)).results

    $session = $sessions | where { $_.namesdata.basenames.MachineOrRDSServerName -eq $station }
    if ($session) {
        $Services1.Session.Session_Logoffforced($session)
    }
     


  • 12.  RE: How disconnect horizon session?

    Posted Mar 26, 2021 03:21 PM

    Can you try like this?

    $userName = 'AnyUser'
    
    $queryService = New-Object VMware.Hv.QueryServiceService
    $userdefn = New-Object VMware.Hv.QueryDefinition
    $userdefn.queryEntityType = 'ADUserOrGroupSummaryView'
    $userfilter1= New-Object VMware.Hv.QueryFilterContains
    $userfilter1.membername='base.name'
    $userfilter1.value=$username
    $userfilter2= New-Object VMware.Hv.QueryFilterEquals
    $userfilter2.membername='base.group'
    $userfilter2.value=$False
    $userfilter=new-object vmware.hv.QueryFilterAnd
    $userfilter.filters=@($userfilter1, $userfilter2)
    $userdefn.filter=$userfilter
    $user=($queryService.QueryService_Create($Services1, $userdefn)).results
    
    $station = 'AnyStation'
    
    $GlobalSessionQueryService = New-Object VMware.Hv.GlobalSessionQueryServiceService
    $sessionfilterspec = New-Object vmware.hv.GlobalSessionQueryServiceQuerySpec
    $sessionfilterspec.user = $user.Id
    $sessions = ($GlobalSessionQueryService.GlobalSessionQueryService_QueryWithSpec($services1, $sessionfilterspec)).results
    
    $session = $sessions | where{$_.namesdata.basenames.MachineOrRDSServerName -eq $station}
    if($session){
        $Services1.Session.Session_Logoffforced($session)
    }
     


  • 13.  RE: How disconnect horizon session?

    Posted Mar 27, 2021 09:41 PM

    Robin reached out to me and I ended up creating this  https://www.retouw.nl/2021/03/26/powercli-script-to-forcefully-log-off-horizon-users/