VMware Cloud Community
andrew_uk
Enthusiast
Enthusiast
Jump to solution

Export specific custom attributes for VMs in CSV

Hi 

I am trying to import a list of VMs from CSV and retrieve some custom attributes then export results into a CSV. 

 

If I run the below for 1 attribute, it kind of works. I get the results but in a horrible way:

AnnotatedEntityIdAnnotatedEntityNameValueUid
VirtualMachine-vm-my vmVMOwner /VIServer=

 

$vmlist = Import-Csv "C:\Users\myvms.csv"

ForEach ($vm in $vmlist) {
    $vmrow = Get-VM -Name $vm.name
    Get-Annotation -Entity $vmrow -CustomAttribute "VMOwner"   | Export-Csv C:\Users\myvmsexport.csv -NoTypeInformation -Append
    }

    
   

 

I would like to get multiple custom attributes and have it exported in a format like this:

VM VMOwnerOwnerEmailServerType

 

3 custom attributes are VMOwner, OwnerEmail and ServerType

 

Thanks!

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try something like this

$vmlist = Import-Csv "C:\Users\myvms.csv"
$caNames = 'VMOwner', 'OwnerEmail', 'ServerType'

Get-VM -Name $vmlist.Name |
ForEach-Object -Process {
  $obj = New-object -TypeName PSObject -Property (
    [ordered]@{
      VM = $_.Name
      VMOwner = ''
      OwnerEmail = ''
      ServerType = ''
    }
  )
  Get-Annotation -Entity $_ -CustomAttribute $caNames -ErrorAction SilentlyContinue |
  ForEach-Object -Process {
    $obj."$($_.Name)" = $_.Value
  }
  $obj
} |
Export-Csv C:\Users\myvmsexport.csv -NoTypeInformation -UseCulture


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

View solution in original post

4 Replies
LucD
Leadership
Leadership
Jump to solution

Try something like this

$vmlist = Import-Csv "C:\Users\myvms.csv"
$caNames = 'VMOwner', 'OwnerEmail', 'ServerType'

Get-VM -Name $vmlist.Name |
ForEach-Object -Process {
  $obj = New-object -TypeName PSObject -Property (
    [ordered]@{
      VM = $_.Name
      VMOwner = ''
      OwnerEmail = ''
      ServerType = ''
    }
  )
  Get-Annotation -Entity $_ -CustomAttribute $caNames -ErrorAction SilentlyContinue |
  ForEach-Object -Process {
    $obj."$($_.Name)" = $_.Value
  }
  $obj
} |
Export-Csv C:\Users\myvmsexport.csv -NoTypeInformation -UseCulture


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

andrew_uk
Enthusiast
Enthusiast
Jump to solution

Amazing...thank you!

 

So all 3 custom attributes need to be put into a variable first? and to get the exported format, that required something else

 

 

Tags (1)
Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

To be able to select the correct Custom Attributes, you do need to provide the names somewhere.

I used a Foreach-Object, since this passes the objects over the pipeline, and so it can be exported to a CSV after the Foreach-Object.


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

jeep2004
Enthusiast
Enthusiast
Jump to solution

HI 

I continue from the same post....

I need a report of "VMs including the value "notes" and including some of the custom attribute.

Some of the custom attribute have an additional value that I created - for example "days" with "number".
if number higher than 30, the VMs Should be on report . If it is below 30 that will not appear in the report.

the table of the report need to be like this

VMname, custom attribute1,custom attribute2,Days,Folder name, Cluster name, Notes, vspherename 

thank you

Reply
0 Kudos