Execute only when the process is running

My task is to create a script that detects if Outlook is open if it is open. The script should open up a prompt and ask:

Outlook is open, click Yes to close outlook and continue the script, or click no to exit the script.

I can give you the code I already have, my problem is that the code works separately from detecting if it is actually running, which means it always opens the prompt even when Outlook is closed.

$ProcessActive = Get-Process outlook.exe -ErrorAction SilentlyContinue
if($ProcessActive -eq $null)
{
 #prompt yes or no
$a = new-object -comobject wscript.shell
$intAnswer = $a.popup("Outlook seems to be open; Press Yes if you want to close Outlook and continue or press No to terminate the script", `
0,"Delete Files",4)
If ($intAnswer -eq 6) {

#kill outlook

  $ProcessName = "outlook"
If ($Process = (Get-Process -Name $ProcessName -ErrorAction SilentlyContinue)) {
    "Closing $($ProcessName) ..." | Write-Host
    $Process.Kill()

}
} 

#exit script
else {
  exit
}

      

+3


source to share


1 answer


You must change the first two lines of your script:

$ProcessActive = Get-Process outlook -ErrorAction SilentlyContinue
if($ProcessActive)

      



Get-Process

accepts a process name, not an executable file name (thus outlook instead of outlook.exe). Also your if statement should check if there is a value inside $ProcessActive

.

+3


source







All Articles