Scheduling a PowerShell script… with arguments

Let’s say that you have a neat PowerShell script, which you want to run on some kind of a schedule (a script which will collect some data and send you an e-mail, every day in the same hour… ‘til the end of time – maybe this one) – how can you do it? Smile

The answer is simple. Yes, there is a tool included with Windows operating system, which can help you… and it’s called… well – Task Scheduler. Smile

And… if you never used the Task Scheduler in Windows, maybe this is the time to start.

It’s a rather simple tool – you click through a simple wizard, select what you need (a program) and when you need it, and you’ve created a scheduled task.

image

OK… so, I can run a program? What about a PowerShell script?

The real question here is “Who/what will run my PowerShell script?”. And then, the answer is simple – the PowerShell engine.

This means that your “program” is powershell.exe. This also means that in your scheduled task you should enter something like this:

image

(note the full path to powershell.exe – C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe)

Now we have a scheduled task which will start PowerShell in designated time, every single day. Usually, this is not enough, and we need to add some arguments to the command running (like the path to the script we want to execute).

image

(argument field in this case contains -Command “& C:\Scripts\Get-HyperVReport.ps1be really careful about the single/double quotes here!)

Configured like this, our scheduled task will execute the following command:

Two remaining things that we have to check is to have our Get-HyperVReport.ps1 script saved in C:\Scripts and that user, under whom this task is running, has the appropriate permissions to run it. Also, if task should be running unnatended (usually it should), make sure to configure it so.

One other thing that may be useful – with this script, we need to specify some additional parameters (like ClusterName or if it will send us an e-mail when completed). In this case, we can easily add the required parameters to the arguments field, like this:

image

(argument field in this case contains -Command “& C:\Scripts\Get-HyperVReport.ps1‘ -ClusterName MyCluster -SendMail $true -SMTPServer smtp.mail.com -MailFrom [email protected] -MailTo [email protected])

The whole command is then:

Hope this helps!

Cheers!

P.S. One other other thing (yes, it’s written twice… live with it Smile) that can be useful – you can also use PowerShell to create scheduled task which will run this PowerShell script (instead of using “the lame wizard”). Pssst… take a look at the New-ScheduledTask command. Winking smile

P.P.S. You can also make use of Adam’s function, which will make your life easier – https://github.com/adbertram/Random-PowerShell-Work/blob/master/Scheduled%20Tasks/New-ScheduledScript.ps1. Thanks, Adam!

20 Comments

    • Hm… I’m not sure what will be the end goal of scheduling a task and waiting for user’s input? Maybe I’ve missed the point, but for the script that requires user’s input, I’ll make the user run it… not Task Scheduler.

      Reply
  1. This is somewhat related to a project that I have inherited. I am very new to powershell and so far I have not been able to accomplish the task. I am looking for a way, to allow a non-admin user (from their own workstation), to have the ability to run a batch or powershell application on a server as administrator. This script copies a known directory structure and sub directories and completely maintains all permissions. Copying the folder structure is easily accomplished with robocopy which will also keep all permissions in tacked. I came across your blog post when searching for a way to pass arguments to powershell scripts within task scheduler thinking that was a possible way of completing the task. Administrator level previlege is required in order to actually copy the permissions.The robocopy command work quite well when I run it directly from my server as admin. The challenge is allowing users to be able to run it from their desktop. Oh, one key piece of information is that the user on their desktop is prompted for a number. /That number needs to be passed to the server script, and then robocopy copies my predefined directory structure underneath that entered number. I have struggled to make this work for about a week.

    Reply
  2. —Quote—
    P.S. One other other thing (yes, it’s written twice… live with it Smile) that can be useful – you can also use PowerShell to create scheduled task which will run this PowerShell script (instead of using “the lame wizard”). Pssst… take a look at the New-ScheduledTask command. Winking smile
    —Quote—
    How would the -Argument Part of the New-ScheduledTaskAction have to be for this?
    -Command “& ‘C:\Scripts\Get-HyperVReport.ps1′”

    I have a problem to find the correct quotation…

    Greetings
    Christian

    Reply
    • Have you tried inverting the quotation marks –> -Command ‘& “C:\Scripts\Get-HyperVReport.ps1″‘ (and with this path you don’t even need the internal ones)?

      Reply
  3. Thanks! This FINALLY solved my problems.. Turned out I needed single-quotes because I pass arguments in the scheduled task AND double-quotes because of the space in C:\Program Files…

    I had everything in double-quotes and that didn’t work.
    But learned a lot about PowerShell, EventLog, Troubleshooting along the way. 😉

    Reply

Leave a Comment.