-2

I have a folder with a PowerShell script and a .cmd file that executes it (among other things).

Folder

I want task scheduler to launch Run.cmd but it won't work. Manually launching Run.cmd works and so does launching just the .ps1 by itself. Here is how I set up my task scheduler:

Taskscheduler

Additional information:

  • The script backs-up our network switches

  • The .ps1 by its nature throws up a few non-critical errors (doesn't stop the script)

  • Run.cmd is this:

    @echo off
    powershell.exe -ExecutionPolicy Bypass -file "BackupSwitch.ps1"
    %SystemRoot%\System32\tree.com "%~dp0backups" /f
    TIMEOUT /t 10
    
Ryan
  • 85
  • 1
  • 4
  • 11
  • This question is probably more suitable for ServerFault or SuperUser, however... Do you get any error messages? Is the task being run with a service account and does the account have "Log on as batch"-rights on the machine? – notjustme Jun 28 '18 at 14:49
  • Personally I would put everything into the powershell script then schedule that. – EBGreen Jun 28 '18 at 14:50
  • No errors, says ran successfully but didn't actually back anything up. I'm logged in as an admin to our server and "log on as batch" rights are set for all users and admins – Ryan Jun 28 '18 at 14:56
  • [Related](https://stackoverflow.com/a/41635982/1630171). – Ansgar Wiechers Jun 28 '18 at 15:04
  • 1
    In the `Start in (optional):` there's an opportunity to input a location, perhaps including the path `C:\!tools\Network_Script_Backups` there and changing your script to use `-file ".\BackupSwitch.ps1"` and `"backups" /f` would be easier. An alternative to completing the `Start in (optional):` location you could make those two changes and instead add a new line `2`, `CD /D "C:\!tools\Network_Script_Backups"` as an option. – Compo Jun 28 '18 at 15:23

1 Answers1

2

The bat file is running, but as you're using relative paths with powershell and tree.com, the paths are incorrect as CMD will start in C:\Users\USERNAME by default.

To correct this you just need to add C:\!tools\Network_Script_Backups to the Start-in field.

henrycarteruk
  • 12,708
  • 2
  • 36
  • 40