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