Header Ads

Header ADS

PS> Get a List of Citrix Delivery Groups then show the member VDA uptime statistics

About

In a typical deployment scenario, the following script can be remotely invoked or ran directly on a Citrix DDC, where the PowerShell snap-ins are installed. 

Tip: You can install the same PowerShell snap-ins on an administrative jumpbox. The Windows Installer packages can be found in the Citrix ISO's \x64 folder.

Sample Output

Single Delivery Group selected
All Delivery Groups selected

Script: Get-VDAUptime

Add-PSSnapin citrix*
 
$RebootOldAvailableVDAs = $false
$DGList = @()
$Item = 0
 
Function Prompt-Reboot
{
    Write-Host "Do you wish to reboot 'Available' systems with more than 24hr uptimes?" -ForegroundColor Yellow -NoNewline
    $RebootPrompt = Read-Host " [y/n] (n)"
    if ($RebootPrompt -eq "y") {$script:RebootOldAvailableVDAs = $true}
}
 
Function Show-n-Bounce-VDA
{
    param([int]$DGNumber = 0)
 
    # Measure elapsed time of the function
    $FunctionStartDTM = Get-Date
 
    # Get a collection of machines from the delivery group
    $DeliveryGroup = $DGList[$DGNumber]
    "== Delivery Group: " + $DeliveryGroup + " =="
   
    $Machines = Get-BrokerMachine -DesktopGroupName $DGList[$DGNumber]
    $LastBootTime = ""
   
    # Get the uptime for each machine enumerated
    foreach ($MachineID in $Machines) {
        # Determine if the VDA is in maintenance mode
        if ($MachineID.InMaintenanceMode -eq "True") {
            $MaintStatus = "[MAINT]"
            $StatusColor = "Yellow"
        }
        else {
            $MaintStatus = ""
            $StatusColor = "White"
        }
        
        if ($MachineID.HostedMachineName) {
            if ($MachineID.PowerState -ne "Off") {
                $Server = Get-WmiObject -Class Win32_OperatingSystem -cn $MachineID.HostedMachineName -ErrorAction SilentlyContinue
 
                if ($Server.LastBootUpTime -eq $null){
                    Write-Host ($MachineID.HostedMachineName + "`t[N/A]`t`t" + $MaintStatus) -ForegroundColor Magenta
                }
                else {
                $LastBootTime = $Server.ConvertToDateTime($Server.LastBootUpTime)
                if ($LastBootTime -ne "") {
                    $UpTime = $Now.Subtract($LastBootTime)
                    $DurationHr = [int]([math]::Round($UpTime.TotalHours))
           
                    #Set Status color if uptime is > 24h
                    if ($DurationHr -ge 24) {$StatusColor = "Yellow"} Else {$StatusColor = "White"}
 
                    $Duration = "{0:d2}:{1:d2}:{2:d2}" -f ($DurationHr), $UpTime.Minutes, $UpTime.Seconds
                    Write-Host ($MachineID.HostedMachineName.PadRight(15) + "`t" ) -ForeGroundColor $StatusColor -NoNewLine
                    Write-Host ($MachineID.SummaryState.ToString().PadRight(13) + "`t") -ForeGroundColor $StatusColor -NoNewLine
                    Write-Host ($Duration + "`t") -ForeGroundColor $StatusColor -NoNewLine
                    Write-Host ("[" + $MachineID.SessionCount + "]  `t") -ForeGroundColor $StatusColor -NoNewLine
                    Write-Host ($MaintStatus.PadRight(7)) -ForeGroundColor $StatusColor
 
                    # Reboot computer as requested
                    if (($RebootOldAvailableVDAs -eq $True) -and ($DurationHr -ge 24) -and ($MachineID.SummaryState -eq "Available")){
                        Write-Host "Restarting " ($MachineID.HostedMachineName) "..." -ForegroundColor Cyan
                        Restart-Computer $MachineID.HostedMachineName
                        }
                    }
                }
            }
            else {
                Write-Host ($MachineID.HostedMachineName + "`t[Off]`t`t" + $MaintStatus) -ForegroundColor Yellow
            }
        }
        else {
            Write-Host "No Machines defined in the Delivery Group" -ForegroundColor Yellow
        }
    }
    # Display the total elapsed time of processing the Delivery Group
    $ElapsedFunctionDTM = ((Get-Date) - $FunctionStartDTM).ToString("hh\:mm\:ss")
    Write-Host ("Delivery Group Elapsed time: " + $ElapsedFunctionDTM)
 
    # Display Total Sessions in the Delivery Group
    $TotalSessions = (Get-BrokerDesktopGroup -Name $DGList[$DGNumber]).Sessions
    Write-Host "Total Sessions: `t$TotalSessions" -ForegroundColor Magenta
    Write-Host "`n"
}
 
######## START SCRIPT ########
# Get reference time
$Now = Get-Date
 
# Get the list of delivery groups
$DeliveryGroups = Get-BrokerDesktopGroup
 
# List the Delivery Groups for user selection
foreach ($DG in $DeliveryGroups) {
    if ($DG.InMaintenanceMode -eq $True) {
        Write-Host ("`t" + ($Item++).ToString() + "`t" + $DG.Name + " [MAINT]") -ForegroundColor DarkGray
        }
    else {
        Write-Host ("`t" + ($Item++).ToString() + "`t" + $DG.Name) -ForegroundColor White
        }
    $DGList += $DG.Name
}
 
# Prompt the user for the Delivery Group # (or [a]ll)
Write-Host "Please enter the number corresponding to the Delivery Group" -ForegroundColor Yellow -NoNewline
$DGNumber = Read-Host " ([a] for all)"
 
# Measure elapsed time of the script (after the last prompt)
$ScriptStartDTM = Get-Date
 
if ($DGNumber -match "a") {
    Prompt-Reboot   # Prompt user for reboots
 
    # Walk through each DG
    $Script:Item = 0
    foreach ($DG in $DeliveryGroups) {
        # Show Delivery Group and their machines; reboot/skip if applicable
        Show-n-Bounce-VDA -DGNumber $Script:Item
        $Script:Item++
    }
}
elseif (([int]$DGNumber -ge 0) -and ([int]$DGNumber -le $DGList.GetUpperBound(0))) {
    Prompt-Reboot   # Prompt user for reboots
 
    # Show Delivery Group and their machines; reboot/skip if applicable
    Show-n-Bounce-VDA -DGNumber $DGNumber
}
else {
    Write-Host "Invalid Number. Please try again. ($DGNumber)"
}
 
 
# Display the total elapsed time of processing the Delivery Group
$ElapsedScriptDTM = ($(Get-Date) - $ScriptStartDTM).ToString("hh\:mm\:ss")
Write-Host ("Total Elapsed time: " + $ElapsedScriptDTM) -ForegroundColor Green


Updates

Initial Release: Dec 03, 2019

Dec 30, 2019:
- Changed output color for uptimes of 24hrs and up

Jan 02, 2020:
- Added the Availability of the VDAs

Feb 19, 2020:
- Added option to reboot Available VDAs with uptimes of 24hrs and up
- Added Total Sessions at the end of the list
- Added all Delivery Groups as an option
- Display the elapsed time to process the selected Delivery Group(s).

No comments

Powered by Blogger.