VMware Cloud Community
corydori
Contributor
Contributor
Jump to solution

How to export tags/category + all other default columns

Hi,
I was able to find a script on these forums that pulled a list of VM's with tag/category but I am trying to figure out how to add all these columns as well as PowerState, Notes, Guest, NumCpu, CoresPerSocket, MemoryMB, MemoryGB, VMHostId, VMHost, Folder, Version, GuestId, UsedSpaceGB, ProvisionedSpaceGB.

 

Here is the code sample I found.  How do I add all of the CPU, Memory, etc columns to see the below sample? Any help appreciated

#
# Get a list of all VMs in connected vCenter
#
$allVM = Get-VM | Sort-Object -Property Name
#
# Get all Categories & Tags
#
$assigned = $allVM | Get-TagAssignment
$ordered = $assigned.Tag.Category.Name | Sort-Object -Unique

#
# Cycle through all VMs and associate the Categories & Tags to those with
# Categories & Tags assigned.
#
$allVM | ForEach-Object -Process {
$obj = [ordered]@{
VM = $_.Name
}

# Create a column header using the Categories.
$ordered |
ForEach-Object -Process {
$obj.Add($_, '')
}
$assigned | Where-Object { $_.Entity.Name -eq $obj.VM } |
Group-Object -Property {$_.Tag.Category.Name} |
ForEach-Object -Process {
$obj[$_.Name] = $_.Group.Tag.Name -join '|'
}
New-Object -TypeName PSObject -Property $obj
} | Export-Csv -Path "C:\Tags Export\Test3\test1.csv" -NoTypeInformation -UseCulture

 

Reply
0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Just add those properties on the $obj before all the Tag columns are added.

Something like this for example

#
# Get a list of all VMs in connected vCenter
#
$allVM = Get-VM | Sort-Object -Property Name
#
# Get all Categories & Tags
#
$assigned = $allVM | Get-TagAssignment
$ordered = $assigned.Tag.Category.Name | Sort-Object -Unique

#
# Cycle through all VMs and associate the Categories & Tags to those with
# Categories & Tags assigned.
#
$allVM | ForEach-Object -Process {
  $obj = [ordered]@{
    VM = $_.Name
    PowerState = $_.PowerState
    Notes = $_.Notes
    Guest = $_.Guest.OSFullName
    NumCpu = $_.NumCpu
    CoresPerSocket = $_.CoresPerSocket
    MemoryMB = $_.MemoryMB
    MemoryGB = $_.MemoryGB
    VMHostId = $_.VMHostId
    VMHost = $_.VMHost.Name
    Folder = $_.Folder.Name
    Version = $_.Version
    GuestId = $_.GuestId
    UsedSpaceGB = $_.UsedSpaceGB
    ProvisionedSpaceGB = $_.ProvisionedSpaceGB
  }

  # Create a column header using the Categories.
  $ordered |
  ForEach-Object -Process {
    $obj.Add($_, '')
  }
  $assigned | Where-Object { $_.Entity.Name -eq $obj.VM } |
  Group-Object -Property { $_.Tag.Category.Name } |
  ForEach-Object -Process {
    $obj[$_.Name] = $_.Group.Tag.Name -join '|'
  }
  New-Object -TypeName PSObject -Property $obj
} | Export-Csv -Path "C:\Tags Export\Test3\test1.csv" -NoTypeInformation -UseCulture


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

View solution in original post

2 Replies
LucD
Leadership
Leadership
Jump to solution

Just add those properties on the $obj before all the Tag columns are added.

Something like this for example

#
# Get a list of all VMs in connected vCenter
#
$allVM = Get-VM | Sort-Object -Property Name
#
# Get all Categories & Tags
#
$assigned = $allVM | Get-TagAssignment
$ordered = $assigned.Tag.Category.Name | Sort-Object -Unique

#
# Cycle through all VMs and associate the Categories & Tags to those with
# Categories & Tags assigned.
#
$allVM | ForEach-Object -Process {
  $obj = [ordered]@{
    VM = $_.Name
    PowerState = $_.PowerState
    Notes = $_.Notes
    Guest = $_.Guest.OSFullName
    NumCpu = $_.NumCpu
    CoresPerSocket = $_.CoresPerSocket
    MemoryMB = $_.MemoryMB
    MemoryGB = $_.MemoryGB
    VMHostId = $_.VMHostId
    VMHost = $_.VMHost.Name
    Folder = $_.Folder.Name
    Version = $_.Version
    GuestId = $_.GuestId
    UsedSpaceGB = $_.UsedSpaceGB
    ProvisionedSpaceGB = $_.ProvisionedSpaceGB
  }

  # Create a column header using the Categories.
  $ordered |
  ForEach-Object -Process {
    $obj.Add($_, '')
  }
  $assigned | Where-Object { $_.Entity.Name -eq $obj.VM } |
  Group-Object -Property { $_.Tag.Category.Name } |
  ForEach-Object -Process {
    $obj[$_.Name] = $_.Group.Tag.Name -join '|'
  }
  New-Object -TypeName PSObject -Property $obj
} | Export-Csv -Path "C:\Tags Export\Test3\test1.csv" -NoTypeInformation -UseCulture


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

corydori
Contributor
Contributor
Jump to solution

Thanks LucD! You're a savior!

Reply
0 Kudos