none
Running Powershell script, with arguments, as a scheduled task RRS feed

  • Question

  • Hi all,

    I've tried searching/various different string patterns, but I've had no joy with this.

    I'm trying to do two things I guess, but solving one should solve the other. I've got a Powershell script, which takes too arguments (via params), which I'm trying to run as a scheduled task.

    I can get the script to run, but not with the arguments passed in. Any help on the string needed here would be great!

    The other issue is that, rather weirdly, the script has the option to schedule itself depending on user need, so I'm trying to use schtasks to schedule this script (from within Powershell). Again, I fall down when trying to pass in the arguments.

    So, I guess the main question here is: "how do I create a scheduled task for a Powershell script, with arguments, within Powershell?"

    This is the string with Powershell I'm working with, if it helps:

    "

    C:\Windows\System32\schtasks.exe /create /TN $taskName /ST $time /SC DAILY /SD $startDate /ED $endDate /TR powershell.exe "`"C:\users\myprofile\desktop\scriptname.ps1 -mode $serviceChoice -list $listChoice`""

    "

    (^ That's the latest permutation, there have been others)

    Cheers for any help! 

    Joe

    Edit - I've since found enclosing the whole thing after /TR in quotes allows it to create a scheduled task, but when it runs, it pops up a PS window full of red text, but I can't read or print screen it in time...


    • Edited by TriggerFish91 Tuesday, January 27, 2015 3:14 PM Updated situation
    Tuesday, January 27, 2015 3:08 PM

Answers

  • The following may work:


    schtasks.exe /create /TN $taskName /ST $time /SC DAILY /SD $startDate /ED $endDate /TR "powershell.exe -file C:\users\myprofile\desktop\scriptname.ps1 -mode $serviceChoice -list $listChoice"

    Note the use of the -file parameter to powershell.exe.


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by John-R Wednesday, January 28, 2015 9:59 AM
    • Marked as answer by TriggerFish91 Wednesday, January 28, 2015 10:00 AM
    Tuesday, January 27, 2015 4:15 PM
    Moderator

All replies

  • The following may work:


    schtasks.exe /create /TN $taskName /ST $time /SC DAILY /SD $startDate /ED $endDate /TR "powershell.exe -file C:\users\myprofile\desktop\scriptname.ps1 -mode $serviceChoice -list $listChoice"

    Note the use of the -file parameter to powershell.exe.


    -- Bill Stewart [Bill_Stewart]

    • Proposed as answer by John-R Wednesday, January 28, 2015 9:59 AM
    • Marked as answer by TriggerFish91 Wednesday, January 28, 2015 10:00 AM
    Tuesday, January 27, 2015 4:15 PM
    Moderator
  • Thanks Bill - I've got it working now :)

    ----

     C:\Windows\System32\schtasks.exe /create /TN $taskName /ST $time /SC DAILY /SD $startDate /ED $endDate /TR "powershell.exe C:\users\myprofile\desktop\myscript.ps1 -mode $serviceChoice -list $listChoice"

    ----

    Although, oddly, it will also work without the -file option too, as shown above. Anyway, thanks again for the help!

    Joe


    Wednesday, January 28, 2015 9:03 AM