The greatest challenge to any thinker is stating the problem in a way that will allow a solution

Bertrand Russell

By

On 26 Mar 2012

Tech Notes

Tags: ,


Being able to create scripts to complete repetitive tasks and complete work that would otherwise give you headache is a wonderful thing, being able to schedule those scripts increases the available fire-power in your arsenal.

Like most things in IT, it can be a bit of a head scratcher to sort out the first time, but once you’ve seen it once you can just copy and paste. See below for a few examples….

With a scheduled task you are able to run a command line at a specific time.  The trick is forming a command line that will complete the task that you want completed.  So if you’re not confident on how to get something to run from a scheduled task, play around running it from the command prompt first.  The basic syntax to run a PowerShell script is…

Powershell.exe -PSConsoleFile <script.ps1>

You may need to use the full path to Powershell.exe, depending on whether your machine knows where to find it, for example it could be at…

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe
C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe

…though if you can run PowerShell directly from the command line then you shouldn’t need to specify the full executable path, eg

Note that, unlike other scripts, you cannot run PowerShell scripts in the background without a GUI console window appearing on your desktop.  Which is annoying.  There is a <code>-WindowStyle</code> option that you can set to <code>Hidden</code>, but I’ve not managed to get it to work.

Windows XP and Windows 2003

In Windows XP or Windows 2003, its mostly about the Run field, which needs to include the path to the PowerShell executable, so assuming that your PowerShell is located at C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe, your Run command could be something like (assuming you need to specify a full executable path rather than just powershell)…

Run: C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile "C:\Scripts\MyScript.ps1"

Be aware that you might also need to set the Start In field as well, especially if your script needs to read or write to other files as the path that your script will be running from won’t necessarily be the same as when you wrote the script, normally you’ll set this to the path that your script is in, eg

Start In: C:\Scripts

Windows 7 and Windows 2008

Its organised a bit differently in Windows 7 or Windows 2008, in that your command line gets split up.  Here in the Actions tab you need to create a Start a program action, and populate the following fields…

Program/script: powershell
Add arguments (optional): -PSConsoleFile "C:\Scripts\MyScript.ps1"
Start in (optional): C:\Scripts

 

PowerCLI and Other SnapIns

If the script you are using requires a SnapIn you have two options;
Explicitly load the SnapIn within the script
Add the SnapIn to the the scheduled task

Which you use is largely down to personal preference, though if you load the SnapIn from your script then you have more options available with regards to handling problems (if the SnapIn fails to load, can’t be found, etc).

To load the SnapIn from the script…

Add-PsSnapin *VMware*

To add the SnapIn to your scheduled task, you need to know its path, then include it in the -PSConsoleFile parameter, eg

-PSConsoleFile "C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1" "& C:\Scripts\MyScript.ps1"

For more info on scheduling PowerShell tasks, and good post for additional options etc can be found here – https://dmitrysotnikov.wordpress.com/2011/02/03/how-to-schedule-a-powershell-script/


Leave a Reply

XHTML: You can use these tags if you know what they are: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>

ERROR: si-captcha.php plugin says GD image support not detected in PHP!

Contact your web host and ask them why GD image support is not enabled for PHP.

ERROR: si-captcha.php plugin says imagepng function not detected in PHP!

Contact your web host and ask them why imagepng function is not enabled for PHP.