0
votes

I'm very new to powershell and server management. I've created a simple powershell script that stops and starts a program. This script works without issues when I run it manually but when Windows Task Scheduler runs it, it will stop the program if it's running, but it won't start it.

Windows Task Scheduler is reporting "This operation completed successfully. (0x0)"

I have transcript set on the script and it's also reporting no errors.

My task is set as follows;

User; Administrator
Run whether user is logged on or not
Run with highest privledges is checked
Configure for: Windows 7, Windows Server 2008 R2

Trigger:

Start one time
Repeat every 5 minutes indefinitely

Action:

Program/script:
powershell.exe

Add arguments: -Executionpolicy Bypass -file "C:\DiscordIntegratorBot\restart.ps1"

restart.ps1 contents:

start-transcript "c:\temp\script.log"

$processname = Get-Process -Name "DiscordIntegratorBot"

if ($processname) {
  Stop-Process -Name $processname.Name -Force
}

Start-Process -FilePath "C:\DiscordIntegratorBot\DiscordIntegratorBot.exe"

stop-transcript

script.log contents:

**********************
Windows PowerShell transcript start
Start time: 20200804113001
Username: NS1001457\Administrator
RunAs User: NS1001457\Administrator
Configuration Name: 
Machine: NS1001457 (Microsoft Windows NT 10.0.17763.0)
Host Application: powershell.exe -Executionpolicy Bypass -file C:\DiscordIntegratorBot\restart.ps1
Process ID: 6312
PSVersion: 5.1.17763.1007
PSEdition: Desktop
PSCompatibleVersions: 1.0, 2.0, 3.0, 4.0, 5.0, 5.1.17763.1007
BuildVersion: 10.0.17763.1007
CLRVersion: 4.0.30319.42000
WSManStackVersion: 3.0
PSRemotingProtocolVersion: 2.3
SerializationVersion: 1.1.0.1
**********************
Transcript started, output file is c:\temp\script.log
**********************
Windows PowerShell transcript end
End time: 20200804113001
**********************

I'm out of ideas of what could be the problem now and appreciate any help.

1

1 Answers

0
votes

I was able to fix my own problem after much more googling..

When setting a task to "run whether user is logged on or not" means the application gets ran in the background. I was checking to see if the program GUI had popped up, not the processes window.