Schedule PowerCLI script in Windows task scheduler

I have received a couple of questions about how to add a PowerCLI scrip to Windows task scheduler after publishing my blog post vSphere Cluster Resource Pool Configuration Script
Depending on the customer requirements there are several ways of doing this. Customer requirements for my implementation includes:

  • An active directory based service account must be used to run the script.
  • The script must not run on the machine where the vCenter Service runs, it will run on a virtual machine used for monitoring purposes.
  • No passwords can be stored in plain text in script files or any other files.

First thing to do is to create an active directory based account.
Screen Shot 2013-03-22 at 19.50.34
Start PowerCLI as the user who will run the script.
Screen Shot 2013-03-22 at 19.56.57
Screen Shot 2013-03-22 at 19.58.10
The PowerCLI prompt is now started with the user who will run the scheduled PowerCLI script. Now we need to get the user credentials.

  • $cred=Get-Credential

Screen Shot 2013-03-22 at 19.59.58
Screen Shot 2013-03-22 at 20.00.30
Make sure the parameter $cred got some content.

  • $cred.Password | ConvertFrom-Securestring

Screen Shot 2013-03-22 at 20.06.38
Export the user credentials to an encrypted file.

  • C:\> $cred.Password | ConvertFrom-Securestring | Set-Content C:\vSphere\scripts\powerclicred

Screen Shot 2013-03-22 at 20.12.01
Now when we got the password stored in a file we need to add the two below lines to our PowerCLI script. Change the red text in your PowerCLI script to match your environment.

  • $pwd = Get-Content c:\vSphere\scripts\powerclicred | ConvertTo-SecureString
  • $cred = New-Object System.Management.Automation.PsCredential “home\domain_user“, $pwd
  • Connect-VIServer vcenter.home.com

Lets start creating the Windows scheduled task in the Task Scheduler. Two things:

  • Make sure to change the user to the service account user (see first green arrow).
  • Mark the check box “Run weather user is logged on or not” (see second green arrow).

Screen Shot 2013-03-22 at 20.21.44
Add the Trigger based on your preferred schedule.
Screen Shot 2013-03-22 at 20.23.40
Add an action.

  • Action = Start a program
  • Program/Script = Path to powercli.exe. In my case C:WINDOWSsystem32windowspowershellv1.0powershell.exe
  • Add arguments (optional): -PSConsoleFile  “path to the file vim.psc1” -command “&{Path to powercli script}”
    In my case -PSConsoleFile  “C:\Program Files (x86)\VMware\Infrastructure\vSphere PowerCLI\vim.psc1”  -command “&{C:\vSphere\scripts\rp.ps1}”

Make sure you get the correct signs when copy and paste the above lines

Screen Shot 2013-03-22 at 20.29.11
Configure the settings in the “Conditions” and “Settings” tabs to your preferred values. I used the default once.
Click ok and type the password for the service account.
Screen Shot 2013-03-22 at 20.31.49
Click ok to the Task Scheduler pop up telling you that the user need to have the “Log on as batch job” rights on the Windows machine where we schedule the PowerCLI script.
Screen Shot 2013-03-22 at 20.32.39
The Scheduled Task looks like this in the Windows Task Scheduler
Screen Shot 2013-03-22 at 20.37.49
Add the user to the “Log on as a batch job” “User Rights Assignment” section in the Windows machine “Local Security Policy” editor.
Screen Shot 2013-03-22 at 20.34.05
Last thing is to make sure the service account has the required priviliges in vCenter Server. For this particular case i selected the Adminsitrator role.
Screen Shot 2013-03-22 at 20.44.07
The PowerCLI script will now run according to your specified schedule.

Thanks to @nkange for useful input when configure my first PowerCLI script to run as a Windows scheduled task a few years back.

17 pings

Skip to comment form

  1. vSphere cluster resource pool configuration script | vcdx56

    […] I created a blog post about how to schedule a PowerCLI script in Windows task scheduler and it can be found here […]

  2. PowerCLI sccript for exporting VM details to HTML | Ping4info

    […] Its a simple script for exporting VM details to HTML format. The HTML report created will be saved into the folder with the name of current month. It will help in generating a daily report with the help of Windows Task Scheduler. Refer this article for scheduling PowerCLI script. […]

  3. VM per datastore PowerCLI e-mail report | vcdx56

    […] I have put together a very simple script which can be be scheduled the same way as described in one of my previous blog post which can be found here. […]

  4. Configure and report the ESXi host TSM-SSH service using PowerCLI | vcdx56

    […] script on a daily basis the but i don’t know if they used the information in my blog post Schedule PowerCLI script in Windows task scheduler or […]

  5. Update vSphere DRS rules using PowerCLI | vcdx56

    […] on a daily basis and i used my previous blog post Schedule PowerCLI script in Windows task scheduler to achieve […]

  6. Using PowerCLI to synchronize vCenter Notes with Active Directory description fields | DiscoPosse – Using the chicken to measure IT

    […] While I could write about how to do this, this is a great way to promote some more awesome content, so head on over to view Magnus Andersson’s post on doing just what we want to do: http://magander.se/2013/03/27/schedule-powercli-script-in-windows-task-scheduler/ […]

  7. PoSh On Time | /var/log

    […] who looks more like a potentially marauding Viking than a mild mannered systems geek has written a good post on scheduling a PowerCLI (vSphere’s PoSh module) script in Windows task scheduler. Check it out, […]

  8. Backup the virtual machine configuration file (.vmx) using PowerCLI | VCDX56

    […] can schedule the PowerCLI script to run on a daily basis using the instruction in this blog. When register the backed up .vmx file to the vCenter Server and edit the virtual machine […]

  9. VCDX56 summary of 2013 | VCDX56

    […] My most read blog post during 2013, apart form the VCDX56 home page, is “Schedule PowerCLI script in Windows task scheduler” […]

  10. vSphere VM vCPU to ESXi host CPU core allocation | VCDX56

    […] blog post “Schedule PowerCLI script in Windows task scheduler” can be used if you want to schedule the PowerCLI script in Windows Task […]

  11. Snapshots and Automated Emails | rnelson0

    […] a task is fairly easy and, thanks to Magnus Andersson, there’s a graphical walkthrough to the process. His walkthrough is pretty good but I ran into a few problems up front. I’ll […]

  12. How To Fix Directory Name Is Invalid Task Scheduler Errors - Windows Vista, Windows 7 & 8

    […] Schedule PowerCLI script in Windows task scheduler | VCDX56 – Schedule PowerCLI script in Windows task scheduler. I have received a couple of questions about how to add a PowerCLI scrip to Windows task scheduler after …… […]

Comments have been disabled.