Virtu-Al.Net

Virtually everything is poshable

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.

, ,

55 Responses to “Running a PowerCLI Scheduled task”

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

  • orims says:

    how do i pass the username and password in a secured manner ??

  • slimboyfat says:

    Maybe I’m missing something, but scheduling doesn’t seem to work?

    I’ve created a batchfile as follows:

    C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -command “& ‘C:\Scripts\VMDailyReport.ps1′ VI-ServerName”

    If I run it interactively, it’s fine and I get the report – but if I try to schedule it, no report arrives….

    Any suggestions??

  • Virtu-Al says:

    @slimboyfat
    Funny enough I had someone with the same issue today, if you put the command in the run window of the scheduled task it should be fine.

  • slimboyfat says:

    @Virtu-Al

    Unfortuately it’s still not working :-(

    I’m obviously missing something in the syntax….

  • Virtu-Al says:

    @slimboyfat
    Ok, Try this:

    C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile “C:\Program Files\VMware\Infrastructure\VIToolkitForWindows\vim.psc1″ “& ‘C:\Scripts\VMDailyReport.ps1′ TESTVC01″

  • slimboyfat says:

    @Virtu-Al

    That seems to have sorted it – although I already had the VMWare stuff loaded (it “complains” if I run it interactively) but I’m not goiing to worry!

    The script is excellent – thanks for all your help!!

    Jack

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

  • Chrisfoo says:

    Hi There, I am new to powershell and having a few issues with getting this DailyReport to work the ways that have been mentioned in sch tasks and command prompts,
    Our Vcenter uses a different Port
    1. I have to first do this: Proxy stops Connection: Set-VIToolkitConfiguration -ProxyPolicy NoProxy

    I can then connect with this: Connect-viserver -server vcenter server-Port 1443 -Credential (Get-Credential servername\administrator) (THIS CONNECTION WORKS)

    But if i try and run the suggestions at the thread above i get this
    [VI Toolkit] C:\Program Files\VMware\Infrastructure\VIToolkitForWindows> C:\WIND
    OWS\systems32\windowspowershell\v1.0\powershell.exe -PSConsoleFile “C:\ProgramFi
    les\VMware\Infrastructure\VIToolkitForWindows\vim.psc1″ “& ‘C:\Scripts\DailyRepo
    rt.ps1 vcenterserver -Port 1443″
    The term ‘C:\WINDOWS\systems32\windowspowershell\v1.0\powershell.exe’ is not re
    cognized as a cmdlet, function, operable program, or script file. Verify the te
    rm and try again.
    At line:1 char:59

    I can VI connect using Ecoshell gui to vcenter also, but how do you run the DailyReport from there?

    Any help would be much appreciated as i would like to be able to manage vcenter with Powershell

  • Virtu-Al says:

    @Chrisfoo
    Hi, the above cmd line is to be used in a scheduled task window, if you are trying to run this from the powershell prompt then you can just CD into the folder and do:

    .\DailyReport.ps1 YOURVISERVER

    As you have custom connection ports and credentials you will need to edit the script manualy and find the line which does a connect-viserver and adjust it to work as specified in your comment.

    Hope this helps.

  • Chrisfoo says:

    Thanks Al, much appreciated.

    Cheers Chris

  • Steve Chung says:

    Hi
    just to let you know that the following script doesn’t run, could you please help my give more clear instruction as how to make it work.

    I tried to run this script from vSphere PowerCLI on my desktop. and I simply replace MyScript.ps1 with my own script called “DailyReport.ps1″. I got error, could you please help.

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

    thanks

    hope to hear from you soon.

    Best Regards,

    Steve Chung

  • [...] 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/ [...]

  • [...] 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/ [...]

  • Krobe says:

    You can just add the line to your script and avoid the error:

    Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue

    I personally find this way easier.

  • Daren says:

    I found on Windows 2008 R2 simply entering the following in the two fields fixed it for me.

    Program/script:
    C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe

    Add arguments (optional):
    C:\Scripts\MyScript.ps1

    Note: removing the & (ambersand) with also removing the speak marks from the script posted above

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

  • Eugene says:

    Hi and Thanks again for doing all this work. I finally got it running the way I want it.

    Here is a correction to post #5, you had the vcenter server inside the quotes and it wasn’t working. It should work with this syntax:

    C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile “C:\Program Files\VMware\Infrastructure\VIToolkitForWindows\vim.psc1″ “& ‘C:\Scripts\VMDailyReport.ps1′” TESTVC01

  • Mike says:

    After many attempts, realized that -noexit need to be after -PSConsoleFile

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

  • Mike says:

    I could not get it to run while logged out until I put it in a .cmd. So now I have a SchedTask calling vCheck.cmd, which contains:
    C:\WINDOWS\system32\WindowsPowerShell\v1.0\powershell.exe -PSConsoleFile “C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1″ “& ‘E:\vCheck\vCheck5.ps1′” my-vcenter

  • Rusty says:

    Hi, Thanks fro this script bit I cannot get it to work. I am actually trying this on ESX 3.5 VC 2.5 U2 and in your example you have the command line …“C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1″

    I actually don’t have that file (vim.psc1) in that path, or any other path for that matter. I do have the vSphere RCLI components installed, powershell installed, and .NET installed. So it would appear I have all the dependencies. On a brighter note the email component does work as I am receiving error emails.

    So if someone could kindly steer me in the right direction I would appreciate it.

  • Rusty says:

    Please disregard my last post. I have successfully run the script interactively thank you now to set it up as a scheduled task.

  • Enone says:

    Hey, great script!
    But I got one problem: When I run the script vom cmd-line everything is fine, I passed the connection Info like written in “Adjusting connection information”, so no user-request here.
    When I run the command as a scheduled task, I get asked for user and password. If I’m logged into the running user, I can click “cancel” multiple times and get a report at the end. But of course the user normally isn’t logged in and the script won’t go on without interaction.

    Any solution?

    Greetings

  • Billy says:

    Hi how can I run vCheck against a specific list of VMs only?

    Cheers

    Billy

  • Burak says:


    Daren:

    I found on Windows 2008 R2 simply entering the following in the two fields fixed it for me.
    Program/script:
    C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe
    Add arguments (optional):
    C:\Scripts\MyScript.ps1
    Note: removing the & (ambersand) with also removing the speak marks from the script posted above
    C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe “& ‘C:\Scripts\MyScript.ps1′”

    This has also worked for me. I added “add-pssnapin VMware.VimAutomation.Core” on the first line of my script.

  • Jon H says:

    Hello I was trying to get this script to run as a Task in Windows and have been having some issues. Here is what I am using:

    C:\WINDOWS\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile “C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1″ -noexit “& ‘C:\VMScripts\vCheck5.ps1′” DITSFAVC01

  • Nicholas says:

    This does not work on Server 2008 R2. I put the entire command in a PS script and then use the scheduled task to call the script to run. It runs, however never terminates or completes.

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

  • I also have the same problem as Nicolas.
    here are my windows 2008 schedule task settings:
    Program/script:
    powershell.exe
    Add arguments (optional):
    -noninteractive -noprofile -command “(ampersand) (open braket)C:\Scripts\vcheck\vcheck.ps1 vcenter1(closing braket)”

    Added the Vmware snapin at the begining of the script.

    The script runs but never ends enven after 24 hours.
    Interesting enough if I run the same command in a windows prompt It will complete without any issues

  • Joe Wozniak says:

    My problem was that my user credentials were not being passed in correctly. I ended up modifying the script to pass the un/pw to the viconnect command:

    $VIServer = Connect-VIServer $VISRV –user “MyUserName” –Pass “thepassword” –Protocol Https

    This is also mentioned on this page –> http://www.virtu-al.net/featured-scripts/vcheck/

  • deercutter says:

    If I issue each command seperately, the script runs. But If I run the script together, it never gets by the first line:

    powershell.exe
    Add-PSSnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue
    C:\PSscript\vCheck521.ps1 vi_server

    I had no problem runniing this script from a Windows XP workstation. Now I have a Windows 7 (64-bit) workstation.

    Any advice would be appreciated.

    Note* This was my previous script on a Windows XP workstation:

    %SystemRoot%\system32\windowspowershell\v1.0\powershell.exe -PSConsoleFile “C:\Program Files\VMware\Infrastructure\vSphere PowerCLI\vim.psc1″ -command “”

    Here is the error message it returns:

    Failed to load Windows PowerShell console file “C:\Program Files\VMware\Infrastr
    ucture\vSphere PowerCLI\vim.psc1″. WIN 32 error: The system cannot find the path
    specified.

  • [...] 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 [...]

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

  • Kevin Lengel says:

    Hey guys,

    For the Windows 7 (64-bit) users, I finally found the syntax in order to run your scripts. Here it is:

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

    Add arguments textbox:
    -psc “C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1″ -command “C:\YourScript.ps1″

    deercutter, I hope you find this useful! I know I am relieved! I will be posting this on my blog (wasupbe.com)

  • Oliver says:

    I got the script to work with Win2008 R2 task scheduler by setting up the task as follows:

    Program/script: powershell.exe
    (i suggest to extend the path variable with %SystemRoot%\system32\WindowsPowerShell\v1.0\)

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

    Regards

  • Chad King says:

    Hey Alan,
    Been busy day for me here at the office but I have been trying to figure out how to get this done. Here is a summary of what I am trying to accomplish.

    1.I have 9 vCenters and they all require different credentials. Where or how can I store these credentials and call them to run the script as a scheduled task. < forgive my ignorance ;)

    2. I dont need SMTP or anything like that I just want it to create HTML reports. I tried finding the path that it dumps the file in but have failed. I also use Powergui not sure if that will help with anything.

    I will more than likely run the scheduled task from my desktop since it has full access to all the different virutal environments over a vpn.

    I tried following the details in the script about a credential store and specifying a user name but that doesnt seem to work…

    Thanks,
    Chad King

  • Chad King says:

    well I guess I could just run it as a batch file and pass the user id information into the script and run that as a scheduled task… not my perferred method but I think it would work – a batch file for each environment would prob work. Sorry for the double post Alan.

  • Guido says:

    I get this error all the time, i run the script from powercli:

    The term ‘param’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if
    try again.
    At C:\scripts\test\vCheck5.ps1:7 char:6
    + param <<<< ( [string] $VISRV)
    + CategoryInfo : ObjectNotFound: (param:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

  • Gerard Delgado says:

    I have created a document to schedule this script as a tasks and also to store your credentials in an encrypted XML file. Please see the link below I hope this helps anyone encountering the same issues I had.

    http://cl.ly/3L3b3T1a1p3n1p382h3q

  • Guido says:

    Help,
    Has anybody any idea about the error above.

  • Guido says:

    I have tried the document you’ve made. made the xml file edited the vcheck5 file.
    But i still get the error that is above.
    The term ‘param’ is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and
    try again.
    At C:\scripts\test\vCheck5.ps1:3 char:6
    + param <<<< ( [string] $VISRV)
    + CategoryInfo : ObjectNotFound: (param:String) [], CommandNotFoundException

  • Gerard Delgado says:

    Guido,

    Add the below to the very top of your script.Also please ensure your running the script with the same account you used to create the XML.

    add-pssnapin VMware.VimAutomation.Core -ErrorAction SilentlyContinue

  • Guido says:

    it keeps coming with the error, about this line:

    param( [string] $VISRV)

  • Gerard Delgado says:

    Comment out param line which is at the very top of the script. If you do that and run the script what error do you get?

  • Nick says:

    I am experiencing an interesting problem … I run the script, it connects, gathers the information, gets to “Displaying the HTML Results” … and … nothing.

    I get no browser display … nothing in the event logs, I cannot see any processes that have started after the script (iexplore.exe for example).

    Anyone seen this before?

  • Guido says:

    i’ve comment out that line, started the scheduled task, a command box is opened and i see
    8:09:03 AM Connecting to VI Server
    PS C:\WINDOWS\system32\WindowsPowerShell\v1.0>

    and nothing happens

  • Gerard Delgado says:

    Try to run the task from a PowerCLI prompt and see what you get. Make sure the account you specified when creating the XML encrypted file has rights to log onto VCenter.

  • Guido says:

    this is all is says.
    [vSphere PowerCLI] C:\> cd scripts
    [vSphere PowerCLI] C:\scripts> cd test
    [vSphere PowerCLI] C:\scripts\test> .\vcheck5.ps1
    2:41:06 PM Connecting to VI Server
    [vSphere PowerCLI] C:\scripts\test>

  • Eric says:

    Note on a 64bit server the path to vim.psc1 is different it should be “C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1″

  • Matt says:

    I read all the comments and could not get this to execute on my Win7 x64 PC. I downloaded System Scheduler free, put the commands into a .bat file and it executed with no issue.

  • Dean says:

    Hello,
    Awesome vCheck script! Saves me so much time it is amazing. I do have one issue that maybe you have seen. When the html comes out to IE it looks perfect but when it sends to my email (outlook 2010) the secondary titles and descriptions and any background colors are lost. Is there anything I can do to clean up the look of the email output to match the IE output?

    Thanks again for an awesome script.

    Dean

  • Nigel Snow says:

    Trouble running as scheduled task resolved.

    I have been playing with this exceptionally helpful script for a few days as I was having trouble triggering it via a scheduled task in Windows 2008R2 and noticed others also had problems.

    I needed to change the line in the script that set the appdata environment variable from “C:\Documents and Settings\” + $USER + “\Application Data” to “C:\Users\” + $USER + “\AppData\Roaming”, I also hardcoded the cred file path to $CredFile = “C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\mycred.crd” once created and tested.

    The correct syntax is throughout the posts above.
    Cheers

  • vSoluationArchitect says:

    vCheck worked right away.

    The surprise was it find ‘exceptions’ and reports on them. I used it recently as we built a lab prior to a mgiration to later version of vSphere. It was helpful to monitor both production and lab with script (aimed at the different vCenters).

    It was easy to adjust limits (such as number of VMs per LUN up from 5 (default), etc).

    Nice job.

    Larry T.

  • Steve says:

    Hi,
    I was wondering how I would run a VIEW PowerCLI script as a scheduled task?
    I do not have the VMware PowerCLI installed on my connection server, just the View PowerCLI, so ‘VMware.VimAutomation.Core’ is not available. Any ideas?

    Cheers,
    Steve

  • Alan says:

    The view snapin is called VMware.View.Broker so i would add something like this to the start of your script:

    if (!(get-pssnapin -name VMware.View.Broker -erroraction silentlycontinue)) {
    add-pssnapin VMware.View.Broker
    }

Leave a Reply

Your email address will not be published. Required fields are marked *

*

You may use these HTML tags and attributes: <a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <strike> <strong>