0

I am trying to get a specific restart command to a cmd.exe window at a particular time in the day everyday in order to restart the server it is running.

My Action in Task Scheduler is currently:

Powershell.exe -ExecutionPolicy Bypass -File C:\Rustide\restart.ps1 -verbose >> C:\Rustide\myscript.log 2>&1"

It does not actually create the log, so I do not think that powershell is getting launched. Since I need the command to run and find a window on my profile, it is set to run off my profile. The script works perfectly when I execute it on my own. Is there a reason or security measure that is built in to task scheduler that is not letting me do this? If so, is there a good alternative?

I have tried this with the only when user is logged on, whether user is logged on or not, with highest privileges, etc. Nothing seems to work.

PowerShell Code

Start-Transcript -Path C:\Rustide\restart.log
Import-Module WASP
Select-Window | Where { $_.Title -Like '*OSF*Oxide' } | Set-WindowActive | Send-Keys 'restart'
start-sleep -seconds 2

Log from executing through task scheduler.

**********************
Windows PowerShell transcript start
Start time: 20160725080935
Username: OSF-WS2012\OSF
RunAs User: OSF-WS2012\OSF
Machine: OSF-WS2012 (Microsoft Windows NT 6.3.9600.0)
Host Application: Powershell.exe -ExecutionPolicy Bypass -File C:\Rustide\restart.ps1 Start-Transcript -Path C:\Rustide\restart.log
Process ID: 10588
**********************
Transcript started, output file is C:\Rustide\restart.log

PS>$global:?
True
**********************
Windows PowerShell transcript end
End time: 20160725080937
**********************
Slacker101
  • 23
  • 6
  • Which PS version are you running? (`$PSVersionTable` shows this). You can always start a transcript at the beginning of your script as follows `Start-Transcript -Path C:\Rustide\restart.log` to see if anything gets logged at all. If the transcript isn't created it means your script isn't run in the first place. – megamorf Jul 25 '16 at 08:00
  • Version 4.0, just checked – Slacker101 Jul 25 '16 at 08:05
  • First try invoking `Start-Transcript -Path C:\Rustide\restart.log` in a regular PS console to see that is supported on your OS. I remember it not being available on older client operating systems in the past. If that works you just add the command to your `C:\Rustide\restart.ps1` script - right at the beginning so that logging starts as soon as possible. Then kick off the scheduled task and check if the restart.log was created. – megamorf Jul 25 '16 at 08:08
  • The output above is from the scheduled task? – megamorf Jul 25 '16 at 08:13
  • Correct, it is from running it through task scheduler. – Slacker101 Jul 25 '16 at 08:15
  • Please read my instructions again. I didn't want you to modify the scheduled task command. – megamorf Jul 25 '16 at 08:17
  • It was added to the script and executed through the task scheduler. I think you misunderstood what I meant. I did not add it to the command. – Slacker101 Jul 25 '16 at 08:22
  • typo error in your command : `Powershhell.exe` no double h in powershell ;) – Loïc MICHEL Jul 25 '16 at 09:10
  • Unfortunately that was not a direct copy and paste, I did not have the extra h in the command. I really wish that had been the issue. :( – Slacker101 Jul 25 '16 at 20:03

0 Answers0