VMware Cloud Community
PowaCLI
Contributor
Contributor
Jump to solution

Problems accesing and configuring the scheduled tasks

Hello all!

I'm new in the community. Im an apprentice in the last year. I work with vmware esx / vsphere since august 2009.

In december 2009 i startet scripting with powershell and powerCLI.

Maybe someone can help me, i searched long time, but I don't know how to do.

I've got a small script, which leads you through some menus. There you can select the datacenter, the template and insert the vm name. The datastores and the host or cluster will get automatically for the deploy. At the end, the script creates an INI File like this:

vmname=test

Location=VENV40

Template=w2k3eesp2x32_template

Cluster_Host=adcv43e.xxx.yyyy.net

DatastoreSystem=esx40_nfs01_SystemDrives1

DatastoreSwap=esx40_nfs02_SwapDrives1

VCenterVENV=xx.yy.xx.yyy

Now to my problem. The second script should be an endless loop, which looks if there are some new INI Files.

Because of our policies, we only can deploy during night (storage load). We've got 3 "slots". at 01:00, at 03:00 and at 05:00, where we can deploy a vm.

The second script should get the scheduled deploy task from the Vcenter and check if there is a "slot" free to deploy, if not, check next night and so on.

My problems are:

I can't get only the deploy task

The start time got one hour difference than in the vcenter ? (daylight saving time ?)

When I got the available task, how do I create an new one, with the Informations out of the Ini file?

Has anyone an idea ?

This is what i've got until now. This was just to try if i can get the scheduled task

  1. --------------------------------------------------------------------------------------------

  2. Force load (otherwise VMware.Vim objects are not known)

http://Reflection.Assembly::LoadWithPartialName("vmware.vim")

cls #clearscreen

$svcRef = new-object VMware.Vim.ManagedObjectReference

$svcRef.Type = "ServiceInstance"

$svcRef.Value = "ServiceInstance"

$serviceInstance = get-view $svcRef

  1. This returns a MoRef to the scheduled task manager

$schMgr_ref = $serviceInstance.Content.scheduledTaskManager

  1. This return the actual scheduled task manager object

$schMgr_obj=get-view $schMgr_ref

  1. The method needs to be called on the object

  2. The method requires 1 argument.

  3. As the API Ref states when the parameter is Null the method will return

  4. all scheduled tasks

$foo=$schMgr_obj.RetrieveEntityScheduledTask($null)

  1. The method returns an array of ScheduledTask object references

foreach ($task in $foo) {

$infos = (get-view $task).info

$taskname = $infos.Name

$taskdate =($infos.Scheduler).RunAt

$task = "$taskname","$taskdate"

}

#----


Thanking you in anticipation.

I'm looking forward to hear from you, and sorry for my bad english Smiley Wink

Greetings PowaCLI

Reply
0 Kudos
47 Replies
PowaCLI
Contributor
Contributor
Jump to solution

When i don't use the MoRef I get this:

Exception setting "Datastore": "Cannot convert value "esx100_nfsswap01_ESXSwapD
rives01" to type "VMware.Vim.ManagedObjectReference". Error: "Ungültige Umwandl
ung von System.String in VMware.Vim.ManagedObjectReference.""

And if you look at the reference above, there the Type by the datastore is "ManagedObjectReference:Datastore"

Greets PowaCLI

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That property definitely needs a MoRef.

I'm looking what else could have caused the error.

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos
PowaCLI
Contributor
Contributor
Jump to solution

Type of operation being performed on the backing of the specified virtual device. If no file operation is specified in the VirtualDeviceSpec, then any backing filenames in the VirtualDevice must refer to files that already exist. The "replace" and "delete" values for this property are only applicable to virtual disk backing files.

Maybe it has something to do with that, i'll check it.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

That's it, you have to add

$devspec.FileOperation = "create"

since it's a new .vmdk file you're creating.

Good find, you see every word in the API Reference is important.

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos
PowaCLI
Contributor
Contributor
Jump to solution

Yees Smiley Happy It's really important to read it ALL !!

I also had to add an unitnr. because I specified a controller!!:

"Unit number of this device on its controller. This property is null if the controller property is null (for example, when the device is not attached to a specific controller object).

Normally, two devices on the same controller may not be assigned the same unit number.

"

$devspec.device.UnitNumber = "1"

Soon, I'll post all of the script Smiley Happy I'm really happy now. Half year ago, I had absolutely no idea about powershell, specially in combination with vmware Smiley Happy But now I get more and more a "feeling" for coding ^^

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

We're waiting anxiously for the script.

Glad you're sold to PowerShell and PowerCLI Smiley Wink

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos
PowaCLI
Contributor
Contributor
Jump to solution

Soo, how I promised to you, here are my scripts.

The scripts will probably not work without some corrections.

The scheduler script you can start for example on the vcenter server and let it endless run.If you have set the right paths you can use the CSV Creator script.

The Script will leads you through following steps:

-- Script gets each Datacenter, which contains a Template. Then you can select the datacenter.

-- After selecting the datacenter, the menu will show you all templates from this datacenter. Also here, you can select a template.

-- Next Step, is a question, if you like to select the Datastore manually, or let the Script select them automatically ( automatically will probably not work. It is scripted to select the datastores with name "System" and "Swap" with the highest upcounting number after nfs, for example esx100_NFS11_Systemdrive1.)

-- Last step is to enter your new VM name. This will be checked, if it contains only a-Z and 0-9. It will also be checked, if it doesn't already exists.

-- The Pool (Cluster or Resourcepool) will be automatically selected for the selected Datacenter.

-- at the end you can select bewteen, "create a new vm" and "exit".

The CSV Creator Script will create a CSV File and save it at the defined folder, where the scheduler will take it, schedule it, and move it to the \scheduled folder.

If you got questions, ask Smiley Wink

I hope, that someone can use this script, or parts of it Smiley Wink

Edit: If the selected Template has only 1 disk, the Script will add a second disk for the pagefile. If the template already contains 2 disk. they will be mounted on the System and Swapdrive datastores.

Greetings

PowaCLI

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Great scripts, well done.

Good luck with your defense.

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos