Difference between revisions of "Script Extracts and Examples (PowerCLI)"

Jump to navigation Jump to search
→‎Script Extracts: Added "ESX Datastore to LUN Mapping"
(→‎ESX Host: Added Get VLANs)
(→‎Script Extracts: Added "ESX Datastore to LUN Mapping")
Line 737: Line 737:
}
}
</source>
</source>
=== ESX Datastore to LUN Mapping ===
Its bizarrely difficult to be able to map VMware presented datastore names to the underlying LUN's, despite the fact that its readily available via the VI Client.  The following was adapted from the work of ''Catman'' found in this forum thread - http://communities.vmware.com/thread/240466#240466.  As I expect to maybe want some of the other fields available during the working my notes from working through this are to be found [[Datastore to LUN Mapping (PowerCLI)|here]]
<source lang="powershell">
# Thieved and adapted from the good work by catman...
# http://communities.vmware.com/thread/240466#240466
$objESX = Get-VMHost "My_ESX*"
# Get .NET views for host and storage system
$objViewESX = Get-View -id $objESX.id
$objViewESXstorageSys = Get-View -id $objViewESX.ConfigManager.StorageSystem
# Get FC HBAs
$HBAs = $objViewESXstorageSys.StorageDeviceInfo.HostBusAdapter | Where-Object {$_.Key -like "*FibreChannelHba*"}
foreach ($hba in $HBAs) {
    # Enumerate LUNs
    $LUNcount = $objViewESXstorageSys.StorageDeviceInfo.MultiPathInfo.Lun.Length
   
    for ($LUNidx = 0; $LUNidx -lt $LUNcount; $LUNidx++ ) {
        $objScsiLUN = $objViewESXstorageSys.StorageDeviceInfo.MultiPathInfo.Lun[$LUNidx]
       
        # Enumerate paths on LUN
        $PathCount = $objScsiLUN.Path.Length
       
        for ($PathIdx = 0; $PathIdx -lt $PathCount; $PathIdx++) {
            $objSCSIpath = $objViewESXstorageSys.StorageDeviceInfo.MultiPathInfo.Lun[$LUNidx].Path[$PathIdx]
           
            # Only care about one path, active on current HBA
            if (($objSCSIpath.PathState -eq "active") -and ($objSCSIpath.Adapter -eq $hba.Key)) {
                # Now get the disk that we want
                $objSCSIdisk = $objViewESXstorageSys.StorageDeviceInfo.ScsiLun | Where-Object{ ($_.CanonicalName -eq $objScsiLUN.Id) -and ($_.DeviceType -eq "disk") }
               
                # Now get the datastore info for disk
                $MountCount = $objViewESXstorageSys.FileSystemVolumeInfo.MountInfo.Length
               
                for ($MountIdx = 0; $MountIdx -lt $MountCount; $MountIdx++ ) {
                    if ($objViewESXstorageSys.FileSystemVolumeInfo.MountInfo[$MountIdx].Volume.Type -eq "VMFS" ) {
                        $objVolume = $objViewESXstorageSys.FileSystemVolumeInfo.MountInfo[$MountIdx].Volume
                       
                        $ExtentCount = $objVolume.Extent.Length
                       
                        for ($ExtentIdx = 0; $ExtentIdx -lt $ExtentCount; $ExtentIdx++ ) {
                            $objExtent = $objVolume.Extent[$ExtentIdx]
                           
                            # Match extent name to disk name
                            if ($objExtent.DiskName -eq $objSCSIdisk.CanonicalName) {
                                Write-Host($objSCSIdisk.Vendor + " " + $objSCSIdisk.Model + " " + $objSCSIdisk.CanonicalName + "`t" + $objVolume.Name)
                            }
                        }
                    }
                }
            }
        }
    }
}
</source>
Gives an output a bit like this...
<pre>
HP      HSV200          vmhba1:0:20  VMFS-DS-08
HP      HSV200          vmhba1:0:7    VMFS-DS-01
HP      HSV200          vmhba1:0:22  VMFS-DS-10
HP      HSV200          vmhba1:0:8    VMFS-DS-02
HP      HSV200          vmhba1:0:10  VMFS-DS-03
HP      HSV200          vmhba1:0:18  VMFS-DS-07
HP      HSV200          vmhba1:0:21  VMFS-DS-09
HP      HSV200          vmhba1:0:12  VMFS-DS-05
HP      HSV200          vmhba1:0:11  VMFS-DS-04
HP      HSV200          vmhba1:0:3    VMFS-DS-Templates
HP      HSV200          vmhba1:0:2    VMFS-DS-ISOs
HP      HSV200          vmhba1:0:30  VMFS-DS-SCRATCH
HP      HSV200          vmhba1:0:13  VMFS-DS-06
</pre>


=== Ping All VM's On ESX ===
=== Ping All VM's On ESX ===

Navigation menu