Visit VMARENA.COM For More Advanced Technical Posts , Learn More .

Tuesday, March 7, 2017

SCHEDULE A TASK IN POWERCLI

SCHEDULE A TASK in POWERCLI



PowerCLI scripts which can be run as scheduled tasks.
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
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:\Script.ps1′”
While  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:\Script.ps1”
Test both method by running them from a cmd prompt before using it to ensure they work properly

No comments:

Post a Comment