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.







about 8 months ago
how do i pass the username and password in a secured manner ??
about 7 months 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 7 months 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 7 months ago
@Virtu-Al
Unfortuately it’s still not working
I’m obviously missing something in the syntax….
about 7 months 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 7 months 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 5 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 5 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 5 months ago
Thanks Al, much appreciated.
Cheers Chris
about 5 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