Question

Kill All Processes - Run from automox

  • 18 January 2024
  • 5 replies
  • 109 views

Badge

Hello Team-

We want to close all running processes without user intervention. I have tested this script locally and have no issues however, I am unable to get it to process through automox. Any ideas?

 

# Get the current PowerShell process
$currentProcess = Get-Process -Id $PID

# Processes to exclude from closure
$excludeProcesses = @("amagent", "powershell")

# Get a list of all running processes except the current PowerShell process and the excluded processes
$runningProcesses = Get-Process | Where-Object { $_.MainWindowTitle -ne "" -and $_.Id -ne $currentProcess.Id -and $_.ProcessName -notin $excludeProcesses }

# Iterate through the list of processes and close them
foreach ($process in $runningProcesses) {
    Write-Host "Closing $($process.ProcessName)..."
    $process.CloseMainWindow()
    
    # Wait for a few seconds to allow the application to close gracefully
    Start-Sleep -Seconds 5
    
    # If the application is still running, terminate it forcefully
    if (!$process.HasExited) {
        Write-Host "Forcefully terminating $($process.ProcessName)..."
        $process | Stop-Process -Force
    }
}

Write-Host "All running applications, except PowerShell, amagent have been closed."
 


5 replies

Userlevel 1

Good morning @ajamaya !

 

What problem are you running into testing through Automox? I don’t see a problem with the code myself but I could see a few spots where the action might not be exactly what you actually want to happen.

 

Look forward to your reply!

 

Anthony M.

 

Badge

@AnthonyM-Automox I attempt to run it out of automox but it never kills any of the applications. I am not sure if I need to exclude something out of it or how does automox process it. 

Userlevel 1

@AnthonyM-Automox I attempt to run it out of automox but it never kills any of the applications. I am not sure if I need to exclude something out of it or how does automox process it. 

That is perplexing as I’m testing on my end and I don’t see any issues.

 

I do know CloseMainWindow() can fail in cases where the application can minimize to the system tray; but otherwise this is working as expected on my end.

 

What applications are you seeing fail to close, specifically?

 

Badge

None of the applications. Chrome, notepad++, Office 365 apps, etc. How are you running it out of automox?

Badge

I think this may be an issue with powershell. I have another msiexec command that runs locally but not within automox as well. 

Reply