Manage vDisks LoadBalancing

By design the maintenance of a PVS Server is pretty easy, in a load balanced environment with at least two PVS Servers you can just take one down and the connections of the target devices are moving to the other Server.

Unfortunately, there is sometimes a gap between theory and reality… We had sometime the issue that the sessions were not handed over to the second one.

I spoke with Sacha Thomet (@sacha81) and he told me about his “chicken-hearted” workaround …  I should statically configure all vDisks on a server, balance the load, and transfer on this way the sessions.

Because I manage an environment with a large amount of vDisks, I won’t do that manually. For this reason, I wrote this Script/Cmdlets. Affinity and rebalancing is no considered.

Be aware! If you have a not working Load balancing, you have an issue in your configuration, and I advise you to fix that, otherwise you will get in trouble sooner or later…

Get-UDPvsDiskLoadblanceInfos

With this cmdlet the LoadBalancing information can be read from one, several or all vDisks.

Set-UDPvsDiskLoadblance

This cmdlet is used to configure the vDisks. It specifies the site and server to be used, for one or more sites.

Set-UDPvsDiskLoadblance -Clear

At the end, the settings have to be reset, which means all vDisks have to be load balanced again.

Script

<#	
	.NOTES
	===========================================================================
	 Created on:   	29.03.2018
	 Created by:   	s.beckmann@unico.ws 
	 E-Mail:        s.beckmann@unico.ch
	 Organization: 	Unico Data AG
	 Filename:     	Manage_Loadbalancing_of_vDisks.ps1
	===========================================================================
	.DESCRIPTION
		Cmdlet to configure loadbalancing on vDisks
	.CALL BY
        Import as module and use the cmdlet.
	.VERSION HISTORY
	    1.0 29.03.2018 - Initial release
#>

Function Get-UDPvsDiskLoadblanceInfos {
    <#
        .SYNOPSIS 
        Get PVS vDisk information.

        .DESCRIPTION
        Get informations about the loadbalancing settings on vDisks.

        .PARAMETER DiskName
        The name of one or many vDisk, or wildecard

        .EXAMPLE
        Get-UDPvsDiskLoadblanceInfos -DiskName 'Win10'
        Get disk info about loadbalancing from vDsik Win10.

        .EXAMPLE
        Get-UDPvsDiskLoadblanceInfos -DiskName 'Win10','Win2012'
        Get disk info about loadbalancing from vDsik Win10 and Win2012.

        .EXAMPLE
        Get-UDPvsDiskLoadblanceInfos -DiskName '*'
        Get disk info about loadbalancing from all vDsiks.

        .EXAMPLE
        Get-UDPvsDiskLoadblanceInfos
        Get disk info about loadbalancing from all vDsiks.

    #>

    [CmdletBinding()]
    Param (
        [Parameter(
            Mandatory = $False,
            Position = 0,
            ValueFromPipeline = $True
        )]
        [System.String[]]
        $DiskName = '*'
    )

    foreach($Disk in $DiskName){
        $Disks = Get-PvsDiskLocator -Fields Name,SiteName,ServerName | Where-Object {$_.Name -like $Disk}
        ForEach ($D in $Disks){
            $Object = New-Object -TypeName psobject -Property @{
                DiskName   = $D.Name
                SiteName   = $D.SiteName
                Servername = $D.ServerName
            }
            Write-Output $Object
        }
    }
}

Function Set-UDPvsDiskLoadblance {
    <#
        .SYNOPSIS 
        Set PVS vDisk loadbalancing configuration.

        .DESCRIPTION
        Set loadbalancing configuration for one, many or all vDsiks. It's possible to set PVS Server per site and clear the static configuration.
        After loadbalancing is configured, the rebalancing will be started.

        .PARAMETER DiskName
        The name of one or manny vDisk, or wildecard

        .PARAMETER Server
        A hash table with Site and PVS Server

        .PARAMETER Clear
        To clear static loadbalancing configuration

        .EXAMPLE
        Set-UDPvsDiskLoadblance -DiskName 'Win10' -Server @{'Site1'='pvsserver01';'Site2'='pvsserver02'}
        Set the loadbalancing options for vDisk Win10, in Site1 to pvsserver01 and in Site2 to pvsserver02. Site1 has also pvsserver03 and Site2 pvsserver2 as PVS Site Server.

        .EXAMPLE
        Set-UDPvsDiskLoadblance -DiskName 'Win10','Win2012' -Server @{'Site1'='pvsserver01';'Site2'='pvsserver02'}
        Set the loadbalancing options for vDisk Win10 and Win2012, in Site1 to pvsserver01 and in Site2 to pvsserver02. Site1 has also pvsserver03 and Site2 pvsserver2 as PVS Site Server.

        .EXAMPLE
        Set-UDPvsDiskLoadblance -DiskName '*' -Server @{'Site1'='pvsserver01';'Site2'='pvsserver02'}
        Set the loadbalancing options for all vDisks, in Site1 to pvsserver01 and in Site2 to pvsserver02. Site1 has also pvsserver03 and Site2 pvsserver2 as PVS Site Server.

        .EXAMPLE
        Set-UDPvsDiskLoadblance -Server @{'Site1'='pvsserver01';'Site2'='pvsserver02'}
        Set the loadbalancing options for all vDisks, in Site1 to pvsserver01 and in Site2 to pvsserver02. Site1 has also pvsserver03 and Site2 pvsserver2 as PVS Site Server.

        .EXAMPLE
        Set-UDPvsDiskLoadblance -DiskName 'Win10' -Clear
        Set the loadbalancing options for vDisk Win10 to loadbalance.

        .EXAMPLE
        Set-UDPvsDiskLoadblance -DiskName 'Win10','Win2012' -Clear
        Set the loadbalancing options for vDisk Win10 and Win2012 to loadbalance.

        .EXAMPLE
        Set-UDPvsDiskLoadblance -DiskName '*' -Clear
        Set the loadbalancing options for all vDisks to loadbalance.

        .EXAMPLE
        Set-UDPvsDiskLoadblance -Clear
        Set the loadbalancing options for all vDisks to loadbalance.

    #>
    [CmdletBinding()]
    Param (
        [Parameter(
            Mandatory = $False,
            Position = 0,
            ValueFromPipeline = $True
        )]
        [System.String[]]
        $DiskName = '*',
        [Parameter(
            Mandatory = $False,
            Position = 1,
            ValueFromPipeline = $True
        )]
        [System.Collections.Hashtable]
        $Server,
        [Parameter(
            Mandatory = $False,
            Position = 2,
            ValueFromPipeline = $True
        )]
        [switch]
        $Clear = $False
    )

    foreach($Disk in $DiskName){
        $Disks = Get-PvsDiskLocator -Fields Name,SiteName,ServerName | Where-Object {$_.Name -like $Disk}
        ForEach ($D in $Disks){
            $Object = New-Object -TypeName psobject -Property @{
                DiskName   = $D.Name
                SiteName   = $D.SiteName
                Servername = $D.ServerName
                
            }
            
            If ($Clear){
                $D.ServerName = $Null
            } Else {
                $D.ServerName = $Server.($D.SiteName)
            }
            Set-PvsDiskLocator $D

            If (!$Clear){
                $ServerToClearDevice = Get-PvsServer -Fields Name,SiteName  | Where-Object {($_.SiteName -eq $D.SiteName) -and ($_.Name -ne $Server.($D.SiteName))}
                Invoke-PvsRebalanceDevices -ServerName $ServerToClearDevice.Name | Out-Null
            }

            $tmpD = Get-PvsDiskLocator -Fields Name,SiteName,ServerName | Where-Object {($_.Name -eq $D.Name) -and ($_.SiteName -eq $D.SiteName)}
            $Object | Add-Member -MemberType NoteProperty -Name 'NewServername' -Value $tmpD.ServerName

            Write-Output $Object

        }
    }
    If ($Clear){
        $Servers = Get-UDPvsDeviceLoadblanceInfos | Sort-Object SiteName
        $Sites = $Servers.SiteName | Select -Unique

        ForEach ($Site in $Sites){
            $Compare = $Servers | Where-Object {$_.SiteName -eq $Site}
            $max = ($Compare | measure-object -Property DeviceCount -maximum).maximum
            $ServerToRebalance = $Compare | ? { $_.DeviceCount -eq $max}
            ForEach ($S in $ServerToRebalance){
                Invoke-PvsRebalanceDevices -ServerName $S.Name | Out-Null
            }

        }
    }
}

Function Get-UDPvsDeviceLoadblanceInfos {
     <#
       .SYNOPSIS 
        Get information about the load from PVS Servers.

        .DESCRIPTION
        Get information about the device load from all PVS Server in any Site in the same Farm.

        .EXAMPLE
        Get-UDPvsDeviceLoadblanceInfos
        Get Informations about the loadbalancing.

    #>   
    $Info = Get-PvsServerInfo |Select Name,SiteName,DeviceCount
    Write-Output $Info

}

if ((Get-PSSnapin "Citrix.PVS.SnapIn" -EA silentlycontinue) -eq $null) {
try { Add-PSSnapin Citrix.PVS.SnapIn -ErrorAction Stop }
catch { write-error "Error loading Citrix.PVS.SnapIn PowerShell snapin"; Return }
}

cls