VMware Cloud Community
Solution_Provid
Contributor
Contributor
Jump to solution

PowerCLI Question

We have multiple vCenters and Datastores and I have been manually creating tables merging information from different commands; however, I believe there must be a way to do this easier leveraging more the capabilities of PowerCLI. The desired output that I produce is a spreadsheet table that looks like this:

vCenterName1   Datastore1   VM1   PowerState

vCenterName1   Datastore1   VM2   PowerState

vCenterName2   Datastore1   VM1   PowerState

vCenterName3   Datastore1   VM1   PowerState

…and so on.

The first thing I do is produce a list of the Datastores on the first vCenter with the following command:

Get-Datastore | Select Name | Out-File -FilePath C:\Path\vCenterName1.TXT

Then I use this command to iterate through the file produced above:

ForEach($File In Get-Content C:\Path\vCenterName1.TXT){Echo "Datastore:$File";Get-VM -DataStore $File|Select PowerState,Name|Format-Table -Wrap -AutoSize}

The output of the command above looks like this on the computer screen:

Datastore:Datastore1

PowerState   Name

PoweredOn VM1

PoweredOff VM2

Datastore:Datastore2

PowerState   Name

PoweredOn VM1

So, I then select all the screen text, which I copy to Excel and after extensive manipulation of all the resulting files and screen captures I come up with the desired output on the top of this message. As you can see it is quite cumbersome as we have thousands of VMs and this table gets regenerated frequently as things are continually changing.

Any recommendations on how to use the power of PowerCLI to produce this result? Thank you for your attention to this matter.

0 Kudos
1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Sure, you could do something like this.
Make sure you are connected to all vCenters before running the script.

Get-VM |

Select Name,PowerState,

    @{N='vCenter';E={([uri]$_.ExtensionData.Client.ServiceUrl).Host}},

    @{N='Datastore';E={(Get-Datastore -VM $_).Name -join '|'}} |

Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


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

View solution in original post

0 Kudos
2 Replies
LucD
Leadership
Leadership
Jump to solution

Sure, you could do something like this.
Make sure you are connected to all vCenters before running the script.

Get-VM |

Select Name,PowerState,

    @{N='vCenter';E={([uri]$_.ExtensionData.Client.ServiceUrl).Host}},

    @{N='Datastore';E={(Get-Datastore -VM $_).Name -join '|'}} |

Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture


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

0 Kudos
Solution_Provid
Contributor
Contributor
Jump to solution

WOW!!! Excellent! This EXACTLY what I needed! Thank you! I really appreciate such prompt and effective response! Much appreciated! Smiley Happy

0 Kudos