Thursday, September 15, 2022

Citrix Application Performance Reports

 A user had the following request:

I have a Citrix XA/XD 7.15 LTSR environment and would like to write performance data for two applications (Ex: App1, App2) to an Excel spreadsheet using a Powershell script with the following information:
- Application launch and load times
- Application usage report with the number of users for a particular timespan

Please see the linked PowerShell scripts below. You will need to modify the variables $SQLServer and $SQLDBName for your environment and run the scripts as a Citrix Admin with rights to the Director Monitoring database.

Get-AppLogonDuration.ps1 - will create a report on logon duration for the specified applications for the past x days (defaults to 7).

Examples:

    >.\Get-AppLogonDuration.ps1 -days 3

    Will display information for ALL applications for the past 3 days

    >.\Get-AppLogonDuration.ps1 -apps "'Notepad','SnippingTool'" | Sort StartDate -Desc

    Will display information for the applications 'Notepad' and 'SnippingTool' for the past 7 days (default),
    and sort latest entries first
   
   >.\Get-AppLogonDuration.ps1 -days 30 | Sort LogonSecs -Desc | Select -first 10

    Will show the longest 10 logon times over the past 30 days


Get-UniqueAppCounts.ps1 - will create a report on the number of unique users executing the specified applications for the specified duration

Examples:
    >.\Get-UniqueAppCounts.ps1 -days 3

    Will display counts for all applications for the past 3 days

    >.\Get-UniqueAppCounts.ps1 -apps "'Notepad','SnippingTool'"

    Will display counts for the applications 'Notepad' and 'SnippingTool' for the past 7 days (default).

Thursday, June 2, 2022

Kill a User's Citrix Desktop or Application Session

 A user asked for an easy way via PowerShell to kill a user's Citrix application or desktop session.

On one of the Delivery Controllers, save the below script as Logoff-Sessions.ps1, and execute it. 

You will be prompted for the username (entered as domain\username), and then all of the user's sessions will be listed.

You will be asked for confirmation when you make your selection before logging the session off.

Function Logoff-Session {

    # logoff a specified user's session

    asnp Citrix*

    $user = Read-Host -Prompt "Enter User Name (domain\user format)"
    if ($user -ne '') {
        $sessions = @(Get-BrokerSession | ? username -eq $user |
            Select  @{n='SessionKey'; e={$_.SessionKey}},
                    @{n='Host/Desktop'; e={$_.MachineName}},
                    @{n='Application(s)'; e={$_.ApplicationsInUse}})

        if ($sessions.count -eq 0) {
            Write-Host "Sorry ... there are no applications active for $($user)."
        } else {
            Write-Host 'Session#'.Padright(10) 'Host/Desktop'.PadRight(30) 'Application(s)'
            for ($i=0; $i -lt $sessions.Count; ++$i) {
                $sessnum = $i + 1
                Write-Host $sessnum.ToString().PadLeft(5) '    ' $sessions[$i].'Host/Desktop'.PadRight(30) $sessions[$i].'Application(s)'
            }
            $logoff = Read-Host "Which session would you like to log off (0 to exit)?"

            if ($logoff -eq 0 -or $logoff -eq '') {
                return
            }
            if ($logoff -le $sessions.Count) {
                $sessnum = $logoff - 1
                Write-Host $sessions[$sessnum].'Host/Desktop'.PadRight(30) $sessions[$sessnum].'Application(s)'
                $yn = Read-Host "Is this the session you would like to logoff (Y/N)?"
                if ($yn -eq 'Y') {
                    Stop-BrokerSession $sessions[$sessnum].SessionKey
                }
            }
        }
    }
}

Tuesday, March 29, 2022

Request from a forum user:

I'm running Citrix 7.15 Enterprise edition. Can I get a PowerShell script to retrieve a count of disconnected sessions and available machines in each delivery group ? I would like to send the output to a .csv file.

Here is a very simple way to accomplish this via PowerShell:

Add-PSSnapIn Citrix*
Get-BrokerDesktopGroup | Sort-Object Name | Select-Object Name, 
DesktopsDisconnected, DesktopsAvailable |
Export-Csv -Path "C:\temp\DGinfo.csv" -NoTypeInformation


Sunday, January 16, 2022

Copy Applications from One Delivery Group to Another

A forum user asked if there was an easy way to copy all applications from one delivery group to another. Here is a simple PowerShell script to accomplish this:

Function Select-DG ($DGs, $Title = 'Select Delivery Group'){

    Write-Host ""
    Write-Host "=====   Delivery Groups   ====="
    $menuDGs = @{}
    For ($i=1;$i -le $DGs.count; $i++)
    {
        Write-Host "$i. $($DGs[$i-1].Name)"
        $menuDGs.Add($i,($DGs[$i-1].Name))
    }
    [int]$ansDG = if(($ansDG = Read-Host $Title) -eq ''){0} else {$ansDG}

    if ($ansDG -eq 0) { return 0 }
    if ($ansDG -gt 0 -and $ansDG -lt $DGs.Count) {
        $DGSelected = $menuDGs.Item($ansDG)
        (Get-BrokerDesktopGroup | ? Name -eq "$($DGSelected)").UID
    } else {
        Write-Host "Selection not valid, please make a valid selection between 1 and $($i-1)..."
        return 0
    }
}

# retrieve Delivery Groups and pass to function so you don't have to retrieve them twice
$DeliveryGroups = Get-BrokerDesktopGroup | Sort Name | Select Name, UID

$sourceDG = Select-DG $DeliveryGroups "Select the source Delivery Group (0 to exit)"
if ($sourceDG -ne 0) {
    $targetDG = Select-DG $DeliveryGroups "Select the target Delivery Group (0 to exit)"
}
if ($targetDG -eq 0) { return }
if ($sourceDG -eq $targetDG) {
    Write-Host "Target Delivery Group may not be the same as the Source Delivery Group."
    return
}
$DGApps = Get-BrokerApplication | ? AllAssociatedDesktopGroupUids -contains $sourceDG

$sourceName = (Get-BrokerDesktopGroup | ? Uid -eq $sourceDG).Name
$targetName = (Get-BrokerDesktopGroup | ? Uid -eq $targetDG).Name
Write-Host ""
Write-Host "Source Delivery Group: $($sourceName)"
Write-Host "Target Delivery Group: $($targetName)"

# copy each app
foreach ($app in $DGApps) {
    Write-Host "Copying: $($app.BrowserName)"
    $app | Add-BrokerApplication -DesktopGroup $targetName
}
Write-Host "Done."