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?
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.
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.
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:
(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).
(argument field in this case contains -Command “& ‘C:\Scripts\Get-HyperVReport.ps1‘“ – be really careful about the single/double quotes here!)
Configured like this, our scheduled task will execute the following command:
1 |
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command "& 'C:\Scripts\Get-HyperVReport.ps1'" |
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:
(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:
1 |
C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -Command "& 'C:\Scripts\Get-HyperVReport.ps1' -ClusterName MyCluster -SendMail $true -SMTPServer smtp.mail.com -MailFrom [email protected] -MailTo [email protected]" |
Hope this helps!
Cheers!
P.S. One other other thing (yes, it’s written twice… live with it ) 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.
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!
Is there any way to pass user input as arguments to the schtask and the powershell script?
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.
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.
So… it looks that UAC is actually giving you headaches. Did you try https://technet.microsoft.com/pt-pt/library/ee844140(v=ws.10).aspx?
Thank you for this how-to!
You are welcome! It ain’t much, but… glad it helps!
—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
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)?
Is there any way to schedule a PS script which calls another PS script which has parameters?
Maybe something like this will help you – https://stackoverflow.com/questions/12850487/invoke-a-second-script-with-arguments-from-a-script.
Great blog post! Thank you.
Thank you!
thanks!
You’re welcome! 🙂
thank you!
Glad it helped! 🙂
e tocno to sam trazio! drugi rezultat na guglu 🙂
Drago mi je da si i našao! 🙂
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. 😉
Glad it helped! And thanks for the info! 😊