Jump to content


Photo

How to stop Citrix receiver with Powershell

Started by Randy White , 15 March 2017 - 01:25 PM
2 replies to this topic

Randy White Members

Randy White
  • 38 posts

Posted 15 March 2017 - 01:25 PM

Here is what I have 

 

 

Stop-Process -ProcessName Receiver -Force
Stop-Process -ProcessName selfservice -Force

 

 

It works as I would expect in Windows 7 but in Windows 10 its gives a crash error that the user has to click ok to close it. 

 

I'm looking for an easy way to kill receiver so that I can run other commands to delete old log files, and if receiver is open I get an error access denied. 



Andreas Schulz Members

Andreas Schulz
  • 37 posts

Posted 20 March 2017 - 05:21 AM

I have tested this command on my Windows 10 (1607) Notebook and it works perfect (no message):

 

Get-Process | sort description | ?{$_.description -like "Citrix*"} | Stop-Process -Force

 

I want to stop the processes always in the same order, thats the reasen for the sort.

 

Regards,

Andreas



Randy White Members

Randy White
  • 38 posts

Posted 25 May 2017 - 06:56 PM

here is my final script 

 

 

This was my final script.  

 

#Disables Mouse and keyboard
write-host "Your mouse and keyboard is disabled for 90 seconds while this script runs"
Start-Job -ScriptBlock { 
$code = @"
    [DllImport("user32.dll")]
    public static extern bool BlockInput(bool fBlockIt);
"@
 
$userInput = Add-Type -MemberDefinition $code -Name UserInput -Namespace UserInput -PassThru
 
function Disable-UserInput($seconds) {
    $userInput::BlockInput($true)
    Start-Sleep $seconds
    $userInput::BlockInput($false)
}
 
Disable-UserInput -seconds 90 | Out-Null  
}    
Sleep 5
Write-host "We're taking a screenshot"
Start-Process "C:\Program Files (x86)\Citrix\ICA Client\concentr.exe"
sleep 5
$File = "\\mydomain\citrix\Documentation\Citrix\ScreenShot\$($env:USERNAME)_$((Get-Date -Format "ddMMyyyyHHmm").ToString()).bmp"
param([string] $proc="C:\Program Files (x86)\Citrix\ICA Client\concentr.exe", [string]$adm)
cls
Add-Type @"
  using System;
  using System.Runtime.InteropServices;
  public class WinAp {
     [DllImport("user32.dll")]
     [return: MarshalAs(UnmanagedType.Bool)]
     public static extern bool SetForegroundWindow(IntPtr hWnd);
 
     [DllImport("user32.dll")]
     [return: MarshalAs(UnmanagedType.Bool)]
     public static extern bool ShowWindow(IntPtr hWnd, int nCmdShow);
  }
 
"@
$p = Get-Process |where {$_.mainWindowTItle }|where {$_.Name -like "$proc"}
if (($p -eq $null) -and ($adm -ne ""))
{
    Start-Process "$proc" -Verb runAs
}
elseif (($p -eq $null) -and ($adm -eq ""))
{
    Start-Process "$proc" #-Verb runAs
}
else
{
    $h = $p.MainWindowHandle
 
    [void] [WinAp]::SetForegroundWindow($h)
    [void] [WinAp]::ShowWindow($h,3);
}
 
Add-Type -AssemblyName System.Windows.Forms
Add-type -AssemblyName System.Drawing
# Gather Screen resolution information
$Screen = [System.Windows.Forms.SystemInformation]::VirtualScreen
$Width = $Screen.Width
$Height = $Screen.Height
$Left = $Screen.Left
$Top = $Screen.Top
# Create bitmap using the top-left and bottom-right bounds
$bitmap = New-Object System.Drawing.Bitmap $Width, $Height
# Create Graphics object
$graphic = [System.Drawing.Graphics]::FromImage($bitmap)
# Capture screen
$graphic.CopyFromScreen($Left, $Top, 0, 0, $bitmap.Size)
# Save to file
$bitmap.Save($File) 
Write-Output "Screenshot saved to:"
Write-Output $File
Sleep 5
Write-host "We're also emailing that screenshot to the VDI team"
$from = "myemail@mydomain.com"
$to = "myemail@mydomain.com"
$subject = "$env:USERNAME"
$body = "Screenshot of Connection Center $file"
$msg = "$file"
$Attachment = "$file"
 
$msg = new-object Net.Mail.MailMessage  
    $smtp = new-object Net.Mail.SmtpClient("mail.kiewit.com")  
    $msg.From = $From 
    $msg.To.Add($To)
    if($Attachment.Length -gt 1)
    {
        $msg.Attachments.Add($Attachment)
    }
    $msg.Subject = $Subject 
    $msg.IsBodyHtml = $true  
    $msg.Body = $Body 
    $smtp.Send($msg)  
 
Sleep 10
Write-host "We're reseting Citrix Receiver"
        start-process "C:\Program Files (x86)\Citrix\ICA Client\SelfServicePlugin\CleanUp.exe" "-cleanUser" 
sleep 30
Write-host "Deleting old Citrix files"
        Remove-Item $env:LOCALAPPDATA\temp\*.ica -ErrorAction SilentlyContinue
        Remove-Item $env:LOCALAPPDATA\temp\CTXReceiverLogs\1\* -Recurse -force -ErrorAction SilentlyContinue
        Remove-Item $env:LOCALAPPDATA\temp\CTXReceiverLogs\2\* -Recurse -force -ErrorAction SilentlyContinue
        Remove-Item $env:LOCALAPPDATA\temp\CTXReceiverLogs\3\* -Recurse -force -ErrorAction SilentlyContinue
        Remove-Item $env:LOCALAPPDATA\temp\CTXReceiverLogs -Recurse -force -ErrorAction SilentlyContinue
sleep 10
Write-host "Start Citrix Receiver and logging you in"
        Start-Process "C:\Program Files (x86)\Citrix\ICA Client\Receiver\Receiver.exe"
        Start-Process "C:\Program Files (x86)\Citrix\ICA Client\SelfServicePlugin\selfservice.exe"
#Write-host "Logging off all Citrix/VDI sessions"
#$user = $env:USERNAME
#Invoke-command -Credential "serviceaccount" -Computer server -scriptblock {param([string]$LocalUser) ; add-content "c:\temp\testing.txt" $localuser ; Add-PSSnapin Citrix* ; Get-BrokerSession -max 10000 | Where-Object brokeringusername -eq  "mydomain\$($LocalUser)"| Stop-BrokerSession } -ArgumentList $user
Sleep 10
Write-host "Done"
Sleep 10
$a = new-object -comobject wscript.shell 
$intAnswer = $a.popup("Did this fix the issue with Citrix/VDI?", 0,"Issue fixed",4) 
If ($intAnswer -eq 6) 
    { 
    $a.popup("Thank You!")
    } 
    else 
    { 
    #$a.popup("You answered no.")
    $intAnswer2 = $a.popup("Would you like to speak to KSS?", 0,"KSS Chat",4)
    if ($intAnswer2 -eq 6)
        {
         #$url = $wshell.Run("https://myemail@mydomain.com")
         $IE=new-object -com internetexplorer.application
         $IE.navigate2("myemail@mydomain.com")
         $IE.visible=$true 
        }
    }  
 
Remove-Variable * -ErrorAction SilentlyContinue