The following code does not find key in hashtable:
# Wait while there are less than $maxTask running
while($taskTab.Count -ge $maxTasks){
out-log "Max tasks running" "yellow"
sleep 15
Get-Task| where {$_.name -eq "RelocateVM_Task"} | %{
write-host "ID " $_.id
$tasktab.Keys | % { "key = $_ , value = " + $tasktab.Item($_) }
if($taskTab.ContainsKey($_.id) ){
Write-host "Task found"
if ($_.State -eq "Success") {
$taskTab.Remove($_.Id)
write-host "remove task"
}
}
}
} #end while
Doing some tests:
display keys from table:
PS C:\WINDOWS\system32> $tasktab.Keys | % { "key = $_ , value = " + $tasktab.Item($_) }
key = Task-task-4059866 , value = T-JKJ-test06
Test if key exist fails
PS C:\WINDOWS\system32> if ($tasktab.containskey("Task-task-4059866") ){write-host "found"} else {write-host "not found"}
not found
Any suggestions?
Hard to tell if you don't show how you built the content of $taskTab.
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Sorry, forgot that – here it is
$disks=$vm|Get-harddisk|% {$_.id.split('/')[1]} #-> 3 disk ids should be here
$spec = New-Object VMware.Vim.VirtualMachineRelocateSpec
$spec.Host = $esx.extensiondata.moref
$spec.pool = $TCluster.extensiondata.resourcepool
$spec.datastore = New-Object VMware.Vim.ManagedObjectReference
$spec.datastore.type = "Datastore"
$spec.datastore.Value = $dsos
$spec.disk = New-Object VMware.Vim.VirtualMachineRelocateSpecDiskLocator[] (3)
$spec.disk[0] = New-Object VMware.Vim.VirtualMachineRelocateSpecDiskLocator
$spec.disk[0].diskId = $disks[0]
$spec.disk[0].datastore = New-Object VMware.Vim.ManagedObjectReference
$spec.disk[0].datastore.type = "Datastore"
$spec.disk[0].datastore.Value = $dsOS
$spec.disk[1] = New-Object VMware.Vim.VirtualMachineRelocateSpecDiskLocator
$spec.disk[1].diskId = $disks[1]
$spec.disk[1].datastore = New-Object VMware.Vim.ManagedObjectReference
$spec.disk[1].datastore.type = "Datastore"
$spec.disk[1].datastore.Value = $dsPF
$spec.disk[2] = New-Object VMware.Vim.VirtualMachineRelocateSpecDiskLocator
$spec.disk[2].diskId = $disks[2]
$spec.disk[2].datastore = New-Object VMware.Vim.ManagedObjectReference
$spec.disk[2].datastore.type = "Datastore"
$spec.disk[2].datastore.Value = $dsData
$tasktab[((Get-View -Id $vm.id).RelocateVM_Task($spec, "defaultPriority"))] =$vmname
There are a few issues with your code.
- You can't use the the [] notation on a hash table if that key does not exists yet.
The correct way would be to use the Add method.
- The Task object is an object with 2 properties, Type and Value.
When you compare this with a string, you are relying on implicit type conversion, which will not work in this case.
Better is to explicitly cast the desired type.
When you do
you will be able to do a ContainsKey with a [string] value
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
Thanks Luc, i works:-)