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
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

There are two problems.

1) The $cloneSpec.Location.Pool property needs to pint to a resourcepool. In the script below I took the default (and hidden) resourcepool called Resources.

2) The $spec.Scheduler property needs to have one of AfterStartupTaskScheduler, OnceTaskScheduler, RecurrentTaskScheduler.

You will probably need to set the $spec.Scheduler.runAt property to a value if you want to avoid that the task starts immediatly.

$taskName = "Create new VM"
$taskHours = 1,3,5
$iniPath = "D:\__IPA_PowerCLI_VM\INI\"

Connect-VIServer 10.11.4.209

$si = Get-View ServiceInstance
$schedMgr = Get-View $si.Content.scheduledTaskManager

$scheduled = @()
$schedMgr.ScheduledTask | %{Get-View -Id $_} | %{
	if($_.Info.Name -like ($taskName + "*")){
		$scheduled += $_.Info.NextRunTime.ToLocalTime()
	}
}

$now = Get-Date -Minute 0 -Second 0
if($now.Hour -gt $taskHours[-1]){
	$now = $now.AddDays(1)
}

$iniFiles = Get-Item ($iniPath + "*") -Include "*.csv"
foreach($iniFile in $iniFiles){
	$params = Import-Csv $iniFile  # -UseCulture
	$notScheduled = $true
	while($notScheduled){
		$taskHours | %{
			$schedTime = $now.Date.AddHours($_)
			if(!($scheduled -contains $schedTime)){
				$spec = New-Object VMware.Vim.ScheduledTaskSpec
				
				$spec.Action = New-Object VMware.Vim.MethodAction
				
				$arg1 = New-Object VMware.Vim.MethodActionArgument
				$arg1.Value = New-Object VMware.Vim.VirtualMachineRelocateSpec
				$arg1.value = (Get-Folder vm| Get-View).MoRef  # (Get-Folder $params.Location| Get-View).MoRef
				$spec.Action.argument += $arg1
				
				$arg2 = New-Object VMware.Vim.MethodActionArgument
				$arg2.value = "TESTVM"
				$spec.Action.argument += $arg2

				$cloneSpec = New-Object VMware.Vim.VirtualMachineCloneSpec
				$cloneSpec.Location = New-Object VMware.Vim.VirtualMachineRelocateSpec
				$cloneSpec.Location.Datastore = (Get-Datastore qesx00_nfs01_SystemDrives1| Get-View).MoRef
				$cloneSpec.Location.Pool = (Get-cluster QVENV00_Cluster00 | Get-ResourcePool Resources  | Get-View).MoRef

				$arg3 = New-Object VMware.Vim.MethodActionArgument
				$arg3.value = $clonespec
				$spec.Action.argument += $arg3
				$spec.Action.Name = "CloneVM_Task"
				$spec.Description = "Test"
				$spec.Scheduler = New-Object VMware.Vim.OnceTaskScheduler
				
				$spec.Enabled = $true
				$spec.Name = $taskName + " " + $now
				
				$entity = Get-Template IPAtestmaschine_Luca | Get-View
				
				$schedMgr.CreateScheduledTask($entity.MoRef,$spec)

				$notScheduled = $false
			}
		}
		$now = $now.AddDays(1)
	}
}

____________

Blog: LucD notes

Twitter: lucd22


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

View solution in original post

Reply
0 Kudos
47 Replies
LucD
Leadership
Leadership
Jump to solution

vCenter uses internally UTC time. In the vSphere client you see local time.

If you in the CET timezone (UTC +1) then this could explain the 1 hour difference you see.

For the 2nd part of your question, I'm not sure if you want to create a scheduled task, with the creation of the new VM, or if you want to check if there is a specific scheduled task present.

____________

Blog: LucD notes

Twitter: lucd22


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

PowaCLI
Contributor
Contributor
Jump to solution

Many thanks for your fast answer. This with the timezone could be the reason.

For the 2nd part, I want to automatically create a scheduled task, with the creation of the new vm (infos from ini file). But the script should automatically manage the tasks, so that not 2 or 3 vm's deploy at the same time, because our storage is not so powerful.

Because of that, the script should check, is there a vm deploying at 01:00? If yes, is there a vm deploying at 03:00, if yes, is there a vm deploying at 05:00? If yes, check this times at the next night, if not, schedule the task at this time.

I hope you know what i mean?

Greetings PowaCLI

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The question was a bit harder than I originally thought.

But I think I might have a working script.

The only adaption I made was that I use CSV files instead of INI files.

The reason being that it i smuch easier to read a CSV file in PowerShell.

I hope that is not a major problem?

The scheduled tasks will all have a name starting with "Create new VM".

The script looks if there are already tasks scheduled on the predefined hours and stores the times in the $scheduled array..

Then it takes all the CSV files one by one and tries to find a free slot.

Also note that the script converts the internal time to LocalTime when going through the already scheduled tasks.

This takes care of the hour difference you noticed.

$taskName = "Create new VM"
$taskHours = 1,3,5
$iniPath = "D:\"

$si = Get-View ServiceInstance
$schedMgr = Get-View $si.Content.scheduledTaskManager

$scheduled = @()
$schedMgr.ScheduledTask | %{Get-View -Id $_} | %{
	if($_.Info.Name -like ($taskName + "*")){
		$scheduled += $_.Info.NextRunTime.ToLocalTime()
	}
}

$now = Get-Date -Minute 0 -Second 0
if($now.Hour -gt $taskHours[-1]){
	$now = $now.AddDays(1)
}

$iniFiles = Get-Item ($iniPath + "*") -Include "*.csv"
foreach($iniFile in $iniFiles){
	$params = Import-Csv $iniFile -UseCulture
	$notScheduled = $true
	while($notScheduled){
		$taskHours | %{
			$schedTime = $now.Date.AddHours($_)
			if(!($scheduled -contains $schedTime)){
				$spec = New-Object VMware.Vim.ScheduledTaskSpec
				
				$spec.Action = New-Object VMware.Vim.MethodAction
				
				$arg1 = New-Object VMware.Vim.MethodActionArgument
				$arg1.value = (Get-Folder $params.Location| Get-View).MoRef
				$spec.Action.argument += $arg1
				
				$arg2 = New-Object VMware.Vim.MethodActionArgument
				$arg2.value = $params.vmname
				$spec.Action.argument += $arg2

				$cloneSpec = New-Object VMware.Vim.VirtualMachineCloneSpec
				$cloneSpec.Location = New-Object VMware.Vim.VirtualMachineRelocateSpec
				$cloneSpec.Location.Datastore = (Get-Datastore $params.DatastoreSystem| Get-View).MoRef
				$cloneSpec.Location = (Get-VMHost $params.Cluster_Host | Get-ResourcePool "Resources" | Get-View).MoRef 

				$arg3 = New-Object VMware.Vim.MethodActionArgument
				$arg3.Action.value = $clonespec
				$spec.Action.argument += $arg3
				$spec.Action.Name = "CloneVM_Task"
				
				$spec.Enabled = $true
				$spec.Name = $taskName + " " + $now
				
				$entity = Get-Template $params.Template | Get-View
				
				$schedMgr.CreateScheduledTask($entity.MoRef,$spec)

				$notScheduled = $false
			}
		}
		$now = $now.AddDays(1)
	}
}

I hope this helps.

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos
PowaCLI
Contributor
Contributor
Jump to solution

Thx a lot for the script.

Sadly I had to do other stuff..

I'll try it this or next week and give you a feedback.

In a short overview i saw a point, which i maybe should make an if condition?

$cloneSpec.Location = (Get-VMHost $params.Cluster_Host | Get-ResourcePool "Resources" | Get-View).MoRef

We got 4 different Vcenter. In some we use and select only the Cluster for deploying. In other Vcenter, we have to select the host and resourcepool.

Can i also use a Cluster for the location? Making an if to check if it's a Host or a Cluster?

Greetings PowaCLI

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Since the script uses the VirtualMachineRelocateSpec object to define the destination, the rules are defined by the destination you want.

The SDK Reference says: "if resource pool is specified, and the target pool represents a DRS-enabled cluster, a host selected by DRS is used".

