Extremely slow when run PowerShell script by scheduled taks

I like automition/programming as much as I like Windows, my first offical training in University was VB, then I learned PHP and HTML after graduated. I can’t believe my first PHP program was welcomed in internel. But I stopped develop the skill.

I got lot of special request for virtual environment. Some people want to monitor snapshot, some want to check space status, and some request to create backup for VM…etc. Regular operation cannot satisify the requests, most of the requests are time consuming operation. Automation can save my ass. 🙂

You may know PowerCLI – VMware automation product for PowerShell. If you put PowerCLI scripts as scheduled task, you will be able to do any workflow you want by schedule.

But there is a problem stuggled me for a long time. The scheduled PowerCLI script is instable, sometimes script ran very well, sometimes script ran extremely slow, sometimes script hung, or sometimes script unexpected stopped. In troubelshooting I noticed it need a long waiting time (maybe  5 – 10 mintues) till execute first line of script when I ran it on Scheduled Task Window. Initially I thought it’s a bug of PowerShell 2.0. I upgraded to 3.0 but no lucky. I also suspected loading PowerCLI Snap-in may slow down the script exectue time, or bugs on PowerCLI components, tried to google but nothing was found.

Finally my colleague Amnon gave my some idea, he asked me change the scheduled taks priority. I figured out the trick after did some testing:

  1. Create a task as usual.
  2. Export the taks to a xml file.
  3. Open the xml file by editor.
  4. Search keyword priority.
  5. Change the default value 7 to 3. ( More detail about priority of Scheduled Task )
  6. Save the xml file and delete created task.
  7. Import the xml as a task.

It signaficantly improved the executive time! Same script was 2 hours, now only 10 minutes!

Here is my another post ($array.count does not show anything in PowerCLI) regarding a bug of PowerShell, it may helpful for your PowerCLI script.