PowerCLI script to get NAA and Datastore name of all storage on ESX Server

Use the following script to get a list of Datastores, their respective capacities and their corresponding NAAs on an ESX Server:

$esxName = "esx01.vi.local"

Connect-VIServer vs01.vi.local

New-VIProperty -Name lunDatastoreName -ObjectType ScsiLun -Value {
    param($lun)

    $ds = $lun.VMHost.ExtensionData.Datastore | %{Get-View $_} | `
        where {$_.Summary.Type -eq "VMFS" -and
            ($_.Info.Vmfs.Extent | where {$_.DiskName -eq $lun.CanonicalName})}
    if($ds){
        $ds.Name
    }
} -Force | Out-Null

Get-VMHost -Name $esxName | Get-ScsiLun | Select CanonicalName, CapacityMB, lunDatastoreName

To export the results to a CSV file run same script adding the following to the end of the last line:

 | Export-csv C:\DatastoreInfo.csv

I can’t remember where I got this script from, whoever it was, I thank-you sincerely! The Get-ScsiLun command line reference can be found here.

Credit to Whowe who added his method in the comments below which worked perfectly. His script code is directly below, I have added some connection parameters to show you how I would use it to get datastore info:

$esxName = "esx01.vi.local"

Connect-VIServer vs01.vi.local

Get-VMHost -Name $esxName | Get-Datastore |

Where-Object {$_.ExtensionData.Info.GetType().Name -eq "VmfsDatastoreInfo"} |

ForEach-Object {

if ($_)

{

$Datastore = $_

$Datastore.ExtensionData.Info.Vmfs.Extent |

Select-Object -Property @{Name="Name";Expression={$Datastore.Name}},

DiskName

}

}

The output from the script is below which shows NAA as well as Datastore info (but not capacity):

[vSphere PowerCLI] C:\Admin> .\GetDSInfo.ps1

Name                           Port  User
----                           ----  ----
vs01.vi.local                  443   DOMAIN\admin

Name     : datastore-1-R1
DiskName : naa.6848f690ec8be200191117982eb0b641

Name     : datastore-2-R5
DiskName : naa.60000970000295800173533030384646

Hope this benefits someone out there and thanks to Whowe for his contribution!

PowerCLI command to list all PortGroups on an ESX/ESXi host

A Lead Engineer asked me to send him a list of all PortGroups on a specific ESX host (running ESX 4.1). Due to the time and effort required to obtain this info manually, I had to use PowerCLI. I ran the command below:

Get-VirtualPortGroup -VMHost esx1 | select Name, VirtualSwitch, VLanId

It outputs results similar to the below:

Name                     VirtualSwitch      VLanId
----                     -------------      ------
DMZ-VLAN                 vSwitch0           1139
Inside-VLAN              vSwitch0           805
VMNet                    vSwitch1           0
VMNet-KS                 vSwitch1           124
VMNet-SC                 vSwitch1           0
VMNet-vmk                vSwitch1           0
Backup-Inside-VLAN       vSwitch2           648
Backup-DMZ-VLAN          vSwitch2           647

To get the results in a spreadsheet export the results to a csv file per the below:

Get-VirtualPortGroup -VMHost esx1 | select Name, VirtualSwitch, VLanId | | Export-Csv C:\VMHostNetworkInfo.csv

The full command line reference for Get-VirtualPortGroup is available here.

PowerCLI one-liner to change PortGroup membership on all VMs in a cluster

In my previous post I provided a PowerCLI command to rename PortGroups across multiple hosts in an ESX/ESXi cluster. You would usually do this when making network segment/infrastructure changes. These would be done in two phases:

  1. Rename PortGroups and tag them with the new vlan(s).
  2. Move VMs from the old network segment (PortGroups) into the new one.

The following command would do it:

Get-Cluster "ESX Cluster" | Get-VM |Get-NetworkAdapter | Where {$_.NetworkName -eq "Inside-VLAN" } |Set-NetworkAdapter -NetworkName "DMZ-VLAN" -Confirm:$false

So the command connects to all the VMs running in Cluster ESX Cluster and changes the networks or PortGroups the vNICs are located in from Inside-VLAN to DMZ-VLAN. The last parameter -Confirm:$false suppresses the prompt to confirm the PortGroup changes.

PowerCLI one-liner to rename a PortGroup on all ESX/ESXi hosts in a cluster

It can be a huge hassle to manually rename PortGroups across multipleĀ ESX/ESXi hosts in a cluster. This is where PowerCLI becomes super handy – the one-liner below will take of it:

Get-Datacenter London-DC | Get-Cluster "ESX Cluster" | Get-VMHost | Get-VirtualPortGroup -Name "Inside-VLAN" | Set-VirtualPortGroup -Name "DMZ-VLAN" -vlanid 1002

The above command connects into the London-DC Datacenter object and then to the Cluster ESX Cluster. It will get all the ESX/ESXi servers in the cluster by running Get-VMHost. It then locates the PortGroup called Inside-VLAN and renames it to DMZ-VLAN and tags it with vlan ID 1002.

If you have upwards of 5 ESX/ESXi hosts it will save you a lot of time and effort.

PowerCLI one-liner to rename a PortGroup on an ESX/ESXi host

If having to login to your VMware vSphere infrastructure is a hassle just to rename a PortGroup on a single ESX/ESXi host is a hassle you can run the following PowerCLI one-liner to take care of business:

Get-VMHost -name esx1 | Get-VirtualPortGroup -Name "Inside-VLAN" | Set-VirtualPortGroup -Name "DMZ-VLAN"

The command connects to the ESX/ESXi host called esx1 and renames the PortGroup called Inside-VLAN to DMZ-VLAN.

If the PortGroup being changed is the native vlan on a trunk port then you would not tag it. However, if it was tagged with a vlan id and you were changing your network configuration this could also mean that you want to change the vland id. If so add the -vlanid parameter followed by the id:

Get-VMHost -name esx1 | Get-VirtualPortGroup -Name "Inside-VLAN" | Set-VirtualPortGroup -Name "DMZ-VLAN" -vlanid 1002

Simple as that!