AutomoxAutomox

Kill All Processes - Run from automox

ajamaya
January 18, 2024 at 8:57 pm UTC
#community#Worklets#powershell#automox#patching catalog updates

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."
 

This discussion is archived and closed to replies. Continue it in the Automox Community Slack.

Discuss on Slack

Replies (5)

AnthonyMJanuary 24, 2024
#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.

 

ajamayaJanuary 25, 2024
#2

@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. 

AnthonyMJanuary 25, 2024
#3

@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?

 

ajamayaJanuary 25, 2024
#4

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

ajamayaJanuary 26, 2024
#5

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