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:
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:
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!
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!