3

My job is to create a script which determines if Outlook is open, if it is open. The script should open a prompt and ask:

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

I can give you the code I already have, my problem is the code works apart from determining if it is really running, this means it opens the prompt always 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
}
Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
Valentino
  • 43
  • 2
  • 8

1 Answers1

3

You have to change the first two lines of your script:

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

Get-Process takes the process name, not the name of the executable (thus outlook instead of outlook.exe). Also your if statement should check whether there is a value within $ProcessActive.

Martin Brandl
  • 56,134
  • 13
  • 133
  • 172
  • Thanks very much everything worked fine, can you tell me why did you removed my "thanks guys" from my question I would like to know – Valentino May 12 '17 at 09:49
  • Stackoverflow questions tends to be read by hundreds of developer facing a similar issue. Removing comments like that will allow users to read the questions faster. – Martin Brandl May 12 '17 at 09:51