下列 PowerShell 函數會判斷所有桌面平台集區的目前使用量,並調整任何達到容量上限的自動佈建或連結複製桌面平台集區大小。

# PollAllPoolsUsage
# Parameters
# $increment Amount by which to increase a pool that is at maximum capacity (default = 5).

function PollAllPoolsUsage
{ param ($increment)

    if(-not $increment){
        $increment = 5
    }
    # Retrieve all pool objects and check each one individually
    $pools = Get-Pool
    foreach ($pool in $pools){
        PollPoolUsage $pool $increment
    }
}
# PollPoolUsage
# Parameters
# $Pool Pool object that represents the pool to be checked.
# $increment Amount by which to increase pool that is at maximum capacity.

function PollPoolUsage
{ param ($Pool, $increment)
    # Get a list of remote sessions for the pool (errors are suppressed)
    $remotes = Get-RemoteSession -pool_id $Pool.pool_id -ErrorAction SilentlyContinue
    # Count the remote sessions.
    $remotecount = 0
    if($remotes){
        $remotecount = ([Object[]]($remotes)).Count
    }

    # Determine the maximum number of desktops configured for a pool.
    $maxdesktops = 0
    if($Pool.deliveryModel -eq "Provisioned"){
        $maxdesktops = $Pool.maximumCount
    } else {
        $maxdesktops = $Pool.machineDNs.split(";").Count
    }

    # Output the usage statistics for a pool.
    Write-Output ("==== " + $Pool.pool_id + " ====")
    Write-Output ("Remote session count: " + $remotecount)
    Write-Output ("Maximum desktops: " + $maxdesktops)
    # If a pool is using all its desktops, increase its maximum size
    # or output a warning if it cannot be resized.
    if($maxdesktops -eq $remotecount){
        if($Pool.deliveryModel -eq "Provisioned"){ # Pool type can be resized
            $newmaximum = [int]$Pool.maximumCount + [int]$increment
            if($Pool.desktopSource -eq "VC"){ # Resize an automatic pool
                Update-AutomaticPool -pool_id $Pool.pool_id -maximumCount $newmaximum
            } elseif ($Pool.desktopSource -eq "SVI"){ # Resize a linked-clone pool
                Update-AutomaticLinkedClonePool -pool_id $Pool.pool_id -maximumCount $newmaximum
            }

            Write-Output ("Pool " + $Pool.pool_id + " is using 100% of its desktops. Maximum VMs
                increased to " + $newmaximum)
        } else { # Pool type cannot be resized
            Write-Output ("Pool " + $Pool.pool_id + " is using 100% of its desktops. Consider
                increasing its capacity.")
        }
    }
}