VMware Cloud Community
mikeanderson173
Enthusiast
Enthusiast
Jump to solution

Trying to have script for Horizon PowerCli to log off sessions after x amount of hours

I'm trying to log off sessions for a specific VDI pool after x amount of hours and just can't seem to figure out the last bit. Here is my code

# Connect to the Horizon Connection Server
Connect-HVserver "<Horizon Connection Server>" -User <User Name> -Password <Password> -Domain <Domain>

$sessionSummaries = Get-HVLocalSession
# For each session in the list
ForEach ($SessionSummary in $SessionSummaries)
{
    # Record the session id
    $SessionId = $SessionSummary.Id

    # Record the machine name
    $MachineName = $SessionSummary.NamesData.MachineOrRDSServerName

    # Get the start time for the session, then find the difference between the current time and the time the session was established
    $SessionTime = (New-TimeSpan -Start $SessionSummary.SessionData.StartTime).TotalHours

    # Only log the session off if the start time was more than 13 hours ago and is a Call Center VM
    If ([int]$SessionTime -gt 13)
      {
        If ($MachineName -like "<DNS Name Wild Card>*")
        {
          Write-Host "Machine with name $machineName has been connected for $sessionTime hours."
          # Log session off
          $hvServices.ExtensionData.Session.Session_Logoff($SessionId)
        }
      }
}

Disconnect-HVserver -Confirm:$false

When I run this, I get the something like the following:

mikeanderson173_0-1622826938735.png

 

Any help would be greatly appreciated, looks like it's just not pulling session ID appropriately.

Thanks!

0 Kudos
1 Solution

Accepted Solutions
mikeanderson173
Enthusiast
Enthusiast
Jump to solution

Thanks for taking a look! Grabbing bits and pieces from the internet and missed that part.

When I added this: $hvServices = Connect-HVserver "<Horizon Connection Server>" -User <User Name> -Password <Password> -Domain <Domain>

It worked 🙂

Thanks again!!

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

Where do you initialise $hvServices?


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

mikeanderson173
Enthusiast
Enthusiast
Jump to solution

Thanks for taking a look! Grabbing bits and pieces from the internet and missed that part.

When I added this: $hvServices = Connect-HVserver "<Horizon Connection Server>" -User <User Name> -Password <Password> -Domain <Domain>

It worked 🙂

Thanks again!!

0 Kudos