That would mean that if you select a resourcepool from the cluster, DRS will automatically select a clusternode.

In that case you would have to change the resourcepool line and leave host property set to $null.

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos
PowaCLI
Contributor
Contributor
Jump to solution

Hello LucD

I changed the line for the resource pool and tested the script with static values (the foreach loop to read the files shouldn't make problems).

$taskName = "Create new VM"
$taskHours = 1,3,5
$iniPath = "D:\__IPA_PowerCLI_VM\INI\"



Connect-VIServer 10.11.4.209



$si = Get-View ServiceInstance
$schedMgr = Get-View $si.Content.scheduledTaskManager

$scheduled = @()
$schedMgr.ScheduledTask | %{Get-View -Id $_} | %{
	if($_.Info.Name -like ($taskName + "*")){
		$scheduled += $_.Info.NextRunTime.ToLocalTime()
	}
}

$now = Get-Date -Minute 0 -Second 0
if($now.Hour -gt $taskHours[-1]){
	$now = $now.AddDays(1)
}

$iniFiles = Get-Item ($iniPath + "*") -Include "*.csv"
foreach($iniFile in $iniFiles){
	$params = Import-Csv $iniFile  # -UseCulture
	$notScheduled = $true
	while($notScheduled){
		$taskHours | %{
			$schedTime = $now.Date.AddHours($_)
			if(!($scheduled -contains $schedTime)){
				$spec = New-Object VMware.Vim.ScheduledTaskSpec
				
				$spec.Action = New-Object VMware.Vim.MethodAction
				
				$arg1 = New-Object VMware.Vim.MethodActionArgument
				$arg1.Value = New-Object VMware.Vim.VirtualMachineRelocateSpec
				$arg1.value = (Get-Folder vm| Get-View).MoRef  # (Get-Folder $params.Location| Get-View).MoRef
				$spec.Action.argument += $arg1
				
				$arg2 = New-Object VMware.Vim.MethodActionArgument
				$arg2.value = "TESTVM"
				$spec.Action.argument += $arg2

				$cloneSpec = New-Object VMware.Vim.VirtualMachineCloneSpec
				$cloneSpec.Location = New-Object VMware.Vim.VirtualMachineRelocateSpec
				$cloneSpec.Location.Datastore = (Get-Datastore qesx00_nfs01_SystemDrives1| Get-View).MoRef
				$cloneSpec.Location.Pool = (Get-cluster QVENV00_Cluster00  | Get-View).MoRef    # Get-VMHost $params.Cluster_Host |

				$arg3 = New-Object VMware.Vim.MethodActionArgument
				$arg3.value = $clonespec
				$spec.Action.argument += $arg3
				$spec.Action.Name = "CloneVM_Task"
				$spec.Description = "Test"
				
				$spec.Enabled = $true
				$spec.Name = $taskName + " " + $now
				
				$entity = Get-Template IPAtestmaschine_Luca | Get-View
				
				$schedMgr.CreateScheduledTask($entity.MoRef,$spec)

				$notScheduled = $false
			}
		}
		$now = $now.AddDays(1)
	}
}

When I run the Script, i get this Error:

Exception calling "CreateScheduledTask" with "2" argument(s): "Not initialized:

vim.scheduler.TaskScheduler scheduler"

At C:\DOCUME1\MXEFER1\LOCALS~1\Temp\c347f81d-260a-49c2-82b1-7dc20e4d47a3.ps1:

72 char:34

+ $schedMgr.CreateScheduledTask <<<< ($entity.MoRef,$spec)

+ CategoryInfo : NotSpecified: (Smiley Happy [], MethodInvocationException

+ FullyQualifiedErrorId : DotNetMethodException

Do you know what that means? Which specification have to be set? At the moment, the script defines the:

- Folder to deploy in

- datastore

- pool

- vm name

- task name

- task description

greetings from PowaCLI

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

There are two problems.

1) The $cloneSpec.Location.Pool property needs to pint to a resourcepool. In the script below I took the default (and hidden) resourcepool called Resources.

2) The $spec.Scheduler property needs to have one of AfterStartupTaskScheduler, OnceTaskScheduler, RecurrentTaskScheduler.

