PowerCLI

 View Only
  • 1.  Set-Annotation on VDPortgroup fails

    Posted Apr 05, 2024 12:02 PM

    set-Annotation -Entity (Get-VDPortgroup -name "k8s") -CustomAttribute "CIDR" -Value "24"

    Results in:
    Cannot convert the "k8s" value of type "VMware.VimAutomation.Vds.Impl.V1.VmwareVDPortgroupImpl" to type "VMware.VimAutomation.ViCore.Types.V1.Inventory.Invent
    oryItem"

    $p=Get-VDPortgroup -name "k8s"
    Set-Annotation -Entity [VMware.VimAutomation.Vds.Types.V1.VDPortgroup]$p -CustomAttribute "CIDR" -Value "24"

    Results in:
    Set-Annotation Value cannot be found for the mandatory parameter Entity

     



  • 2.  RE: Set-Annotation on VDPortgroup fails

    Posted Apr 05, 2024 12:05 PM

    What does this return?

    Get-VDPortgroup -Name 'k8s'
    (Get-VDPortgroup -Name 'k8s').GetType()
    


  • 3.  RE: Set-Annotation on VDPortgroup fails

    Posted Apr 05, 2024 12:22 PM

    Get-VDPortgroup -Name 'k8s':

    Name NumPorts PortBinding
    ---- -------- -----------
    k8s 16 Static

     

    (Get-VDPortgroup -Name 'k8s').GetType()

    IsPublic IsSerial Name BaseType
    -------- -------- ---- --------
    False False VmwareVDPortgroupImpl VMware.VimAutomation.Vds.Impl.V1.VDPortgroupImpl



  • 4.  RE: Set-Annotation on VDPortgroup fails
    Best Answer

    Posted Apr 05, 2024 01:04 PM

    Seems Custom Attributes are not implemented for all inventory items.
    Also the vdPortgroup object is not derived from an inventory item in the same way as for example a VM is.

    In any case, you can use the API to fix this

     

     

    $caName = 'CIDR'
    $caValue = '24'
    $vdpgName = 'k8s'
    
    $ca = Get-CustomAttribute -Name $caName
    $pg = Get-VDPortgroup -Name $vdpgName
    
    $si = Get-View ServiceInstance
    $fldMgr = Get-View -Id $si.Content.CustomFieldsManager
    $fldMgr.SetField($pg.Id, $ca.Key, $caValue)

     

     



  • 5.  RE: Set-Annotation on VDPortgroup fails

    Posted Apr 05, 2024 01:09 PM

    Fantastic, thank you!



  • 6.  RE: Set-Annotation on VDPortgroup fails

    Posted 17 days ago

    I have the same issue but then for retrieving a custom attribute that's already set on a portgroup.

    $test = Get-VDPortGroup -VDSwitch $dvsManagementObject -Name "test-portgroup"
    Get-Annotation -Entity $test

    Get-Annotation: Cannot bind parameter 'Entity'. Cannot convert the "test-portgroup" value of type "VMware.VimAutomation.Vds.Impl.V1.VmwareVDPortgroupImpl" to type "VMware.VimAutomation.ViCore.Types.V1.Inventory.InventoryItem".

    I've tried converting the workaround but can't get it working for retrieving attributes.

    Any change you can push me in the right direction?

    Thanks!

    P




  • 7.  RE: Set-Annotation on VDPortgroup fails

    Posted 16 days ago

    Got it working, for anyone else looking:

            $customFieldMgr = Get-View ($global:DefaultVIServer.ExtensionData.Content.CustomFieldsManager)
            # Retrieve custom attributes from vcenter inventory and create lookup table
            $customKeyLookup = @{}
            $customNameLookup = @{}
            $customFieldMgr.Field | % {
                $customKeyLookup.Add($_.Key, $_.Name)
                $customNameLookup.Add($_.Name, $_.Key)
            }
            $networkView = Get-View -ViewType Network -Property Name,Value -Filter @{ "name" = $($vmotionPortgroupObject.name) }
            foreach($thisIndex in 0..$($networkView.Value.Key.Length-1)) {
                Write-Host "Portgroup $($vmotionPortgroupObject.name) has custom attribute $($customKeyLookup[$networkView.Value[$thisIndex].Key]) with value: $($networkView.Value[$thisIndex].Value)"
            }