SID-500

Home » PowerShell » PowerShell: How to automatically restart applications when they were closed

PowerShell: How to automatically restart applications when they were closed

If you want an applicatio always started and available then this article is for you. Sometimes an application is mistakenly closed by the user. This article shows a step-by-step scenario how to configure a scheduled task watching the status of your application.

Prerequisites

Well, we are going to use a PowerShell Three-Liner. For the execution of PowerShell script files we have to take a glance at the PowerShell Executionpolicy. Make sure the policy setting is configured as remotesigned (recommended) or unrestricted.

Get-ExecutionPolicy

1.PNG

If it shows Restricted, then run

Set-ExecutionPolicy remotesigned -Force

2.PNG

PowerShell script for checking the status of the application (started / not started)

Open PowerShell ISE. (Type ise in PowerShell). Copy the code in your ISE session.

If (!(Get-Process -Name notepad.exe -ErrorAction SilentlyContinue))
{Invoke-Item C:\Windows\notepad.exe
}

Let’s stay here for a short explanation. The If Statement checks if notepad.exe is running. The ! at the beginning of the command is a NOT statement. Only if notepad is not running, Invoke-Item will start notepad again.

Save the script. I name it notepad.ps1.

Unbenannt.PNG

Now we can move on and include the script in a scheduled task. Unfortunately, I have to tell you this is not a good solution. Why? PowerShell will pop up every time the script runs. That’s annoying. The good news: I have a workaround for that. We call the PowerShell script from a vb script!

Creating the VB script

Open notepad. Type the following:

command = "powershell.exe -nologo -command C:\Temp\notepad.ps1"
set shell = CreateObject("WScript.Shell")
shell.Run command,0

Unbenannt.PNGNow we have two files: A PowerShell Script and a VB Script.

Unbenannt.PNG

Configuring a Scheduled Task to run every 60 seconds

Open PowerShell ISE. First, think about which user you want to create the task for. In the following code example the task will be configured for the local user account patrick. Pay also attention to the path of your VB script (Line 1):

$Action=New-ScheduledTaskAction -Execute "C:\Temp\notepad.vbs"
$Trigger=New-ScheduledTaskTrigger -Once -At (Get-Date) -RepetitionInterval (New-TimeSpan -Minutes 1)
$Set=New-ScheduledTaskSettingsSet
$Principal=New-ScheduledTaskPrincipal -UserId "patrick" -LogonType Interactive
$Task=New-ScheduledTask -Action $Action -Trigger $Trigger -Settings $Set -Principal $Principal
Register-ScheduledTask -TaskName "Start Notepad" -InputObject $Task

When Patrick logs on and if notepad is not already started, then the task will start it. That happens every 60 seconds. Configurations less than 1 minute are not supported.

No pain no gain

End of subject.

Writing that Scheduled Task script was a nightmare. I had a typo and a logical error and couldn’t find them for over an hour. No pain no gain. 😉

Don’t worry. All of the above is working. Have fun with it!

Further thoughts

If you like Scheduled Tasks and PowerShell, then have a look at my articles

How to schedule software installation with PowerShell

Configuring the automatic start of PowerShell at every logon


2 Comments

  1. […] PowerShell: How to automatically restart applications when they are closed […]

    Like

  2. […] PowerShell: How to automatically restart applications when they are closed […]

    Like

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s

Categories

WHOIS

My name is Patrick Grünauer (pewa2303). I am from Austria. On sid-500 I write about Windows, Cisco and IT-Security in English and German. Have fun while reading!

Patrick Gruenauer
Follow SID-500 on WordPress.com