Automation

 View Only
Expand all | Collapse all

Clearing all custom attributes and annotations on my VMs

  • 1.  Clearing all custom attributes and annotations on my VMs

    Posted Nov 07, 2017 04:03 PM

    A previous VMWare admin had set annotations on all of our VMs.  No one has maintained these annotations since then and the info is now out of date and confusing.  So what I want to do is clear all of these out.  Can someone comeup with a powercli script that would delete all custom attributes and annotations for VMs in my vCenter?

    Regards,

    Michael



  • 2.  RE: Clearing all custom attributes and annotations on my VMs

    Posted Nov 07, 2017 04:27 PM

    Would this do the trick?

    Get-CustomAttribute | Remove-CustomAttribute -Confirm:$false

    Get-VM | Set-Annotation -Notes '' -Confirm:$false



  • 3.  RE: Clearing all custom attributes and annotations on my VMs

    Posted Aug 13, 2018 07:23 AM

    Hi,

    the command

    Get-VM | Set-Annotation -Notes '' -Confirm:$false

    isn't working on our Systems. we have vCenter 6.5.

    Best regards



  • 4.  RE: Clearing all custom attributes and annotations on my VMs

    Posted Aug 13, 2018 07:41 AM

    Try with the following (in the more recent PowerCLI versions, the Set-Annotation cmdlet doesn't have a Notes parameter)

    Get-VM | Set-VM -Notes '' -Confirm:$false



  • 5.  RE: Clearing all custom attributes and annotations on my VMs

    Posted Aug 13, 2018 07:49 AM

    Okay, this works fine but i have a other problem.

    We are using Citrix to deploy virtual machine on the esx.

    Citrix create a custom Attribute with the Name "XdConfig" and the value "XdProvisioned=true" on the vm.

    i want to know, how delete the custom Attribute on the virtual machine?



  • 6.  RE: Clearing all custom attributes and annotations on my VMs

    Posted Aug 13, 2018 07:59 AM

    Can you try with

    Get-CustomAttribute -Name XdConfig -TargetType VirtualMachine |

    Remove-CustomAttribute -Confirm:$false



  • 7.  RE: Clearing all custom attributes and annotations on my VMs

    Posted Aug 13, 2018 08:12 AM

    Hi Luc,

    thank you very much for the quick Response.

    But i wont to delete the global Attribute on the Host/vCenter.

    There is a custom Attribute on the virtual machine.

    If i use the powershell command:

    (get-vm -Name "NameOfTheVM").ExtensionData.AvailableField

    i become the value:

    Key                     : 408

    Name                    : XdConfig

    Type                    : string

    ManagedObjectType       : VirtualMachine

    FieldDefPrivileges      :

    FieldInstancePrivileges :

    DynamicType             :

    DynamicProperty         :

    How to delete this custom field on the vm?

    Thank for your Response.



  • 8.  RE: Clearing all custom attributes and annotations on my VMs

    Posted Aug 13, 2018 08:27 AM

    If you define a Custom Attribute of the type VirtualMachine, each VM will have this Custom Attribute.

    You have 2 options:

    • remove the Custom Attribute globally (see my previous reply)
    • set the Custom Attribute value to blank on a specific VM. Like this

    $ca = Get-CustomAttribute -Name Test2 -TargetType VirtualMachine

    Get-VM -Name MyVM | Get-Annotation -CustomAttribute $ca | Set-Annotation -Value '' -Confirm:$false

    You can not remove a Custom Attribute on a single VM, only change its value.



  • 9.  RE: Clearing all custom attributes and annotations on my VMs

    Posted Aug 13, 2018 09:06 AM

    Hi Luc,

    thank you very much.

    This Reply solved my Issue.



  • 10.  RE: Clearing all custom attributes and annotations on my VMs

    Posted 19 days ago

    Hi @LucD

    I know this is an old post but this is very helpful!

    I'd like to remove a value of a custom attribute on multiple VMs from a CSV file. Can you help me with this please?  TIA




  • 11.  RE: Clearing all custom attributes and annotations on my VMs

    Posted 18 days ago

    Try with.
    It assumes the CSV contains a column named VMName.

    $ca = Get-CustomAttribute -Name 'CAName' -TargetType VirtualMachine
    
    Import-Csv -Path '.\vmnames.csv' -PipelineVariable row |
    Foreach-Object -Process {
       Get-VM -Name $row.VMName |
       Get-Annotation -CustomAttribute $ca |
       Set-Annotation -Value '' -Confirm:$false
    }


    ------------------------------


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


    ------------------------------



  • 12.  RE: Clearing all custom attributes and annotations on my VMs

    Posted 16 days ago

    Hi @LucD,

    Thanks for this.

    I am getting this error:


    Set-Annotation : The input object cannot be bound to any parameters for the command either because the command does not take pipeline input or the input and its properties do not match any of the 
    parameters that take pipeline input.
    At line:6 char:1
    + Set-Annotation -Value '' -Confirm:$false
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : InvalidArgument: (NBU_POLICY_ENABLED:scsi0-2:PSObject) [Set-Annotation], ParameterBindingException
        + FullyQualifiedErrorId : InputObjectNotBound,VMware.VimAutomation.ViCore.Cmdlets.Commands.SetAnnotation




  • 13.  RE: Clearing all custom attributes and annotations on my VMs

    Posted 16 days ago

    My bad, you can't send 2 parameters to the Set-Annotation cmdlet via the pipeline.
    Try like this

    $ca = Get-CustomAttribute -Name 'CAName' -TargetType VirtualMachine
    
    Import-Csv -Path '.\vmnames.csv' -PipelineVariable row |
    Foreach-Object -Process {
       $vm = Get-VM -Name $row.VMName
       $caAno = Get-Annotation -CustomAttribute $ca -Entity $vm
       Set-Annotation -CustomAttribute $caAno -Entity $vm -Value '' -Confirm:$false
    }


    ------------------------------


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


    ------------------------------



  • 14.  RE: Clearing all custom attributes and annotations on my VMs

    Posted 15 days ago

    Thank you!

    Got it working finally :)

    $ca = Get-CustomAttribute -Name 'attributename' -TargetType VirtualMachine
    Import-csv -Path 'file.csv' -PipelineVariable row |
    ForEach-Object -Process {
    $vm = Get-VM -Name $row.VMName
    $caAno = Get-Annotation -CustomAttribute $ca -Entity $vm
    Set-Annotation -Entity $vm -CustomAttribute 'attributename' -Value '' -Confirm:$false
    }




  • 15.  RE: Clearing all custom attributes and annotations on my VMs

    Posted 15 days ago

    Always look at the required parameters of a command:

    PARAMETERS
        -CustomAttribute <CustomAttribute>
            Specifies the custom attribute whose annotation you want to change.

            Required?                    true
        -Entity <InventoryItem[]>
            Specifies the entities to which the new annotation value applies.

            Required?                    true
        -Server <VIServer[]>
            Specifies the vCenter Server systems on which you want to run the cmdlet. If no value is provided or $null value is passed to this parameter, the command runs on the defau
            lt servers. For more information about default servers, see the description of Connect-VIServer.

            Required?                    false
        -Value <String>
            Specifies a new value for the annotation.

            Required?                    true
        -Confirm [<SwitchParameter>]
            If the value is $true, indicates that the cmdlet asks for confirmation before running. If the value is $false, the cmdlet runs without asking for user confirmation.

            Required?                    false
        -WhatIf [<SwitchParameter>]
            Indicates that the cmdlet is run only to display the changes that would be made and actually no objects are modified.

            Required?                    false
        <CommonParameters>
            This cmdlet supports the common parameters: Verbose, Debug,
            ErrorAction, ErrorVariable, WarningAction, WarningVariable,
            OutBuffer, PipelineVariable, and OutVariable. For more information, see
            about_CommonParameters (https://go.microsoft.com/fwlink/?LinkID=113216).

    Those cannot be omitted.

    Possibly you first have to read them and than pipe them to be removed.

    So something like Get-Annotation -Entity (required) | Set-Annotation -Value ''
    Or maybe use the CustomAttribute cmdlt's if needed.
    Get-CustomAttribute
    Remove-CustomAttribute or Set-CustomAttribute




  • 16.  RE: Clearing all custom attributes and annotations on my VMs

    Posted 14 days ago

    How about "Remove-CustomAttribute"? : Remove-CustomAttribute - PowerCLI VMware.VimAutomation.Core Help Reference

    Quick tip : 

    If you have a commandlet Noun you want to know what you can do with it:

    Get-Command -Noun CustomAttribute

    This will spit out the following available commandlets if the vmware.vimautomation.core module is loaded:

    Get-CustomAttribute

    New-CustomAttribute

    Remove-CustomAttribute

    Set-CustomAttribute




  • 17.  RE: Clearing all custom attributes and annotations on my VMs

    Posted 14 days ago

    I'm afraid that the Remove-CustomAttribute cmdlet removes the Custom Attribute from the environment.
    It is NOT clearing a Custom Attribute for a specific Entity.



    ------------------------------


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


    ------------------------------



  • 18.  RE: Clearing all custom attributes and annotations on my VMs

    Posted 14 days ago

    Yes, I can see that now. Have to use Get and Set annotation.

    There are some parts of PowerCLI that are less intuitive and can seem cumbersome at first.