VMware Cloud Community
AGFlora
Enthusiast
Enthusiast
Jump to solution

How to get names of all Datacenters in vSphere

Hi

The following code retrieves the ful path of VMDK files.

How can I get this info from all of the DC's in my environment instead of querying one DC at a time?

CODE:

Get-DataCenter $DC | Get-Datastore | Where-Object {$_.Name -notlike "DataStore1*"} |
Sort-Object -Property Name |
Select-Object -Property Name,
@{Name="CapacityGB";Expression={[Math]::Round($_.CapacityGB,0)}},
@{Name="UsedSpaceGB";Expression={[Math]::Round($_.CapacityGB-$_.FreeSpaceGB,0)}},
@{Name="FreeSpaceGB";Expression={[Math]::Round($_.FreeSpaceGB,0)}},
@{Name="UncommittedSpaceGB";Expression={
  if (-not $_.ExtensionData.Summary.Uncommitted) {
    0
  }
  else {
    [Math]::Round($_.ExtensionData.Summary.Uncommitted/1GB,0)
  }
}},
@{Name="ProvisionedSpaceGB";Expression={
    $Summary = $_.ExtensionData.Summary
    if (-not $Summary.Uncommitted) {
      [Math]::Round(($Summary.Capacity - $Summary.FreeSpace)/1GB,0)
    }
    else {
      [Math]::Round(($Summary.Capacity - $Summary.FreeSpace + $Summary.Uncommitted)/1GB,0)
    }
}},
@{Name="PercentageUsed";Expression={[Math]::Round(100*($_.CapacityGB-$_.FreeSpaceGB)/$_.CapacityGB,0)}},
@{Name="PercentageFreeSpace";Expression={[Math]::Round(100*$_.FreeSpaceGB/$_.CapacityGB,0)}},
@{Name="PercentageProvisioned";Expression={
    $Summary = $_.ExtensionData.Summary
    if (-not $Summary.Uncommitted) {
      [Math]::Round(100*($Summary.Capacity - $Summary.FreeSpace)/$Summary.Capacity,0)
    }
    else {
      [Math]::Round(100*($Summary.Capacity - $Summary.FreeSpace + $Summary.Uncommitted)/$Summary.Capacity,0)
    }
}},
@{Name="VMs";Expression={[string]::Join(";",($_.ExtensionData.Vm | ForEach-Object {Get-View -Id $_}).Name)}},
@{Name="VMDKs";Expression={[string]::Join(";",(&{
    $Datastore = $_
    $Datastore.ExtensionData.Vm |
    ForEach-Object {
      Get-View -Id $_ |
      Get-VIObjectByVIView |
      Get-HardDisk |
      Where-Object {$_.FileName -like "*$($Datastore.Name)*"}
    }
  }).FileName)
 
 
  }} | Export-CSV $FileName -NoTypeInformation -UseCulture

0 Kudos
1 Solution

Accepted Solutions
RvdNieuwendijk
Leadership
Leadership
Jump to solution

The easiest way is to make $DC equal to '*'.

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition

View solution in original post

0 Kudos
2 Replies
RvdNieuwendijk
Leadership
Leadership
Jump to solution

The easiest way is to make $DC equal to '*'.

Blog: https://rvdnieuwendijk.com/ | Twitter: @rvdnieuwendijk | Author of: https://www.packtpub.com/virtualization-and-cloud/learning-powercli-second-edition
0 Kudos
AGFlora
Enthusiast
Enthusiast
Jump to solution

Thanks!

0 Kudos