You will probably need to set the $spec.Scheduler.runAt property to a value if you want to avoid that the task starts immediatly.

$taskName = "Create new VM"
$taskHours = 1,3,5
$iniPath = "D:\__IPA_PowerCLI_VM\INI\"

Connect-VIServer 10.11.4.209

$si = Get-View ServiceInstance
$schedMgr = Get-View $si.Content.scheduledTaskManager

$scheduled = @()
$schedMgr.ScheduledTask | %{Get-View -Id $_} | %{
	if($_.Info.Name -like ($taskName + "*")){
		$scheduled += $_.Info.NextRunTime.ToLocalTime()
	}
}

$now = Get-Date -Minute 0 -Second 0
if($now.Hour -gt $taskHours[-1]){
	$now = $now.AddDays(1)
}

$iniFiles = Get-Item ($iniPath + "*") -Include "*.csv"
foreach($iniFile in $iniFiles){
	$params = Import-Csv $iniFile  # -UseCulture
	$notScheduled = $true
	while($notScheduled){
		$taskHours | %{
			$schedTime = $now.Date.AddHours($_)
			if(!($scheduled -contains $schedTime)){
				$spec = New-Object VMware.Vim.ScheduledTaskSpec
				
				$spec.Action = New-Object VMware.Vim.MethodAction
				
				$arg1 = New-Object VMware.Vim.MethodActionArgument
				$arg1.Value = New-Object VMware.Vim.VirtualMachineRelocateSpec
				$arg1.value = (Get-Folder vm| Get-View).MoRef  # (Get-Folder $params.Location| Get-View).MoRef
				$spec.Action.argument += $arg1
				
				$arg2 = New-Object VMware.Vim.MethodActionArgument
				$arg2.value = "TESTVM"
				$spec.Action.argument += $arg2

				$cloneSpec = New-Object VMware.Vim.VirtualMachineCloneSpec
				$cloneSpec.Location = New-Object VMware.Vim.VirtualMachineRelocateSpec
				$cloneSpec.Location.Datastore = (Get-Datastore qesx00_nfs01_SystemDrives1| Get-View).MoRef
				$cloneSpec.Location.Pool = (Get-cluster QVENV00_Cluster00 | Get-ResourcePool Resources  | Get-View).MoRef

				$arg3 = New-Object VMware.Vim.MethodActionArgument
				$arg3.value = $clonespec
				$spec.Action.argument += $arg3
				$spec.Action.Name = "CloneVM_Task"
				$spec.Description = "Test"
				$spec.Scheduler = New-Object VMware.Vim.OnceTaskScheduler
				
				$spec.Enabled = $true
				$spec.Name = $taskName + " " + $now
				
				$entity = Get-Template IPAtestmaschine_Luca | Get-View
				
				$schedMgr.CreateScheduledTask($entity.MoRef,$spec)

				$notScheduled = $false
			}
		}
		$now = $now.AddDays(1)
	}
}

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos
PowaCLI
Contributor
Contributor
Jump to solution

Awesome ! Many thanks, now it works.

I only have to write the $spec.scheduler.runat then it will work fine Smiley Happy

When i finished my script, i'll post it here.

Reply
0 Kudos
PowaCLI
Contributor
Contributor
Jump to solution

Hello LucD

Thanks a lot again for your help. Im near at the end of my script.

It works pretty good. It only had an little error in the logic. It scheduled Foreach File, all Taskhours. Now i added a Counter and only after that, it will add a day.

My last bigger problem is: When i'm connected to more than 1 Vcenter, it gives an Error, although i specified the Vcenter like this:

$si = get-view ServiceInstance -server $params.VCenterVENV

My CSV File looks like this, and everything works for scheduling:

vmname,Location,Template,Cluster_Host,DatastoreSystem,DatastoreSwap,VcenterVENV

testvm6,QVENV00,w2k8eer2x64_template,QVENV00_Cluster00,qesx00_nfs01_SystemDrives1,qesx00_nfs02_SwapDrives1,10.11.4.209

Here also my script:

