Public/Get-AvdLatestSessionHost.ps1

function Get-AvdLatestSessionHost {
    <#
    .SYNOPSIS
    Gets the latest session host from the AVD Hostpool
    .DESCRIPTION
    The function will help you getting the latests session host from a AVD Hostpool.
    By running this function you will able to define the next number for deploying new session hosts.
    .PARAMETER HostpoolName
    Enter the AVD Hostpool name
    .PARAMETER ResourceGroupName
    Enter the AVD Hostpool resourcegroup name
    .PARAMETER InputObject
    You can put the hostpool object in here.
    .PARAMETER NumOnly
    With this switch parameter you will set, you will get the next sessionhost number returned.
    .EXAMPLE
    Get-AvdLatestSessionHost -HostpoolName avd-hostpool -ResourceGroupName avd-resourcegroup
    #>

    [CmdletBinding(DefaultParameterSetName = 'Parameters')]
    param (
        [parameter(Mandatory, ParameterSetName = 'Parameters')]
        [ValidateNotNullOrEmpty()]
        [string]$HostpoolName,

        [parameter(Mandatory, ParameterSetName = 'Parameters')]
        [ValidateNotNullOrEmpty()]
        [string]$ResourceGroupName,

        [parameter(ParameterSetName = 'Parameters')]
        [switch]$NumOnly,

        [parameter(Mandatory, ValueFromPipeline, ParameterSetName = 'InputObject')]
        [ValidateNotNullOrEmpty()]
        [PSCustomObject]$InputObject
    )

    Write-Verbose "Start searching"
    AuthenticationCheck
    switch ($PsCmdlet.ParameterSetName) {
        InputObject { 
            $Parameters = @{
                HostpoolName      = $InputObject.Name
                ResourceGroupName = (Get-AzResource -ResourceId $InputObject.Id).ResourceGroupName
            }
        }
        Default {
            $Parameters = @{
                HostPoolName      = $HostpoolName
                ResourceGroupName = $ResourceGroupName
            }
        }
    }
    $SessionHosts = Get-AvdSessionHost @Parameters |  Sort-Object properties.ResourceId -Descending
    # Convert hosts to highest number to get initial value
    if ($null -eq $SessionHosts) {
        $InitialNumber = 0
    }
    else {
        $All = [ordered]@{}
        $Names = $SessionHosts | % { ($_.Name).Split("/")[-1].Split(".")[0] }
        $Names | % { $All.add([int]($_).Split("-")[-1], $_) }
    
        $InitialNumber = ($All.GetEnumerator() | Sort-Object Name | Select-Object -Last 1).Key + 1
        $VirtualMachineName = $All.GetEnumerator() | Sort-Object Name | Select-Object -Last 1 -ExpandProperty Value
        $LatestSessionHost = $SessionHosts | Where-Object { $_.Name -match $VirtualMachineName }
    }
    if ($NumOnly) {
        return $InitialNumber
    }
    else {
        return $LatestSessionHost
    }
}