Running a PowerCLI Scheduled task

There are a fair few PowerCLI scripts which can be run as scheduled tasks, some that email you or maybe a monitoring script but recently I have been asked how this can be done, what do you put in the Run line for the script to work properly.

There are two ways of doing this really:

1.  Add a line to a start of each of your scripts to make sure it loads the PowerCLI snapin, without which PowerShell will not recognise any of the PowerCLI cmdlets.

Add the following line to the top of each script you will be running as a scheduled task:

add-pssnapin VMware.VimAutomation.Core

Then in the run box on your scheduled task you can call your script with the following command:

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe “& ‘C:\Scripts\MyScript.ps1′”

But remember when you are troubleshooting this script or amending it you will most likely get an error as your script editor will automatically add the VMware snapin.

2.  This method is the one I use as its easier than remembering to put the line at the top of each file and then commenting it out when you want to edit it etc, this method runs the PowerCLI Console file before it runs your script.

A PowerShell Console file is a xml like file containing information on what snapins to load when PowerShell is started.

This will enable the snapin for you and there is nothing to ad to the scripts.

The run command then looks like this:

C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile “C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1″ ” &  “C:\Scripts\MyScript.ps1″

You can test both of these methods by running them from a cmd prompt before using it to ensure they work properly.

-Alan

Alan

Alan Renouf has a role of Automation Frameworks Product Manager at VMware responsible for providing the architects and operators of the cloud infrastructure with the toolkits/frameworks and command-line interfaces they require to build a fully automated software-defined datacenter. Alan is a frequent blogger at http://blogs.vmware.com/vipowershell and has a personal blog at http://virtu-al.net. You can follow Alan on twitter as @alanrenouf.

You may also like...

69 Responses

  1. Jimbob says:

    Here’s the working version for 2012*

    C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -psconsolefile “C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1″ -noexit -command c:\myscript.ps1

    If you’re on a 32-bit system, try this:

    C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -psconsolefile “C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1″ -noexit -command c:\myscript.ps1

  2. Brandon says:

    For some reason this command works when I run it from a command prompt, but doesn’t seem to start the script (I added a line to create a file at the start of my script, and it doesn’t even get to that) when run as a scheduled task. Any ideas why this might be?

  3. Ayanes says:

    Hey the vcheck script is great with tons of useful information. However when I run this even though I have “set-executionpolicy unrestricted” I am still prompted for each script with the security warning whether I want to run once or not waiting for me to type r to run it, i can use bypass and skip the prompts. But I am trying to schedule this through the task scheduler and it is getting stuck I assume on waiting for the input. I have changed my task to have the following C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe and the following arguments -executionpolicy -bypass -PSConsoleFile “C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1″ “& ‘E:\Scripts\VMWare\vCheck\vCheck.ps1′”. This results in the script saying it ran succesffuly in less than 60 seconds but nothing happens, no email no nothing. Any suggestions how to skip those security warning prompts.

  4. Rhian Cohen says:

    Ok I actually got this working in Task Scheduler on Windows Server 2008 R2 by doing the following

    Program/script:

    C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe

    Arguments

    -PSConsoleFile “C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1″ “& ‘C:\vCheck\vCheck.ps1′”

  5. Rhian Cohen says:

    So out of these possibilities, which are correct for putting in a Windows 2008 R2 Task Scheduler?

    C:\WINDOWS\System32\WindowsPowerShell\v1.0\powershell.exe -PSConsoleFile “C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1″ -noexit “& ‘C:\vCheck\vCheck.ps1′” my-vcenter

    C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile “C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1″ “& ‘C:\Scripts\VMDailyReport.ps1′ my-vcenter″

    C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile “C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1″ “& ‘C:\Scripts\VMDailyReport.ps1′” my-vcenter

    or

    Program/script: C:\Windows\system32\WindowsPowerShell\v1.0\powershell.exe

    Arguments: -PSConsoleFile “C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1″ -command “& c:\vCheck\vcheck5.ps1 vcenterserver”

  1. July 19, 2009

    [...] Running a PowerCLI scheduled task by Virtu-Al [...]

  2. September 22, 2009

    [...] Using the great post from Virtu-al, we apply a little magic to Windows Task Scheduler using a command line similar [...]

  3. January 5, 2010

    [...] You can schedule this script on a VM that runs on another cluster or maybe better, schedule the script on a physical box. If you want to know how to schedule a Powershell/CLI script, go check out this post from Alan Renouf: http://www.virtu-al.net/2009/07/10/running-a-powercli-scheduled-task/ [...]

  4. January 24, 2010

    [...] The script can either be ran as needed or scheduled to run using Windows Scheduled Tasks, for information on how to do this check out Alan Renouf’s blog post http://www.virtu-al.net/2009/07/10/running-a-powercli-scheduled-task/ [...]

  5. October 1, 2010
  6. October 4, 2010

    [...] took the wonderful vcheck script and made it run every night. Great email overview of the virtual infrastructure each [...]

  7. February 19, 2011

    [...] Report does what it says on the tin, it runs as a scheduled task before you get into the office to present you with key information about your infrastructure in a [...]

  8. February 19, 2011

    [...] forget to set it up as a scheduled task to run at 00:01 every night and update your VI with the next [...]

  9. February 8, 2012

    [...] is a vCenter checking script, the script is designed to run as a scheduled task before you get into the office to present you with key information via an email directly to your [...]

  10. March 9, 2012

    [...] You can automate this task by running the script as a scheduled task. [...]

  11. August 3, 2012
  12. August 22, 2012
  13. December 5, 2013

Leave a Reply

%d bloggers like this: