VMware Cloud Community
robinstolpe
Enthusiast
Enthusiast

How disconnect horizon session?

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
                                            }
                                        }
                                    }

 

IT engineer that works with the hole VMWare portfolio and also loves to develop and automate in C# and PowerShell
Reply
0 Kudos
12 Replies
LucD
Leadership
Leadership

Did you already try Wouter's code from Need script to log off all sessions for particular... - VMware Technology Network VMTN


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
robinstolpe
Enthusiast
Enthusiast

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.

IT engineer that works with the hole VMWare portfolio and also loves to develop and automate in C# and PowerShell
Reply
0 Kudos
LucD
Leadership
Leadership

That should be via the Get-HvLocalsession function.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
robinstolpe
Enthusiast
Enthusiast

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.

 

IT engineer that works with the hole VMWare portfolio and also loves to develop and automate in C# and PowerShell
Reply
0 Kudos
LucD
Leadership
Leadership

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
robinstolpe
Enthusiast
Enthusiast

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)

 

IT engineer that works with the hole VMWare portfolio and also loves to develop and automate in C# and PowerShell
Reply
0 Kudos
LucD
Leadership
Leadership

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


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
robinstolpe
Enthusiast
Enthusiast

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.

IT engineer that works with the hole VMWare portfolio and also loves to develop and automate in C# and PowerShell
Reply
0 Kudos
LucD
Leadership
Leadership

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)
}
 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
robinstolpe
Enthusiast
Enthusiast

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)
}
 
IT engineer that works with the hole VMWare portfolio and also loves to develop and automate in C# and PowerShell
Reply
0 Kudos
LucD
Leadership
Leadership

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)
}
 


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

Reply
0 Kudos
Magneet
Hot Shot
Hot Shot

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/

Reply
0 Kudos