Powershell script to verify if a process is running from correct path

Simple script to verify if a process is running from correct path;
# Variables
$Time = [system.DateTime]::Now 
$processpath = 'C:\Program Files\Microsoft SQL Server\110\LocalDB\Binn\sqlservr.exe'

# Get running process with name sqlservr and filter based on correct path
$test = Get-Process sqlservr | ? { $_.Path -eq $processpath }

if ($test.HasExited -eq $False) {
 # Replace spaces in path with underscore
 $procpath = $test.path.tostring().Replace(' ', '_')
 write-host "0" $procpath "- OK - process is running ($Time)"

 }
 else {
 # 2 Critical - process is not running
 write-host "2 sqlservr.exe not running - Critical - process is not running ($Time)"

}

No comments: