-- Leo's gemini proxy

-- Connecting to gmi.runtimeterror.dev:1965...

-- Connected

-- Sending request

-- Meta line: 20 text/gemini;lang=en-US

💻 [runtimeterror $]

2022-01-13

Using PowerCLI to list Linux VMs and Datacenter Locations



I recently needed to export a list of all the Linux VMs in a rather large vSphere environment spanning multiple vCenters (and the entire globe), and I wanted to include information about which virtual datacenter each VM lived in to make it easier to map VMs to their physical location.

I've got a `Connect-vCenters` function [1] that I use to quickly log into multiple vCenters at once. That then enables me to run a single query across the entire landscape - but what query? There isn't really a direct way to get datacenter information out of the results generated by `Get-VM`; I could run an additional `Get-Datacenter` query against each returned VM object but that doesn't sound very efficient.

[1] `Connect-vCenters` function

What I came up with is using `Get-Datacenter` to enumerate each virtual datacenter, and then list the VMs matching my query within:

$linuxVms = foreach( $datacenter in ( Get-Datacenter )) {
  Get-Datacenter $datacenter | Get-VM | Where { $_.ExtensionData.Config.GuestFullName -notmatch "win" -and $_.Name -notmatch "vcls" } | `
  Select @{ N="Datacenter";E={ $datacenter.Name }},
  Name,
  Notes,
  @{ N="Configured OS";E={ $_.ExtensionData.Config.GuestFullName }},  # OS based on the .vmx configuration
  @{ N="Running OS";E={ $_.Guest.OsFullName }}, # OS as reported by VMware Tools
  @{ N="Powered On";E={ $_.PowerState -eq "PoweredOn" }},
  @{ N="IP Address";E={ $_.ExtensionData.Guest.IpAddress }}
}
$linuxVms | Export-Csv -Path ./linuxVms.csv -NoTypeInformation -UseCulture

This gave me a CSV export with exactly the data I needed.




---


📧 Reply by email



Related articles


Enabling FIPS Compliance Fixes Aria Lifecycle 8.14

I Ditched vSphere for Proxmox VE

PSA: Microsoft's KB5022842 breaks Windows Server 2022 VMs with Secure Boot

---


Home

This page on the big web

-- Response ended

-- Page fetched on Fri May 10 21:52:43 2024