#VARIABLEN DEKLARATION
# ---------------------------------------------------------------------------------
$user = “myAdminuser”  #user 
$credentialFile=”C:\securestring.txt”
$taskName = "Create new VM"
$taskHours = 1,3,5
$csvPath = "D:\__IPA_PowerCLI_VM\INI\"  #temporaer
$scheduledinis = "D:\__IPA_PowerCLI_VM\INI\scheduled" #temporaer
$endlessloop = $true
$counter = 0


#----------------------CONNECTION MIT VCENTER-----------------------------------------
#Passwort einlesen und in File Securestring 
Write-Host "User: $user Bitte geben Sie Ihr Passwort ein:" 
Write-Host ""  # leere Zeile
Read-Host -AsSecureString | ConvertFrom-SecureString| Out-File C:\securestring.txt

cls #clearscreen
#passwort wieder einlesen und als credential hinterlegen

$pass = cat $credentialFile| ConvertTo-SecureString
$cred= New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $user,$pass


#zuerst mit allen VCentern connecten
Connect-VIServer 10.11.4.209 -Credential $cred -WarningAction SilentlyContinue  # qualityumgebung
#Connect-VIServer 10.11.4.200 -Credential $cred -WarningAction SilentlyContinue
#Connect-VIServer 10.11.4.159 -Credential $cred -WarningAction SilentlyContinue
#Connect-VIServer 10.11.5.100 -Credential $cred -WarningAction SilentlyContinue
#---------------------------------------------------------------------------------------



while($endlessloop -eq $true){
$csvFiles = $null   # alle bereits eingelesenen Files wieder reseten

#
#
#$si = Get-View ServiceInstance
#$schedMgr = Get-View $si.Content.scheduledTaskManager



#$scheduled = @()
#$schedMgr.ScheduledTask | %{Get-View -Id $_} | %{
#	if($_.Info.Name -like ($taskName + "*")){
#		$scheduled += $_.Info.NextRunTime.ToLocalTime()   #Zeiten der Tasks auslesen
#	}
#}



$now = Get-Date -Minute 0 -Second 0
if($now.Hour -gt $taskHours[-1]){
	$now = $now.AddDays(1)
}


do{
	sleep 3 #timer, damit das File fertig geschrieben werden kann
	$csvFiles = Get-Item ($csvPath + "*") -Include "*.csv"  #csvs aus pfad lesen 
}while($csvFiles -eq $null)   #  scane so lange, bis ein file gefunden wird




foreach($csvFile in $csvFiles){
	$params = Import-Csv $csvFile # -UseCulture  # inhalt des csv files einlesen
	$notScheduled = $true  # neues file noch nicht scheduled, 
	$counter = 0  # counter reset

#-----------------------------------
$si = Get-View ServiceInstance  -Server $params.VcenterVENV       <<< HERE I SPECIFIE WHICH VCENTER
$schedMgr = Get-View $si.Content.scheduledTaskManager

#tasks neu einlesen
$scheduled = @()  #array zuerst wieder leeren
$schedMgr.ScheduledTask |%{Get-View -Id $_} | %{
	if($_.Info.Name -like ($taskName + "*")){
		$scheduled += $_.Info.NextRunTime.ToLocalTime()
	}
}
#----------------------------------
		
		
		
	
	while($notScheduled){	
			$taskHours | %{ $schedTime = $now.Date.AddHours($_)			
							
							if(!($scheduled -contains $schedTime) -and $notScheduled -eq $true ){
						
							$spec = New-Object VMware.Vim.ScheduledTaskSpec
							$spec.Action = New-Object VMware.Vim.MethodAction
							
							$arg1 = New-Object VMware.Vim.MethodActionArgument
							$arg1.Value = New-Object VMware.Vim.VirtualMachineRelocateSpec
							$arg1.value = (Get-Folder -Location $params.location vm| Get-View).MoRef  # (Get-Folder $params.Location| Get-View).MoRef
							$spec.Action.argument += $arg1
							
							$arg2 = New-Object VMware.Vim.MethodActionArgument
							$arg2.value = $params.vmname # "testvm"
							$spec.Action.argument += $arg2
			
							$cloneSpec = New-Object VMware.Vim.VirtualMachineCloneSpec
							$cloneSpec.Location = New-Object VMware.Vim.VirtualMachineRelocateSpec
							$cloneSpec.Location.Datastore = (Get-Datastore $params.DatastoreSystem | Get-View).MoRef  # bsp. qesx00_nfs01_SystemDrives1
							$cloneSpec.Location.Pool = (Get-ResourcePool Resources  | Get-View).MoRef  # Get-cluster QVENV00_Cluster00 |    bsp. QVENV00_Cluster00
							
							
							# -------- DISK dem Datastore MAPPEN
							$diskSpec1 = new-object VMware.Vim.VirtualMachineRelocateSpecDiskLocator
							$diskSpec1.diskID = 1
							$diskSpec1.Datastore = (Get-Datastore $params.DatastoreSystem | Get-View).MoRef
							$cloneSpec.Location.Disk = $diskSpec1 # DISK 1  SYSTEM DISK in CloneSpec schreiben
							
							$diskSpec2 = new-object VMware.Vim.VirtualMachineRelocateSpecDiskLocator
							$diskSpec2.DiskID = 2
							$diskSpec2.Datastore = (Get-Datastore $params.DatastoreSwap | Get-View).MoRef
							$cloneSpec.Location.Disk += $diskSpec2 # DISK 2 SWAP DISK in CloneSpec schreiben
							# -------------------------------------
							
			
							$arg3 = New-Object VMware.Vim.MethodActionArgument
							$arg3.value = $clonespec
							$spec.Action.argument += $arg3
							$spec.Action.Name = "CloneVM_Task"
							$spec.Description = "Automated VM Deployment"
							$spec.Scheduler = New-Object VMware.Vim.OnceTaskScheduler
							$spec.Scheduler.Runat = $schedTime
							
							
							$spec.Enabled = $true
							$spec.Name = $taskName + " " + $params.vmname + " " + $schedTime
							
							$entity = Get-Template $params.template | Get-View
							
							$schedMgr.CreateScheduledTask($entity.MoRef,$spec) 
			
							$notScheduled = $false
							Copy-Item $csvFile $scheduledinis 
							Remove-Item $csvFile

				
							
							
						}# if
						else{
						$counter = $counter + 1
								if($counter -eq 3){
									$now = $now.AddDays(1)
									$counter = 0
								}
						}
											 
					}#foreach taskhour
						
					
		
				
	} # while notscheduled
}#foreach csv file


} #endless

