Abhinay
Posts: 36
Joined: Wed Feb 27, 2019 9:33 am

Could not close the process when the process is already running in the installer

I was able to execute the below code successfully in the powershell ISE window, but the same code that is the 2nd if condition does not work when i make it as part of the installer, Can you please suggest if i missed any code

Code: Select all

Param()
$ProcessName = "EngineMonitor"
if((get-process $ProcessName -ErrorAction SilentlyContinue) -eq $Null)
{ Start-Process -FilePath "EngineMonitor.exe" -WorkingDirectory "D:\Sinet\EngineMonitor" }else{ Start-Sleep -s 15
 Get-Process EngineMonitor | Stop-Process}

if((get-process $ProcessName -ErrorAction SilentlyContinue) -ne $Null)
{  
Start-Sleep -s 60
 Get-Process EngineMonitor -ErrorAction SilentlyContinue | Stop-Process
 Get-Process siNetNodeWPF -ErrorAction SilentlyContinue | Stop-Process
 Get-Process siNetNodeWPF -ErrorAction SilentlyContinue | Stop-Process}
Catalin
Posts: 7492
Joined: Wed Jun 13, 2018 7:49 am

Re: Could not close the process when the process is already running in the installer

Hello Abhinay,

First of all, what you mean by "does not work"? It does not do what you expect it to do or it fails with an error? If it is the second one, please give me some more details about what the error is.

So, from what I understand from the presented code, you want to start a process if it does not exist and stop it if it exists. Please correct me if i am wrong.

Here is how I understand your code:

Code: Select all

if the process is not started
       start it
else stop it
In my opinion, the second "if" is additional. What I mean here is the fact that the "else" actually means your second if.

If your condition looks like this:

Code: Select all

if (something -eq $null)
then it means that the "else" branch means that:

Code: Select all

if (something -ne $null)
which is the same as your second if, thus you do not need it.

Also, this line is doubled here (you have it twice in your code):
Get-Process siNetNodeWPF -ErrorAction SilentlyContinue | Stop-Process
Hope this helps.

Best regards,
Catalin
Catalin Gheorghe - Advanced Installer Team
Follow us: Twitter - Facebook - YouTube

Return to “Common Problems”