gooly
gooly

Reputation: 1341

win 7 scheduler does not start a powershell script - what to do?

I want the scheduler to start my powershell script. This script (re-)writes at first the time-stamp of its start:

   $StatusFile = $Env:USERPROFILE+"\Desktop\Status.log"
   set-content $StatusFile "Start $($((get-date).ToString('ddd. yyyy.MM.dd HH:mm:ss')))"

If this was written I can be sure the script (and not only powershell) has been started.

I defined in the scheduler for this task
- user: SYSTEM and
- the highest privileges and all the other stuff of the other tabs.

In the Actions-Tab I define:
1) Action = "Program start"
2) Programm/Skript = powershell.exe (or C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe)
3) Arguments (optional):

a)  C:\Users\calli\Desktop\TimeSync_v2.ps1
b) -file "C:\Users\calli\Desktop\TimeSync_v2.ps1"
c) -command "C:\Users\calli\Desktop\TimeSync_v2.ps1"
d) -file C:\Users\calli\Desktop\TimeSync_v2.ps1
   ....

No matter what I enter in the Arg.-Line I can always start the task and the task keeps running
but the file Status.log was never re-written. So the scheduler starts only powershell but not my script??

If I try (got that from google) to enter all in the Programm/Skript-line (leaving Args. empty) like:

 powershell.exe -file "C:\Users\calli\Desktop\TimeSync_v2.ps1"

(even here various versions) I immediately get a launch error and nothing was running.

I even tried to set: s.th. like kill a running instance if a new one is started (if I remember correctly).

What do I have to do? Where is my fault?

Thanks in advance,
Gooly

I added the result of my tries to run the script. I either get the launch error (2nd try) the other two are running but none of them writes the time-stamp in the log-File The initial script-lines:

  $StatusFile = $Env:USERPROFILE+"\Desktop\TimeSyncStatus.log"
  set-content $StatusFile "Starte TimeSync $($( (get-date).ToString('ddd. yyyy.MM.dd HH:mm:ss') ))"
  do { chkSync } while ( $true )

1st Try (briantist)

  Action: powershell -NoProfile -NonInteractive -NoLogo -ExecutionPolicy Bypass -File 'C:\Users\calli\Desktop\TimeSync_v2.ps1'"
  Add Args: -NoProfile -NonInteractive -NoLogo -ExecutionPolicy Bypass -File C:\Users\calli\Desktop\TimeSync_v2.ps1"
  Start in: (empty)
  Task Scheduler started "{fad9623d-7527-48c7-a95c-4adac7e0dba2}" instance of the "\start TimSync" task for user "WORKGROUP\MYMSI$".
  Task Scheduler launched action "powershell" in instance "{fad9623d-7527-48c7-a95c-4adac7e0dba2}" of task "\start TimSync".
  Task Scheduler launch task "\start TimSync" , instance "C:\Windows\System32\WindowsPowerShell\v1.0\powershell.EXE"  with process ID 5824.

2nd Try (launch error)

  Action: powershell "-NoProfile -NonInteractive -NoLogo -ExecutionPolicy Bypass -File 'C:\Users\calli\Desktop\TimeSync_v2.ps1'"
  Add Args:
  Start in:
  Task Scheduler launched action "powershell "-NoProfile -NonInteractive -NoLogo -ExecutionPolicy Bypass -File 'C:\Users\calli\Desktop\TimeSync_v2.ps1'"" in instance "{653f5e6a-e992-44d0-8189-fda1d44a114f}" of task "\start TimSync".
  Task Scheduler failed to launch action "powershell -NoProfile -NonInteractive -NoLogo -ExecutionPolicy Bypass -File 'C:\Users\calli\Desktop\TimeSync_v2.ps1'" in instance "{653f5e6a-e992-44d0-8189-fda1d44a114f}" of task "\start       TimSync". Additional Data: Error Value: 2147942523.
  Task Scheduler failed to start instance "{653f5e6a-e992-44d0-8189-fda1d44a114f}" of "\start TimSync"  task for user "WORKGROUP\MYMSI$" . Additional Data: Error Value: 2147942523.

3rd Try (Matt)

  Action: %SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe
  Add arguments: -ExecutionPolicy Unrestricted -NoProfile -File C:\Users\calli\Desktop\TimeSync_v2.ps1
  Start in: Start in (optional): %SystemRoot%\system32\WindowsPowerShell\v1.0
  Task Scheduler started "{3c00ad49-e8a0-48a2-9578-90b512db0932}" instance of the "\start TimSync" task for user "WORKGROUP\MYMSI$".
  Task Scheduler launched action "%SystemRoot%\system32\WindowsPowerShell\v1.0\powershell.exe" in instance "{3c00ad49-e8a0-48a2-9578-90b512db0932}" of task "\start TimSync".
  Task Scheduler launch task "\start TimSync" , instance "C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe"  with process ID 8980.

Upvotes: 2

Views: 1694

Answers (1)

briantist
briantist

Reputation: 47772

Try using these arguments:

-NoProfile -NonInteractive -NoLogo -ExecutionPolicy Bypass -File "C:\path\to\your\file.ps1"

If this doesn't work please post the actual error message if you have it.

Alternatively use psexec to run a command prompt as SYSTEM and them run the same command you're running from the task, that way you can see the output.

Upvotes: 1

Related Questions