This is the error i got:

-


Get-View : Cannot validate argument on parameter 'Id'. The argument cannot be n

ull or empty.

At C:\DOCUME1\MXEFER1\LOCALS~1\Temp\93ab042b-6cc9-4685-a59e-2da752b4abb1.ps1:

79 char:40

+ $schedMgr.ScheduledTask |%{Get-View -Id <<<< $_} | %{

+ CategoryInfo : InvalidData: (Smiley Happy , ParameterBindingVal

idationException

+ FullyQualifiedErrorId : ParameterArgumentValidationError,VMware.VimAutom

ation.Commands.DotNetInterop.GetVIView

Method invocation failed because [http://System.Object[http://System.Object[]] doesn't contain a method nam

ed 'CreateScheduledTask'.

At C:\DOCUME1\MXEFER1\LOCALS~1\Temp\93ab042b-6cc9-4685-a59e-2da752b4abb1.ps1:

139 char:37

+ $schedMgr.CreateScheduledTask <<<< ($entity.MoRef

,$spec)

+ CategoryInfo : InvalidOperation: (CreateScheduledTask:String) [

], RuntimeException

+ FullyQualifiedErrorId : MethodNotFound

-


I have no idea why its $null or empty ... and the Strange thing is, when i type in that in the console, it works xD

Maybe you have an idea?

Greets, PowaCLI

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Could it be that there are no scheduled tasks at that moment ?

In that case $schedMgr.ScheduledTask will be $null and the ForEach-Object loop will execute once but the $_ variable will contain $null.

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos
PowaCLI
Contributor
Contributor
Jump to solution

Hmm i don't think soo, but i have to check at monday.

When I'm connect only to 1 Vcenter at the beginning, it works fine with this:

$si = Get-View ServiceInstance -Server $params.VcenterVENV # <<< HERE I SPECIFIE WHICH VCENTER FROM THE CSV FILE

  1. it makes no difference if i add -server $params.VcenterVENV or not, when im only connected to 1 Vcenter, but it works with this.

$schedMgr = Get-View $si.Content.scheduledTaskManager

But when I'm connected to 2 Vcenters, it brings this error, although I try to schedule the same CSV file, which selects the same Vcenter.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Perhaps you could get the $si for each vCenter immediately after the Connect-VIServer ?

And store the values in a hash table where you could use the name of the vCenter as the key.

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos
PowaCLI
Contributor
Contributor
Jump to solution

I tried it like this:

Directly after connecting to all VCenters I get the $si for all Vcenters:

$siVCenters=@{"10.11.4.209"=Get-View serviceinstance -Server 10.11.4.209;"10.11.4.200"=Get-View serviceinstance -Server 10.11.4.200;"10.11.4.159"=Get-View serviceinstance -Server 10.11.4.159;"10.11.5.100"=get-view serviceinstance -Server 10.11.5.100;}

Then, Foreach CSV File in CSV Files i do this:

$actVcenter = $params.VCenterVENV # this will get the IP Adress from the Vcenter out of the CSV File

$si = $siVcenters.$actVcenter

$schedMgr = Get-View $si.Content.scheduledTaskManager

I've got the same error, I'm gettin crazy xD Smiley Wink because i absolutly don't know why this error occures.

Still the same thing:

Connected to 1 Vcenter, regardless which one, it works.

Connected to 2 Vcenter, it Says the -ID is $null or empty xD

Hmm Any idea else?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

The only thing I can think of is to do a

Disconnect-VIServer
Connect-VIServer $params.VCenterVENV
$si = Get-View Service-Instance

each time.

But then you loose of course the flexibility of being conencted to all the vCenters Smiley Sad

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos
PowaCLI
Contributor
Contributor
Jump to solution

I tested it and it works. But the script is a lot slower with this.

uumm I'll look for an other solution. If i find out something i'll post it here.

By the way. Is it on vSphere 4.0 the same to Deploy a new VM ?

Reply
0 Kudos
PowaCLI
Contributor
Contributor
Jump to solution

LucD I GOT IT Smiley Happy wohooo hehe

If i look to it now, its soo simple, and i spend hours -.- grrr

Even if i specifie the Vcenter from which I want the ServiceInstance Object -> for example like this:

$si = get-view ServiceInstance -server 10.11.4.209

When I then set the TaskManager

$schedMgr = get-view $si.content.scheduledtaskmanager

It will Take the Task Manager from each Vcenter, even if i specified that i only want 10.11.4.209

Now I did it like this:

$si = get-view ServiceInstance

$schedMgr = get-view $si.content.scheduledtaskmanager -server 10.11.4.209

Greetings PowaCli

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Great !

I overlooked that one as well.

Strange you don't have to use the -Server parameter when you do the

$si = Get-View ServiceInstance

____________

Blog: LucD notes

Twitter: lucd22


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

Reply
0 Kudos
PowaCLI
Contributor
Contributor
Jump to solution

Ouu Yes sorry, didn't look right.

I use on both the -server 10.11.4.209

$si = get-view serviceinstance -server 10.11.4.209

$schedMgr = get-view $si.content.scheduledTaskmanager -server 10.11.4.209

Reply
0 Kudos
PowaCLI
Contributor
Contributor
Jump to solution

Ahh next error -.-

Now it works for all Vcenters (when i only have 1 file, and let the script go). But when i run the script with all CSV Files, i have this error after the first file:

Exception calling "CreateScheduledTask" with "2" argument(s): "entity"
At C:\DOCUME~1\MXEFER~1\LOCALS~1\Temp\26545d6e-d51a-42aa-82e0-3f1abea75915.ps1:
141 char:37
+                             $schedMgr.CreateScheduledTask <<<< ($entity.MoRef
,$spec)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

Maybe I have to reset a value foreach CSV File ?

Reply
0 Kudos