VMware Cloud Community
devrawat2
Contributor
Contributor

Unable to run a scheduled task in PowerCLI

Hi Folks,

Need some help here. I was playing around with PowerCli and I am facing some issue.

I have a really small test script which adds a 20GB hard disk to a VM.This works absolutely fine when run it through PowerCli.

==============

# Add-Disk.Ps1

Connect-VIserver -server <my_vcenter>

New-HardDisk -vm testmachine1 -CapacityGB 20

==============


However , I wish to schedule it on a particular time. I wrote another script which schedules the above mentioned script (Add-Disk.ps1) successfully for a future date and time. I can see the task in the task scheduler. I can see that it executes fine.It doesnt return any error message.  But , it doesnt add the disk to the VM. Any ideas what am I doing wrong here


================

# Schedule_task.ps1


$addition_date = Read-Host -Prompt "`nEnter the date when you wish to add disk to this VM in DD/MM/YYYY format"

$addition_time = Read-Host -Prompt "`nEnter the time when you wish to add disk to this VM in HH:MM format"

$trigger = New-JobTrigger -Once -At "$addition_date $addition_time"

Register-Scheduledjob -Name disk_addition -Trigger $trigger -FilePath "D:\VMware Scripts\Add-disk.ps1"

==========================


Thanks

-Dev

Tags (2)
0 Kudos
4 Replies
LucD
Leadership
Leadership

You have to do the Add-PSSnapin -Name VMware* to get the PowerCLI cmdlets avaiable.

See Alan's Running a PowerCLI Scheduled task post for details.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
devrawat2
Contributor
Contributor

Thanks LucD‌ for the reply. However, it is still not working. Again, here are the two scripts that I use after modification.

Add-Disk.ps1 -> I wish to schedule this script. ( This script runs fine manually when I run it on powershell.exe)

====================================

Add-PSSnapin -Name VMware*

Connect-VIserver -server <MyVcenter> -Force

New-HardDisk -vm testmachine1 -CapacityGB 20

exit

========================================

Schedule-task.ps1 -> I use this script to input the date and time when I wish to schedule the above mentioned script.

*****************************************************************

$vm = Read-Host -Prompt "`nEnter the name of the VM to which you wish to add the disk."

$size = Read-Host -Prompt "`nEnter the size of the disk that you wish to add (in GB)"

$addition_date = Read-Host -Prompt "`nEnter the date when you wish to add disk to this VM in DD/MM/YYYY format"

$addition_time = Read-Host -Prompt "`nEnter the time when you wish to add disk to this VM in HH:MM format"

$trigger = New-JobTrigger -Once -At "$addition_date $addition_time"


Register-Scheduledjob -Trigger $trigger -Name "Disk_addition" -FilePath <Location to Add-Disk.ps1 Script>


******************************************************************

This does create a job in my schedule. The job runs successfully. But doesnt add the disk to the machine. Any thoughts ? May be I need to tweak my arguments.

0 Kudos
LucD
Leadership
Leadership

You should also disable all warnings, otherwise the script might hang.

Do a Set-PowerCLIConfiguration -DisplayDeprecationWarnings:$false


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos
LucD
Leadership
Leadership

Did you test the script under the same account as with which you run the scheduled task ?

Did you check if the $trigger is a valid trigger object ?


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos