Below are some PowerCLI snippets I have used to quickly gather information on an environment, as well as to help document settings. The following were grabbed from PowerCLI gurus such as LucD and Alan Renouf, and I have tried to cite the author’s work with each snippet (Note: I was unable to find where I grabbed some of these, but please shoot me an email if I accidentally didn’t cite the source).

These all exports into CSV format to allow easy merging into a document. I tend to include the code to grab the information within the overview document to allow the data to be easily updated on a periodic basis by anyone with PowerCLI installed, as well.

Cluster and Host Hardware Overview

Get-VMHost | Select Parent,Name,Manufacturer,Mode,Version,MemoryTotalMB,Model,NumCpu | Export-Csv “PathHere”

Datastore Capacity and Usage (Credit: LucD.info)

$DS = @()
Get-Cluster | ForEach-Object {
  $Cluster = $_
  $Cluster | Get-VMHost | ForEach-Object {
    $VMHost = $_
    $VMHost | Get-DataStore | Where-Object { $_.Name -notlike "local*"} | ForEach-Object {
      $out = "" | Select-Object Cluster, DSName, FreespaceGB, CapacityGB, PercentFree
      $out.Cluster = $Cluster.Name
      $out.DSName = $_.Name
      $out.FreespaceGB = $($_.FreespaceMB / 1024).tostring("F02")
      $out.CapacityGB = $($_.CapacityMB / 1024).tostring("F02")
      $out.PercentFree = (($_.FreespaceMB) / ($_.CapacityMB) * 100).tostring("F02")
      $DS += $out
    }
  }
}
$DS | Sort-Object Cluster, DSName –Unique | Export-Csv “PathHere”

List of iSCSI Targets by Host (Credit: LucD.info)

Get-VMHost | Get-View | %{
     $esx = $_
     $esx.Config.StorageDevice.HostBusAdapter | where {$_.GetType().Name -eq "HostInternetScsiHba"} | %{
          $hba = $_
          $_.ConfiguredSendTarget | `
               Select @{N="ESX Name";E={$esx.Name}},
               @{N="HBA Device";E={$hba.Device}},
               @{N="IScsi Name";E={$hba.IScsiName}},
               @{N="IScsi Target";E={$_.Address}}
     }
} | Export-Csv "PathHere" -NoTypeInformation –UseCulture

Service Console Details by Host (Credit: Virtu-al.net)

Get-VMHost | Get-VMHostNetwork | Select Hostname, ConsoleGateway, DNSAddress -ExpandProperty ConsoleNic | Select Hostname, PortGroupName, IP, SubnetMask, ConsoleGateway, Devicename | Export-Csv “PathHere”

VMKernel Details by Host

Get-VMHost | Get-VMHostNetwork | Select Hostname, VMkernelGateway -ExpandProperty VirtualNic | Select Hostname, PortGroupName, IP, SubnetMask, VMkernelGateway, Devicename | Export-Csv "PathHere"

Port Group Details by Host

Get-VMHost  | ForEach-Object -Process {
  Tee-Object -InputObject $_ -Variable Temp | Get-VirtualPortGroup |
  Select @{N="VMHost";E={$Temp.Name}},Name,VirtualSwitch,VirtualSwitchName,VLanId
} | Export-Csv "PathHere”

vSwitch Details by Host (Credit: Virtu-al.net)

$NetworkInfo = @()
Foreach ($VMHost in (Get-View -ViewType HostSystem | Where {$_.Runtime.ConnectionState -ne "disconnected"})){
    Write $VMHost.Name
    $NetworkSystem = Get-View $VMHost.ConfigManager.NetworkSystem
    Foreach ($PG in $NetworkSystem.NetworkInfo.PortGroup){
        $Details = "" | Select VMHost, vSwitch, PortGroup, ActiveNics, StandbyNics
        $Details.VMHost = $VMHost.Name
        $Details.Portgroup = $PG.Spec.Name
        If ((($PG.ComputedPolicy.NicTeaming.NicOrder.ActiveNic | Select -ExpandProperty $ActiveNic).Length) -gt 1){
            $Details.ActiveNics = [string]::join(';',($PG.ComputedPolicy.NicTeaming.NicOrder.ActiveNic | Select -ExpandProperty $ActiveNic))
        }
        Else {
            $Details.ActiveNics = ($PG.ComputedPolicy.NicTeaming.NicOrder.ActiveNic | Select -ExpandProperty $ActiveNic)
        }
        If ((($PG.ComputedPolicy.NicTeaming.NicOrder.StandbyNic | Select -ExpandProperty $StandbyNic).Length) -gt 1){
            $Details.StandbyNics = [string]::join(';',($PG.ComputedPolicy.NicTeaming.NicOrder.StandbyNic | Select -ExpandProperty $StandbyNic))
        }
        Else{
            $Details.StandbyNics = ($PG.ComputedPolicy.NicTeaming.NicOrder.StandbyNic | Select -ExpandProperty $StandbyNic)
        }
        Foreach ($VS in $NetworkSystem.NetworkInfo.vSwitch){
            If ($VS.Name -eq $PG.Spec.vSwitchName){
                $Details.vSwitch = $VS.Name
                }
        }
        $NetworkInfo += $Details
    }
}
$NetworkInfo | Sort VMHost, VSwitch, PortGroup | Export-Csv "PathHere" –NoTypeInformation

Another great tool that should be in every Admin’s tool-belt is RVTools, which leverages PowerCLI to generate a great deal of information. All of the data generated through RVTools can also be exported to CSV and merged into the documentation.