2

I have a Powershell script, which is working just fine, when called from command line, but only partially fine when executed by the Windows Task Scheduler. The script looks as follows:

# Do things, which are always working.
$session = new-pssession -computername SRV
Invoke-Command -session $session -scriptblock { D:\script.bat }
# Do things, which are always working.

The task defined in the Task Scheduler is completed without errors. As you see, all parts before and after Invoke-Command are working, also when called by the Task Scheduler. Only the Invoke-Command itself is only working when called from the command line.

My only guess is, that the Powershell script is exiting prematurely, but I didn't find any way to confirm this or even solve the issue.

sk904861
  • 1,247
  • 1
  • 14
  • 31

2 Answers2

1

it is likely to be a permissions issue - as a test, run the task using the same credentials as when it is run at the command line...

Jimbo
  • 2,529
  • 19
  • 22
-1

I had the same issue with Invoke-Command when I called a script from Task Scheduler to run certain scriptblocks on multiple computers. I don't know why, but setting the "Start in" folder of the action to the folder of the script solved it. action configuration window

henrycarteruk
  • 12,708
  • 2
  • 36
  • 40
  • This would only solve the issue if OP was using a relative path to batch file eg `Invoke-Command -session $session -scriptblock { script.bat }` rather than using the full path to the file like they are `Invoke-Command -session $session -scriptblock { D:\script.bat }` – henrycarteruk Dec 12 '16 at 17:11
  • I also used pull path to the script (see screenshot) and still invoke-command did not work, only after I set the "Start in" folder. Perhaps any valid "Start in" could work, but I did not test that. – Attila Kovács Dec 14 '16 at 08:14
  • I mean **"full path"**. – Attila Kovács Dec 14 '16 at 08:20