Create Scheduled Tasks with PowerCLI to Call PowerShell Scripts.

Create Scheduled Tasks with PowerCLI to Call PowerShell Scripts.

Create Scheduled Tasks with PowerCLI to Call PowerShell Scripts.

Recently i was involved in a very tricky situation with one of the customer, There was a set of performance metrics which had to be extracted from about 200 RDSH VM’s which are part of a large Horizon View Farm.

The problem was that a lot of manual effort was involved as the admin guys in support team had to log into each and every VM and create scheduled tasks manually to run for 30 days .

The scheduled task was basically used to invoke a PowerShell Script and run in it in “non-interactive” mode every 5 minutes for 9 hours on these set of server’s every day at 9AM and dump the data generated in a csv file on an external file share.

Well if this was the case then why dont we automate it!! and then came PowerCLI to the rescue 🙂 .

Also note each of these 100 VM’s was distributed across two vcenters!!, hence i had to give a script to the helpdesk guys so that in event of a scary “RECOMPOSE” was executed on these set of servers which would make the VM’s vanilla again and delete any changes made, the help desk guys could launch a powershell console and run the script again and make sure all’s back on track.

Ok so let me go step by step on how i made it to work.

Create Scheduled Tasks with PowerCLI to Call PowerShell Scripts.

Create the Credential object for GuestOS and vCenter Server, as this would be a script just for temporary purpose and to make it easy for help desk guys!! i hard-coded the credentials 🙂 .

Next we would connect to the vcenter one by one, get a list of VM’s which match the VM prefix, then iterate across them one by one and copy the required powershell scripts to the VM’s using Copy-VMGuestfile which internally uses VMware Tools

Next we would use the cool cmdlet Invoke-VMScript to call the powershell scripts across all the 200 VM’s which internally would create the scheduled tasks! 😉 and we could disconnect from the vCenter.

Next we would again connect to the second vcenter and go through this process again to copy the scripts and create scheduled tasks on these additional set of VM’s

The end result would be something like this, where we see a number of scheduled tasks were created on the servers.

Below are the details on the RegisterScheduledtask.ps1 which created these scheduled tasks across the servers for 38 days.

Happy Scripting!!, i hope you found the post useful and get ideas on how PowerShell and PowerCLI can come to your rescue in touch situations! 🙂

Please follow and like us:

Share this post

Post Comment