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.
| Print article | This entry was posted by Virtu-Al on July 10, 2009 at 19:53, and is filed under PowerCLI, powershell, vmware. Follow any responses to this post through RSS 2.0. You can leave a response or trackback from your own site. |







(3)
(13)
(0)
about 1 year ago
how do i pass the username and password in a secured manner ??
about 1 year ago
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??
about 1 year ago
@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.
about 1 year ago
@Virtu-Al
Unfortuately it’s still not working
I’m obviously missing something in the syntax….
about 1 year ago
@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″
about 1 year ago
@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
about 11 months ago
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
about 11 months ago
@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.
about 11 months ago
Thanks Al, much appreciated.
Cheers Chris
about 10 months ago
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
about 5 months ago
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.
about 4 months ago
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′”
about 4 months ago
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
about 3 months ago
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
about 3 months ago
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
about 3 months ago
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.
about 3 months ago
Please disregard my last post. I have successfully run the script interactively thank you now to set it up as a scheduled task.
about 3 months ago
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
about 3 months ago
Hi how can I run vCheck against a specific list of VMs only?
Cheers
Billy
about 3 weeks ago
This has also worked for me. I added “add-pssnapin VMware.VimAutomation.Core” on the first line of my script.
about 2 weeks ago
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