Terminate Citrix Session/s via PowerShell

Hello! Once again another PowerShell Code for you to automate quite common task which is done daily on a support engineer perspective.

I use this script pretty much everyday at some point its much easier when you do it via PowerShell, the reason i say this you will have to login to the Citrix Cloud click here and there to find the right place where you need to look for a specific user and then you will need to see how many session he/she has opened and then select the right session. Then you would go into that page and click logoff or disconnect. which is a very mundane task but what we are talking here is about time (TIME IS MONEY).

Imagine now you have to do it for around 20 users, 50 Users, 100 Users and etc.

The Time comes in handy when you have simple script which will save all those time in-fact you can schedule or many other ways to trigger but lets talk about that later now. let me show you how does this script looks. Before that you need make the initial connect to the Citrix Cloud click here to learn how to.

Enough is said here you go:

Set-XDCredentials -ProfileType CloudApi -StoreAs default -CustomerId "customerID" -SecureClientFile C:\Temp\secureclient.csv

$users = get-content -path "C:\Users\Fazul\Download\names.txt"

foreach($user in $users){
$samuser = $user.accountname

$output = "D:\Citrix_termination\Termination_$samuser.txt"

 Get-BrokerSession -UserName "Domain\$samuser" -Filter { DesktopGroupName -like 'UK-W10*'} | select UserName,machinename,DesktopGroupName,EstablishmentTime

    if($results -ne $null){
            foreach($result in $results){

                (Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " -> Active Session Found - " + $result.UserName+ " | "+$result.MachineName+ " | "+$result.DesktopGroupName+ " | "+$result.EstablishmentTime >> $output
                
		try{ 
		Get-BrokerSession -UserName "lseg\$samuser" -Filter { DesktopGroupName -like 'UK-W10*'} -ErrorAction stop #| Stop-BrokerSession 
		}catch{ 
		(Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " -> " + $samuser + " Citrix Session unable to terminate!!!">> $output 
		}

                (Get-Date -Format "yyyy-MM-dd HH:mm:ss") + " -> " + $samuser + " Citrix Session test terminated!!!">> $output
            }
}
}

Quick Explanation on the function of the script above.

The script starts by setting some credentials for a cloud API, and then retrieves a list of user names from a file called “names.txt” located in a folder called “Download” on the user’s computer.

For each user in the list, the script searches for any active sessions for that user on a Citrix server. If an active session is found, the script attempts to terminate it. If the termination is successful, a message is written to a log file indicating that the session was terminated. If the termination fails for any reason, an error message is written to the log file instead.

The log file is created in a folder called “Citrix_termination” located on the user’s computer. The log file name includes the user’s account name as part of the file name.

Overall, the script automates the task of searching for and terminating active sessions on a Citrix server for a list of users, and provides a log file to keep track of the termination attempts.

Hope this Helps, Have a great friday.

Published by iamfazul

Author of the site

Leave a comment