PowerShell How-To

Automate the Creation of Scheduled PowerShell Scripts

Here's how to set it and forget it!

Have you ever needed to schedule a PowerShell script? While you may not need to execute the script right away, you may have a process that must be done daily, monthly or somewhere in between. Common examples include log rotation schedules, server reboots, backups, and so on. Do you schedule these scripts now? If so, you're probably scheduling them with the Windows Task Scheduler, and chances are you're doing it via the GUI.

There are two common ways to create tasks in the Windows Task Scheduler:

1. Create a task from scratch.
2. Export a similar task, change it a little bit, and import it back in.

If this is a common practice for you, I'm here to make your day easier. Why stop your automation by using Task Scheduler to kick off your PowerShell scripts? By going this route, you're still manually creating the task. Instead, use PowerShell to create your scheduled tasks for you!

I used to spend five minutes creating a scheduled task that kicked off a PowerShell script. Now it takes me longer to figure out the server name I want to create it on! Here's how I do it.

I built a nice tool in PowerShell called New-ScheduledScript. This tool enables me to build my script anywhere I want and schedule it to run on any Windows computer on my network. If I'm just using my personal workstation to schedule scripts, I can do that now. If I want to build my scripts on my personal workstation -- and then have a server somewhere launch those scheduled tasks -- I can do that just as easily now.

This tool solves three distinct problems.

1. It enables you to build a PowerShell script anywhere on your network. Just by specifying a -Computername parameter, it will automatically transfer your script to a specified folder on a remote computer. Then it will place this folder path into the scheduled task.

This snippet is detecting the location of your script. If it's in a remote location, it will copy the script to the remote computer and automatically modify the $ScriptFilePath. It does this in order to use the script when it creates the scheduled task in a later step.

[Click on image for larger view.] 

2. It provides an easy way to specify whether to run your scheduled script in a 32- or 64-bit context, with automatic 64-bit detection. By using the $PowershellRunAsArch parameter, you can simply specify x86 or x64. This way, you can be sure the appropriate path to the powershell.exe file is used.

[Click on image for larger view.] 

3. The tool creates the scheduled task on either the local or a remote computer. Regardless of whether or not you choose to use the -Computername parameter, the script will use PowerShell v4's scheduled task cmdlets to create the task.

[Click on image for larger view.] 

Finally, there's a great example of scheduling a PowerShell script called callfromvbs.ps1. This syntax copies the callfromvbs.ps1 script to the C:\ drive on the labdc.lab.local server. It then creates a scheduled task named 'Test' on labdc.lab.local that runs daily at 3AM. It runs under the lab.local\administrator account using the password 'MyPassword'.

Here's the syntax from the callfromvbs.ps1 PowerShell script:

New-ScheduledScript.ps1 -ScriptFilePath C:\callfromvbs.ps1 -LocalScriptFolderPath  'C:\' -TaskTriggerOptions @{'Daily' = $true; 'At' = '3Am'} -TaskName 'Test'  -TaskRunAsUser 'lab.local\administrator' -TaskRunAsPassword 'MyPassword'  -Computername labdc.la
  b.local

This script really helped me to automate the creation of my scripts and saved me tons of time over just the past few months. It's yet another tool in my (and now your) IT tool belt!

About the Author

Adam Bertram is a 20-year veteran of IT. He's an automation engineer, blogger, consultant, freelance writer, Pluralsight course author and content marketing advisor to multiple technology companies. Adam also founded the popular TechSnips e-learning platform. He mainly focuses on DevOps, system management and automation technologies, as well as various cloud platforms mostly in the Microsoft space. He is a Microsoft Cloud and Datacenter Management MVP who absorbs knowledge from the IT field and explains it in an easy-to-understand fashion. Catch up on Adam's articles at adamtheautomator.com, connect on LinkedIn or follow him on Twitter at @adbertram or the TechSnips Twitter account @techsnips_io.


comments powered by Disqus
Most   Popular