Announcement

Collapse
No announcement yet.

schtasks.exe need to pass parameters to script

Collapse
X
  • Filter
  • Time
  • Show
Clear All
new posts

  • schtasks.exe need to pass parameters to script

    Hi,
    I want to run a task in Windows 2008, the Script.cmd need to get a parameter %user%

    how can I run schtasks.exe and pass the parameters to script

    Thanks!

  • #2
    Re: schtasks.exe need to pass parameters to script

    There is an environment variable %username% which you can use in the .cmd script. I need more information if you need more help. What are you trying to run give me examples.
    Andrew

    ** Remember to give credit where credit is due and leave reputation points sigpic where appropriate **

    Comment


    • #3
      Re: schtasks.exe need to pass parameters to script

      Where is this %user% variable defined? Since it is an environment variable, why passing via a command line as an parameter and not just exand the variable in the batch?

      And like Andrew already wrote, in the batch you can use the environment variable %username% what will exand the logonname of the user.


      Although, to answer your question,
      yes it is possible to pass parameters to script via the schtasks.exe command line.

      The path and file name of the task to be run at the scheduled time will be specified with the TaskRun switch [ /tr ]. For example:
      - /tr C:\test\myscript.cmd
      Where the value after ' /tr ' is read as a string - if you want to pass parameters then the string will be containing a space between each parameter, therefore the entire string must be between quotes. For example:
      - /tr "C:\test\myscript.cmd argument1 argument2"

      You also have to use quotes around the string if the path or file name would have contained spaces. It is a good practice to always use quotes for the '/tr' string value.

      When you use a path that has embedded spaces and also want to add parameters, just enclose the string between quotes, it is not nessesary to separately put quotes around the path.

      But if you do like (or have to) to use quotes within the quoted string, you'll have to escape all the quotes on the command line that are between quotes, otherwise the second quote will be interpreted as an end-of-string quote. Escaped quotes are entered like: \" instead of ". For example:
      - /tr "\"C:\test 2\myscript.cmd\" argument1 argument2"

      When you use one parameter that is containing spaces you'll have to put that parameter seperately between quotes.

      The syntax when you want both the path\file and the parameter be between quotes would be like:
      Code:
      /create /tn "Run my script" /tr "\"C:\test 2\myscript.cmd\" \"one of the arguments to pass\""

      FYI
      Use the escape sign "\" for the quotes that are between quotes with all kind of command lines (especially for Runas.exe command lines).



      \Rems
      Last edited by Rems; 3rd January 2009, 11:19.

      This posting is provided "AS IS" with no warranties, and confers no rights.

      __________________

      ** Remember to give credit where credit's due **
      and leave Reputation Points for meaningful posts

      Comment


      • #4
        Re: schtasks.exe need to pass parameters to script

        Many thanks!

        Comment


        • #5
          Re: schtasks.exe need to pass parameters to script

          @Rems great post!
          I need a clarification though,shouldn't the code be:

          /create /tn "Run my script" /tr "\"C:\test 2\myscript.cmd\" \"one of the arguments to pass\" theotherargument"
          ?

          Comment


          • #6
            Re: schtasks.exe need to pass parameters to script

            Originally posted by discovery2009 View Post
            @Rems great post!
            I need a clarification though,shouldn't the code be:

            /create /tn "Run my script" /tr "\"C:\test 2\myscript.cmd\" \"one of the arguments to pass\" theotherargument"
            ?
            Nope it is just the one parameter that I've called "one of the arguments to pass", the name is irrelevant. The made up batch "myscript.cmd" knows how to deal with it. Hahahah

            Result of my line how it will be visible in the scheduled task "Run my script":
            C:\test 2\myscript.cmd "one of the arguments to pass"

            And this is how your line will show up in the task:
            C:\test 2\myscript.cmd "one of the arguments to pass" theotherargument
            have to agree, this looks more reasonable because of the text of the sample arguments .


            And you can add many more parameters if applicable.
            Like four parameters where two are having spaces (i.e. could be a path name) in the name:
            Code:
            /create /tn "Run my script" /tr "C:\test 2\myscript.cmd \"one of the arguments to pass\" anotherargument \"This is the third argument\" lastargument"
            will result in:
            C:\test 2\myscript.cmd "one of the arguments to pass" anotherargument "This is the third argument" lastargument


            Thanks discovery2009!

            \Rems

            This posting is provided "AS IS" with no warranties, and confers no rights.

            __________________

            ** Remember to give credit where credit's due **
            and leave Reputation Points for meaningful posts

            Comment

            Working...
            X