We help IT Professionals succeed at work.

Script to remotely kill process for a specific user on all Citrix servers

Medium Priority
70 Views
1 Endorsement
Last Modified: 2020-03-18
Looking for a script that can be used remotely to kill a specific process for a specific user on any server in the Citrix farm.  
Also is it possible that there can be an Input Box that I can enter the Username and process name?

Thanks in advance.
Comment
Watch Question

CERTIFIED EXPERT

Commented:
Use psexec
CERTIFIED EXPERT

Commented:
Or pskill, I mean 😃
Sam JacobsCitrix Technology Professional / Director of TechDev Services, IPM
CERTIFIED EXPERT

Commented:
Are you looking to kill a process or a Citrix published application?
CERTIFIED EXPERT

Commented:
or psexec to execute a taskkill

Author

Commented:
When I tried Taskkill it killed the process on the entire server not just for that user.

@Sam - It's a Citrix published app. It sometimes gives error which we need to end that process for that user session. But it's a long manual process.
CERTIFIED EXPERT

Commented:
Yes, you need to maybe use something like pslist to identify the process ID and then kill it using that rather than the image name
CERTIFIED EXPERT

Commented:
Don't you have Studio or Director where you can view the processes remotely and terminate them from the GUI? You didn't say what version of Citrix it was.
Sam JacobsCitrix Technology Professional / Director of TechDev Services, IPM
CERTIFIED EXPERT

Commented:
What version of Citrix are you running? 7.x?
CERTIFIED EXPERT
Most Valuable Expert 2019
Most Valuable Expert 2018
Commented:
For the fun of it, here's a PowerShell script that will list the processes on the machines specified in a GridView.
You can use the GridView's Filter to select the process(es) you want killed and click OK.
$computerList = 'Server1', 'Server2', 'Server3'

Write-Host 'Gathering process list ...'
$wmiProcessList = Get-WmiObject -Class Win32_Process -ComputerName $computerList

Write-Host 'Waiting for process selection in the GUI form ...'
$terminateList = $wmiProcessList |
	Select-Object -Property CSName, Name, ProcessId, SessionId, @{n='CreationDate'; e={$_.ConvertToDateTime($_.CreationDate)}}, @{n='UserName'; e={$u = $_.GetOwner(); "$($u.Domain)\$($u.User)"}}, @{n='UserSid'; e={$_.GetOwnerSid().Sid}} |
	Where-Object {$_.UserSid -like 'S-1-5-21-*'} |
	Out-GridView -OutputMode Multiple -Title "Please select the process(es) to terminate:"

ForEach ($process in $terminateList) {
	Write-Host "Terminating '$($process.Name)' for $($process.UserName) on $($process.CSName) ..." -NoNewline
	$wmiProcess = $wmiProcessList | Where-Object {($_.ComputerName -eq $process.ComputerName) -and ($_.ProcessId -eq $process.ProcessId)}
	Try {
		$result = $wmiProcess.Terminate()
		If ($result.ReturnValue -eq 0) {
			Write-Host " OK." -ForegroundColor Green
		} Else {
			Write-Host " failed: $($result.ReturnValue)" -ForegroundColor Yellow
		}
	} Catch {
		Write-Host " failed: $($_.Exception.Message)" -ForegroundColor Yellow
	}
}

Open in new window

Author

Commented:
Thank you all!

Big THANKS to oBdA. That script is extremely wicked! It's doing exactly what was needed in an amazing fashion! Wow
CERTIFIED EXPERT

Commented:
So small yet so elegant, great code.
Sam JacobsCitrix Technology Professional / Director of TechDev Services, IPM
CERTIFIED EXPERT

Commented:
You might also want to try this (on a DDC):

$user = Read-Host "Enter user name (in domain\user format)"
$application = Read-Host "Enter published application to log off" 

Add-PSSnapIn Citrix*

$sessions = Get-BrokerApplicationInstance | ? {$_.applicationName -eq $application -and $_.username -eq $user}

if ($sessions.Count -gt 0) 
{ 
    Write-Host "The following sessions will be logged off:"
    $sessions | Select Username, MachineName, ApplicationName | ft -auto

    $confirm = Read-Host "Do you wish to continue? (Y/N)"
    if ($confirm -eq 'Y')
    {
        Foreach ($session in $sessions)
        {
            Stop-BrokerSession $session.SessionKey
        }
    }
} 
else
{
    Write-Host "There are no sessions matching your criteria."
}

Open in new window