VMware Cloud Community
breakstuf50
Enthusiast
Enthusiast
Jump to solution

automate updating of customfield

Hi Experts,

I need help with automating the updating of annotation custom attributes by importing the name of the VM on the first column and applying the new value on the product field on the 2nd column from a csv file.

(csv file)

Name | Product

VM1   | test1

VM2   | test2

The annotation custom attribute should be:

(Summary Annotations section)

VM1 > Product: test1

VM2 > Product: test2

what i have in mind is import from csv the name and values then run a loop of this

Get-VM VM2 | Set-CustomField -Name Product -Value test1

Many thanks.

Cheers

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

You mean something like this ?

foreach($row in (Import-Csv -Path input,csv -UseCulture)){

    Get-VM -Name $row.Name | Set-CustomField -Name Product -Value $row.Product

}


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

View solution in original post

Reply
0 Kudos
3 Replies
LucD
Leadership
Leadership
Jump to solution

You mean something like this ?

foreach($row in (Import-Csv -Path input,csv -UseCulture)){

    Get-VM -Name $row.Name | Set-CustomField -Name Product -Value $row.Product

}


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

Reply
0 Kudos
breakstuf50
Enthusiast
Enthusiast
Jump to solution

Thanks Luc that did the trick. If i want to update multiple customfield what do i need to do? do i pipe another set-customfield?

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

If you want to have all the custom fields in 1 CSV file, you could do something like this

# CSV layout

#

# VM,CField1,CField2,CField3

# vm1,field11,field21,field31

# vm2,field12,field22,field32

#

$t.psobject.Properties | where{$_.Name -ne 'VM'} | %{

    $_

}

foreach($row in (Import-Csv -Path input,csv -UseCulture)){

    $vm = Get-VM -Name $row.VM

    $row.psobject.Properties | where{$_.Name -ne 'VM'} | %{

        Set-CustomField -Entity $vm -Name $_.Name -Value $_.Value

    }

}


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

Reply
0 Kudos