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.
Start PowerCLI as the user who will run the script.
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
Make sure the parameter $cred got some content.
- $cred.Password | ConvertFrom-Securestring
Export the user credentials to an encrypted file.
- C:\> $cred.Password | ConvertFrom-Securestring | Set-Content C:\vSphere\scripts\powerclicred
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).
Add the Trigger based on your preferred schedule.
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
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.
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.
The Scheduled Task looks like this in the Windows Task Scheduler
Add the user to the “Log on as a batch job” “User Rights Assignment” section in the Windows machine “Local Security Policy” editor.
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.
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 ↓
vSphere cluster resource pool configuration script | vcdx56
March 27, 2013 at 8:22 pm (UTC 0) Link to this comment
[…] I created a blog post about how to schedule a PowerCLI script in Windows task scheduler and it can be found here […]
PowerCLI sccript for exporting VM details to HTML | Ping4info
March 30, 2013 at 10:55 pm (UTC 0) Link to this comment
[…] 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. […]
VM per datastore PowerCLI e-mail report | vcdx56
April 8, 2013 at 9:11 pm (UTC 0) Link to this comment
[…] 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. […]
Configure and report the ESXi host TSM-SSH service using PowerCLI | vcdx56
June 13, 2013 at 3:49 pm (UTC 0) Link to this comment
[…] 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 […]
Update vSphere DRS rules using PowerCLI | vcdx56
July 9, 2013 at 6:01 pm (UTC 0) Link to this comment
[…] on a daily basis and i used my previous blog post Schedule PowerCLI script in Windows task scheduler to achieve […]
Using PowerCLI to synchronize vCenter Notes with Active Directory description fields | DiscoPosse – Using the chicken to measure IT
July 10, 2013 at 3:18 am (UTC 0) Link to this comment
[…] 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/ […]
PoSh On Time | /var/log
July 11, 2013 at 5:20 pm (UTC 0) Link to this comment
[…] 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, […]
Backup the virtual machine configuration file (.vmx) using PowerCLI | VCDX56
December 10, 2013 at 7:25 am (UTC 0) Link to this comment
[…] 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 […]
VCDX56 summary of 2013 | VCDX56
December 31, 2013 at 8:56 am (UTC 0) Link to this comment
[…] My most read blog post during 2013, apart form the VCDX56 home page, is “Schedule PowerCLI script in Windows task scheduler” […]
vSphere VM vCPU to ESXi host CPU core allocation | VCDX56
June 1, 2014 at 4:51 pm (UTC 0) Link to this comment
[…] blog post “Schedule PowerCLI script in Windows task scheduler” can be used if you want to schedule the PowerCLI script in Windows Task […]
Snapshots and Automated Emails | rnelson0
July 9, 2014 at 5:04 pm (UTC 0) Link to this comment
[…] 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 […]
How To Fix Directory Name Is Invalid Task Scheduler Errors - Windows Vista, Windows 7 & 8
November 10, 2014 at 12:32 pm (UTC 0) Link to this comment
[…] 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 …… […]