Tutorials  Tutorials

Powershell Basics
Introduction to Windows PowerShell Part 1
Introduction to Windows PowerShell Part 2
Introduction to Windows PowerShell Part 3
PowerShell Script Signing
PowerShell Scheduled Tasks
PowerShell Networking Tasks

Powershell Advanced Tutorials
PowerShell and VMware Introduction


PowerShell Scheduled Tasks

Creating recurring tasks that run your PowerShell one liners and scripts is a common admin function. We will take a look at a few different scenarioes for creating scheduled tasks.

Scheduling a PowerShell one liner
Let's say we want to check our C drive's disk space every day at 8:00 AM, and then write that data to a file. Our command would be

PS C:\> Get-PSDrive C

Name           Used (GB)     Free (GB) Provider      Root           CurrentLocation
----           ---------     --------- --------      ----            ---------------
C                  24.19        273.89 FileSystem    C:\
So Get-PSDrive C >> c:\scripts\disklog.txt would create and then append the PSDrive output to a local text file. It's always a good idea to test the syntax of a command prior to scheduling, we can do this by using the windows run box. When calling the console, there are a number of options that we may want to include
PS C:\> powershell -?

PowerShell[.exe] [-PSConsoleFile  | -Version ]
    [-NoLogo] [-NoExit] [-Sta] [-NoProfile] [-NonInteractive]
    [-InputFormat {Text | XML}] [-OutputFormat {Text | XML}]
    [-WindowStyle ] [-EncodedCommand ]
    [-File  ] [-ExecutionPolicy ]
    [-Command { - |  [-args ]
                  |  [] } ]
When testing or troubleshooting you will probably want to turn on the -NoExit option so that the console will stay up after execution. Also if your execution policy is not set to remote signed or less strict you may run into issues. The actual command you wish to run is specified with the -command switch. It's important to note that the & symbol forces powershell to evaluate your text as a command rather than a string, so that will be a good idea to use. Our runbox formated command now looks like
powershell -noexit -command &{psdrive c >> c:\scripts\disklog.txt}
Remember Windows Key + r is the runbox shortcut. After testing and verifying everything, you can open up the Windows Task Scheduler and right click the Task Scheduler (Local) choosing Create Basic Task. Schedule for the time you want and then add in the command when prompted. For production use you will most likely want to remove the -noexit option and add -nointeractive to ensure the command does not hang waiting on input. Like so
powershell -noninteractive -command &{psdrive c >> c:\scripts\disklog.txt}
If prompted about arguments, you can safely say yes, as this will add the options to the arguments text box. After finishing the wizard right click the new task in the task library to run it immediately. F5 to refresh and show the updated Last Run Result.

Scheduling a PowerShell Script
Running a larger block of code contained in a .ps1 script file works basicly the same way as running a single command. A few additional considerations should be taken into account however. By default the -File option should be the default so the -command option can be removed. So after copying our command into test.ps1, our scheduled task action line should now read
powershell -noninteractive c:\scripts\test.ps1
However many times you will want to specify additional command line params to pass to your script, if this is the case you will need to use the -command option. Often larger scripts will use additional modules or snapins, make sure you either specify them at the top of your script file with the Add-PSSnapin syntax or add the specific initialization script from the snaping/module vendor. For example if you are using the VMware PowerCLI tools take a look at the properties of the windows shortcut for PowerCLI to see what is called.