sharpwg
Contributor
Contributor

PowerCLI - Create CSV file containing VM Name/Tags/OS/Power State

Hi,

I'm trying to use PowerCLI with a vCenter instance to script the creation of a .csv file containing 4 columns for every VM in the vCenter instance:

1.  Name of the VM

2. Any tag names under the "Owner" category (if no tag, have the cell be blank)

3. Operating System of the VM (as shown under Guest OS on the VM view)

4. Power State of the VM (on/off) 

Is this possible, and if so how could I accomplish it? 

 

Here's what I am trying, but the Owner column displays FALSE...

Get-VM | Select Name,@{N="Owner";E={$_.Tag -like "Owner*"}},@{N="Operating System";E={$_.ExtensionData.Config.GuestFullname}},@{N="Power State";E={$_.PowerState}} | Export-Csv "C:\Inventory.csv" -NoTypeInformation -UseCulture

0 Kudos
3 Replies
LucD
Leadership
Leadership

There is no Tag property on the object returned by Get-VM, use the Get-TagAssignment cmdlet.

Get-VM |
Select-Object Name, @{N = "Owner"; E = { (Get-TagAssignment -Entity $_ -Category 'Dummy').Tag.Name } },
  @{N = "Operating System"; E = { $_.ExtensionData.Config.GuestFullName } },
  @{N = "Power State"; E = { $_.PowerState } } |
Export-Csv "C:\Inventory.csv" -NoTypeInformation -UseCulture


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

sharpwg
Contributor
Contributor

this works awesome is there a way to do this on 2 seperate vcenters at once and have both the results added to the same csv file (2 different sheets)? 

0 Kudos
LucD
Leadership
Leadership

Provided you are connected to both vCenters, you could do something like this

$global:defaultVIServers |
ForEach-Object -Process {
  $vc = $_
  Get-VM -Server $vc |
  Select-Object Name,
    @{N='vCenter';E={$vc.Name}},
    @{N = "Owner"; E = { (Get-TagAssignment -Entity $_ -Category 'Dummy' -Server $vc).Tag.Name } },
    @{N = "Operating System"; E = { $_.ExtensionData.Config.GuestFullName } },
    @{N = "Power State"; E = { $_.PowerState } }
} |
Export-Csv "C:\Inventory.csv" -NoTypeInformation -UseCulture


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

0 Kudos