Intended for use as part of a bigger script where you need to check if a process or processes are running or closed. Checks if an array of processes are running on the system, and counts how many of them are running in an integer variable.
#Declare variables
[Int]$intRunning = 0
[bool]$Debug = $true
#Main logic
function Main
{
$menucolor = [System.ConsoleColor]::white
write-host '-------------------------------------------------------------------'-ForegroundColor $menucolor
write-host '| Check if processes are running |'-ForegroundColor $menucolor
write-host '| Jan Kåre Lokna |'-ForegroundColor $menucolor
write-host '| v 1.0 |'-ForegroundColor $menucolor
write-host '-------------------------------------------------------------------'-ForegroundColor $menucolor
write-host
checkProcesses
}
#Check processes
function checkProcesses
{
$processes = "iexplore", "winamp", "Opera", "dfdsafs"
foreach ($process in $processes)
{
try
{
$s= Get-Process $process -ErrorAction stop
if($Debug -eq $true) {write-host $process 'is running'-ForegroundColor Green}
$intRunning = $intRunning + 1
}
catch
{
if($Debug) {write-host $process 'is not running' -ForegroundColor Magenta}
}
}
if($Debug) {Write-host "Running processes: " $intRunning "of" $processes.count}
}
. Main