How to – run a PowerCLI .ps1 script using the Task Scheduler

If you want to schedule a PowerCLI/Powershell script (.ps1) using the Windows Task Scheduler, you have to take care of the following requirements:

1. Load the PowerCLI modules

Add the following line at the beginning of your .ps1 script to load the PowerCLI modules:

Get-Module -Name VMware* -ListAvailable | Import-Module

2. Use the following settings when creating the scheduled task

When creating the scheduled task, take care of the following settings in the Action Tab:

PowerCLI Powershell Task Scheduler

Action: „Start a program“
Setting Program/Script: „powershell.exe“
Add arguments (optional): -ExecutionPolicy Bypass C:\Scripts\Test.ps1 (replace Test.ps1 with the name of your script)

You can configure all other settings in the scheduled task as you are used to it. The settings above are the only ones differing from those normally used.

2 Comments

  1. Marcus Woods

    Load only the modules you need or else your task will take up way too much compute and memory. Yes this works, but it is the lazy way out!

    Marcus

  2. Marcus Woods

    Most scripts can run with just

    Get-Module -Name VMware.VimAutomation.Core | Import-Module. Remember to look at the definitions of the modules. Use only the modules that you need. When you end up with a bunch of running tasks in your datacenter you want them to be lean, green, fighting machines.

    Great post otherwise.

Leave a Comment

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