Automation

 View Only
  • 1.  Hash table containskey does not work

    Posted Aug 24, 2020 07:15 AM

    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?



  • 2.  RE: Hash table containskey does not work

    Posted Aug 24, 2020 07:25 AM

    Hard to tell if you don't show how you built the content of $taskTab.



  • 3.  RE: Hash table containskey does not work

    Posted Aug 24, 2020 09:01 AM

    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



  • 4.  RE: Hash table containskey does not work

    Posted Aug 24, 2020 09:22 AM

    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

    $tasktab.Add((Get-View -Id $vm.id).RelocateVM_Task($spec, "defaultPriority").ToString(),$vmname)

    you will be able to do a ContainsKey with a [string] value

    $tasktab.ContainsKey('Task-task-12345')


  • 5.  RE: Hash table containskey does not work

    Posted Aug 24, 2020 09:33 AM

    Thanks Luc, i works:-)