dewyser.net

solutions, scripting and more

Querying Horizon Server API — 6th Aug 2024

Querying Horizon Server API

I recently was asked, how can we get a list of all the devices connecting to our environment and what client are they using to do so? With the help of the VMware.Hv.Helper I came up with the following in a couple of minutes:

function Get-ViewAPIService {
  param(
    [Parameter(Mandatory = $false)]
    $HvServer
  )
  if ($null -ne $hvServer) {
    if ($hvServer.IsConnected) {
      return $hvServer.ExtensionData
    }
  }
}

function Get-HVGlobalSession { 
  param(
    [Parameter(Mandatory = $false)]
    $HvServer = $null
  )
    
  $services = Get-ViewAPIService -HvServer $HvServer
  
  $query_service_helper = New-Object VMware.Hv.GlobalSessionQueryServiceService
  $query = new-object vmware.hv.GlobalSessionQueryServiceQuerySpec
    
  $SessionList = @()
  foreach ($pod in $services.Pod.Pod_List()) {
    $query.pod = $pod.id
    $queryResults = $query_service_helper.GlobalSessionQueryService_QueryWithSpec($services, $query)
    $GetNext = $false
    do {
      if ($GetNext) { 
        $queryResults = $query_service_helper.GlobalSessionQueryService_GetNext($services, $queryResults.id)
      }
      $SessionList += $queryResults.results
      $GetNext = $true
    } while ($queryResults.remainingCount -gt 0)
    $query_service_helper.GlobalSessionQueryService_Delete($services, $queryresults.id)
  }
  return $sessionlist
}

function Connect-HV() {
  param (
    [string] $HvServer,
    [string] $username,
    [securestring] $password,
    [string] $domain
  )

  $connection = Connect-HVServer -Server $HvServer -User $username -Password $password -Domain $domain -ErrorAction Stop

  return $connection
}

function Disconnect-HV() {
  param (
    [string] $HvServer
  )

  Disconnect-HVServer -Server $HvServer -ErrorAction Stop -Confirm:$false
}


$target_connection_server = "<connection server>"
$service_account_username = "<username>"
$service_account_domainname = "<domain>"
$service_account_password = ConvertTo-SecureString -AsPlainText "<password>" -Force

$connection = Connect-HV -HvServer $target_connection_server -username $service_account_username -password $service_account_password -domain $service_account_domainname

$sessions = Get-HVGlobalSession -HvServer $connection

$endpoints = $sessions.namesdata | Select-Object -ExpandProperty BaseNames
$endpoints | Format-Table UserName, ClientName, ClientVersion 

Disconnect-HV -HvServer $target_connection_server
Documenting, love it or hate it … (Part 3) — 9th Nov 2022
Documenting, love it or hate it … (Part 2) — 8th Nov 2022

Documenting, love it or hate it … (Part 2)

Part 2: Connection to the Horizon API

To access the Horizon API we need a credential that has read access. Let’s begin …

What I usually do in this scenario is creating an account and saving the password as a secure string in the same location as the script. This method is more secure then passing it along in clear text.

'<password>' | ConvertTo-SecureString -AsPlainText -Force | ConvertFrom-SecureString | Out-File ./secure-password.txt

You only need to do this again when your password has changed. Next we will create a credential object that we can use to connect to the Horizon API.

$svc_account_username = '<username>@<domain>'
$svc_account_password = get-content .\secure-password.txt | ConvertTo-SecureString

$Credential = New-Object System.Management.Automation.PSCredential -ArgumentList ($svc_account_username, $svc_account_password)

The next part is actually connecting to the Horizon API using the credential and storing the output in a variable:

$services = Open-HVConnection -HVServer <connection server fully qualified domain name> -Credential $Credential

Last piece of the puzzle is to close the connection and we are done for part 2.

Close-HVConnection -HVServer <connection server fully qualified domain name>

In Part 3 we will have a look at what we received.

Documenting, love it or hate it … (Part 1) — 7th Nov 2022

Documenting, love it or hate it … (Part 1)

In the next series you will see how we can use the VMware Horizon API in combination with some Powershell modules to document the environment.

Part 1: Installing the necessary Tools and Powershell modules

First we need to install Powershell. There or several ways to do so but I tend to turn to Homebrew whenever I can.

Brew Install --cask powershell

PScribo: PScribo provides a set of functions that make it easy to create a document-like structure within Powershell scripts without having to be concerned with handling output formatting or supporting multiple output formats.

Install-Module PScribo  

Next we will need the VMware modules. With the following command you get a list of the possibilities:

Find-Module -name VMware.*

We only need the one for VMware Horizon for now, let’s install it:

Install-Module VMware.VimAutomation.HorizonView

That’s all for now. In part 2 of the series we will set-up the credential and connect to the Horizon API.