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

Jump to navigation Jump to search
→‎Script Extracts: Re-orged sections
(→‎VM's with Snapshots Running: Updated for Get-Snapshot size bug)
(→‎Script Extracts: Re-orged sections)
Line 323: Line 323:


== Script Extracts ==
== Script Extracts ==
=== VM's with Datastores List ===
=== Virtual Machine ===
List of Virtual Machines, and their datastores (with usage)
==== VM's with Host and Cluster List ====
<source lang="powershell">
$datastoreExp = @{N="Datastore"; E={ ($_ | get-datastore | select-object -first 1).Name }}
$diskSizeExp = @{N="Total Disk"; E={ ($_ | get-harddisk | measure-object -property CapacityKB -sum).Sum }}
get-vm | select Name, $datastoreExp, $diskSizeExp | sort -property Datastore,"Total Disk"
</source>
 
=== VM's with Host and Cluster List ===
<source lang="powershell">
<source lang="powershell">
$vms = Get-VM | sort -property Name
$vms = Get-VM | sort -property Name
Line 340: Line 333:
</source>
</source>


=== VM's Inventory CSV ===
==== VM's Inventory CSV ====


<source lang="powershell">
<source lang="powershell">
Line 397: Line 390:
</source>
</source>


=== VM Storage Usage ===
==== VM's with Snapshots Running ====
'''Total''' storage usage (including any snapshots, logs, etc, etc), not just the VMDK files.
Adapted from post by Arnim van Lieshout http://www.van-lieshout.com/2009/07/how-big-is-my-vm/
 
<source lang="powershell">
function Get-VMDiskUsage($vm2do)
{
    #Initialize variables
    $VMDirs =@()
    $VMSize = 0
 
    $searchSpec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec
    $searchSpec.details = New-Object VMware.Vim.FileQueryFlags
    $searchSpec.details.fileSize = $TRUE
 
    Get-View -VIObject $vm2do | % {
        #Create an array with the vm's directories
        $VMDirs += $_.Config.Files.VmPathName.split("/")[0]
        $VMDirs += $_.Config.Files.SnapshotDirectory.split("/")[0]
        $VMDirs += $_.Config.Files.SuspendDirectory.split("/")[0]
        $VMDirs += $_.Config.Files.LogDirectory.split("/")[0]
        #Add directories of the vm's virtual disk files
        foreach ($disk in $_.Layout.Disk) {
            foreach ($diskfile in $disk.diskfile){
                $VMDirs += $diskfile.split("/")[0]
            }
        }
        #Only take unique array items
        $VMDirs = $VMDirs | Sort | Get-Unique
 
        foreach ($dir in $VMDirs){
            $ds = Get-Datastore ($dir.split("[")[1]).split("]")[0]
            $dsb = Get-View (($ds | get-view).Browser)
            $taskMoRef  = $dsb.SearchDatastoreSubFolders_Task($dir,$searchSpec)
            $task = Get-View $taskMoRef
 
            while($task.Info.State -eq "running" -or $task.Info.State -eq "queued"){$task = Get-View $taskMoRef }
            foreach ($result in $task.Info.Result){
                foreach ($file in $result.File){
                    $VMSize += $file.FileSize
                }
            }
        }
    }
 
    # VM disk usage in GB
    $VMSize/1048576000
}
Get-VMDiskUsage (Get-VM "MyVM")
</source>
 
=== VM's with Snapshots Running ===
Be aware that there's a bug in the way <code>Get-Snapshot</code> handles VM's in VI4, in that for the snapshot size it tends to report the maximum size a snapshot file could become, not the actual size that it is.  As a result there is a workaround to found here - http://blogs.vmware.com/vipowershell/2010/09/snapshot-size.html.  I've found that this reports smaller sizes for VI3 snapshots as well (which match the actual disk usage in the few occasions I've bothered to check).
Be aware that there's a bug in the way <code>Get-Snapshot</code> handles VM's in VI4, in that for the snapshot size it tends to report the maximum size a snapshot file could become, not the actual size that it is.  As a result there is a workaround to found here - http://blogs.vmware.com/vipowershell/2010/09/snapshot-size.html.  I've found that this reports smaller sizes for VI3 snapshots as well (which match the actual disk usage in the few occasions I've bothered to check).


Line 531: Line 473:
...then I've just replaced <code>Get-Snapshot</code> with <code>CalculateVMSnapshotsSizeMB</code>
...then I've just replaced <code>Get-Snapshot</code> with <code>CalculateVMSnapshotsSizeMB</code>


=== VM's Effective CPU Shares ===
==== VM's Effective CPU Shares ====
Calculates the effective relative CPU shares of VM's contained within resource pools.  Can only handle resource pool depth of 1.
Calculates the effective relative CPU shares of VM's contained within resource pools.  Can only handle resource pool depth of 1.


Line 610: Line 552:
</source>
</source>


=== ESX NIC Info ===
==== Ping All VM's On ESX ====
Useful sanity check prior to and after network level changes
<source lang="Powershell">
$esxToFind = "MyESX*"
 
$ESX = get-vmhost $esxToFind
if (!$ESX) {
    Write-Host "ERROR: No ESX found with name matching expression $esxToFind" -Background Red -ForegroundColor DarkRed
    Exit
}
 
# Get list of VM's on $ESX
$VMs = $ESX | get-vm | Where {$_.PowerState -eq "PoweredOn"} | Sort -property Name
$objPing = New-Object system.Net.NetworkInformation.Ping
 
Foreach ($VM in $VMs) {
    Write-Host $VM.Name.PadRight(20) -nonewline
   
    # Get guest's primary IP address
    $ip = (Get-VMGuest -VM $vm).IPAddress[0]
    if (!$ip) {
        Write-Host "NULL - Skipping test" -Background DarkYellow -ForegroundColor Yellow
        Continue
    }
    Write-Host $ip.PadRight(17) -nonewline
    if ($ip -eq '0.0.0.0') {
        Write-Host "Skipping" -Background DarkYellow -ForegroundColor Yellow
        Continue
    }
       
    [string]$res = $objPing.Send($ip).Status
    if ($res.CompareTo("Success")) {                    # Returns 1 if $res doesn't match "Success" !!
        Write-Host $res -BackgroundColor DarkRed -ForegroundColor Red
    } else {
        Write-Host $res -BackgroundColor DarkGreen -ForegroundColor Green
    }
}
</source>
 
=== ESX ===
==== ESX NIC Info ====
Provides a list of all vmnic speeds for ESX's connected to vCentre
Provides a list of all vmnic speeds for ESX's connected to vCentre
<source lang="powershell">
<source lang="powershell">
Line 629: Line 611:
</source>
</source>


=== ESX Log Tail ===
==== ESX Log Tail ====
'''''Note that the below only seems to work for ESX3.5, I gave up trying to get it work for both v3 and v4, its a lot easier to just enable [[ESX#ESXi_Tech_Support_Mode|ESXi Tech Support Mode]] and tail the log'''''
'''''Note that the below only seems to work for ESX3.5, I gave up trying to get it work for both v3 and v4, its a lot easier to just enable [[ESX#ESXi_Tech_Support_Mode|ESXi Tech Support Mode]] and tail the log'''''


Line 695: Line 677:
</source>
</source>


=== ESX Discovered Networks Hint ===
==== ESX Discovered Networks Hint ====
This function provides the discovered network hints for the network interface its passed.  Bear in mind that its just a hint, for an ESX to be aware of a particular vLAN it needs to see traffic.  If there's no traffic it will show nothing.
This function provides the discovered network hints for the network interface its passed.  Bear in mind that its just a hint, for an ESX to be aware of a particular vLAN it needs to see traffic.  If there's no traffic it will show nothing.


Line 734: Line 716:
</source>
</source>


=== ESX CDP Info ===
==== ESX CDP Info ====
Adapted from posted by LucD on VMware forum http://communities.vmware.com/message/977487
Adapted from posted by LucD on VMware forum http://communities.vmware.com/message/977487
<source lang="powershell">
<source lang="powershell">
Line 747: Line 729:
</source>
</source>


=== ESX Datastore to LUN Mapping (ESX3) ===
==== ESX BIOS, NIC and HBA Driver Versions ====
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>
 
 
=== ESX Datastore to LUN Mapping (ESX4) ===
Produces a similar output to the above, but works for ESX4 servers.
 
<source lang="powershell">
function Get-DS-LUNs-v4 ($objESXs) {
    foreach ($cluster in (Get-View -ViewType "ClusterComputeResource")) {
      $vmhostsview = $cluster.host | % { Get-View $_ }
      $vmhostview  = $vmhostsview | Select -first 1
      $ScsiLuns    = $vmhostsview | % { $_.Config.StorageDevice.ScsiLun } | Select -unique *
      $UUIDs      = $ScsiLuns | Select -unique UUID
      $Datastores  = $vmhostsview | % { $_.Config.FileSystemVolume.MountInfo } | % { $_.Volume } | Select -Unique *
      $HostLUN    = $vmhostsview | % { $_.Config.StorageDevice.ScsiTopology.Adapter } | % { $_.Target | % { $_.LUN } } | Select -unique *
      foreach ($UUID in $UUIDs) {
        $Lun = $ScsiLuns | ? { $_.UUID -eq $UUID.UUID } | Select -first 1
        $objVolume              = "" | Select Datastore, Make, Model, LUN
 
        $objVolume.LUN      = ($HostLUN | ? { $_.ScsiLun -eq $Lun.Key } | select -unique LUN).LUN
        $objVolume.Make        = $Lun.Vendor
        $objVolume.Model        = $Lun.Model
        foreach ($vol in $Datastores) {
          if ($vol.extent | % { $_.diskname -eq $Lun.CanonicalName}) {
            $objVolume.Datastore  = $vol.Name
          }
        }
        $objVolume
      }
    }
}
</source>
 
 
=== Ping All VM's On ESX ===
Useful sanity check prior to and after network level changes
<source lang="Powershell">
$esxToFind = "MyESX*"
 
$ESX = get-vmhost $esxToFind
if (!$ESX) {
    Write-Host "ERROR: No ESX found with name matching expression $esxToFind" -Background Red -ForegroundColor DarkRed
    Exit
}
 
# Get list of VM's on $ESX
$VMs = $ESX | get-vm | Where {$_.PowerState -eq "PoweredOn"} | Sort -property Name
$objPing = New-Object system.Net.NetworkInformation.Ping
 
Foreach ($VM in $VMs) {
    Write-Host $VM.Name.PadRight(20) -nonewline
   
    # Get guest's primary IP address
    $ip = (Get-VMGuest -VM $vm).IPAddress[0]
    if (!$ip) {
        Write-Host "NULL - Skipping test" -Background DarkYellow -ForegroundColor Yellow
        Continue
    }
    Write-Host $ip.PadRight(17) -nonewline
    if ($ip -eq '0.0.0.0') {
        Write-Host "Skipping" -Background DarkYellow -ForegroundColor Yellow
        Continue
    }
       
    [string]$res = $objPing.Send($ip).Status
    if ($res.CompareTo("Success")) {                    # Returns 1 if $res doesn't match "Success" !!
        Write-Host $res -BackgroundColor DarkRed -ForegroundColor Red
    } else {
        Write-Host $res -BackgroundColor DarkGreen -ForegroundColor Green
    }
}
</source>
 
=== ESX Storage Events ===
Creates a CSV of storage events (eg path up/downs).  Useful in order to be analyse storage events, for example if you need to tally up to SAN logs etc.  Note the <code> get-VIEvent </code> is limited to 1000 results, which will typically get 1 - 2 days worth depending on your infrastructure.  To get a longer history would  require multiple calls to <code> get-VIEvent </code> using the <code>-Start</code> and <code>-Finish</code> parameters
<source lang="Powershell">
$OutputFile = "ESX-EventsStorage.csv"
 
$Results = @()
$events = Get-VIEvent -MaxSamples 1000 | Where {$_.EventTypeID -like "esx.*.storage.*"}
foreach ($event in $events) {
    $row = "" | Select Date, Host, EventID, Device, Datastore, Path
    $row.Date = $event.CreatedTime
    $row.Host = $event.Host.Name.Split(".")[0]
    $row.EventID = $event.EventTypeID
    # Allow for Argument Keys being mixed up between "problem" aka fault start and "clear" aka fault end events
    $row.Device = $event.Arguments[0].Value
    if ($row.EventID -like "esx.problem.*") {
        $row.Datastore = $event.Arguments[2].Value
        $row.Path = $event.Arguments[1].Value
    } elseif ($row.EventID -like "esx.clear.*") {
        $row.Datastore = $event.Arguments[1].Value
        $row.Path = $event.Arguments[2].Value
    } else {
        Write-Host "ERROR: Unexpected EventTypeID - " $row.EventID
    }
    $Results = $Results + $row
}
$Results | Format-Table *
$Results | Export-Csv -path $OutputFile -NoTypeInformation
</source>
=== ESX BIOS, NIC and HBA Driver Versions ===
Getting BIOS, NIC and HBA driver versions is dependant on what's reported to the ESX by the hardware vendor's CIM provider.  This script was written using various HP servers and may well only work for them.
Getting BIOS, NIC and HBA driver versions is dependant on what's reported to the ESX by the hardware vendor's CIM provider.  This script was written using various HP servers and may well only work for them.


Line 1,072: Line 879:
</source>
</source>


=== Storage ===
==== VM's with Datastores List ====
List of Virtual Machines, and their datastores (with usage)
<source lang="powershell">
$datastoreExp = @{N="Datastore"; E={ ($_ | get-datastore | select-object -first 1).Name }}
$diskSizeExp = @{N="Total Disk"; E={ ($_ | get-harddisk | measure-object -property CapacityKB -sum).Sum }}
get-vm | select Name, $datastoreExp, $diskSizeExp | sort -property Datastore,"Total Disk"
</source>
==== VM Storage Usage ====
'''Total''' storage usage (including any snapshots, logs, etc, etc), not just the VMDK files.
Adapted from post by Arnim van Lieshout http://www.van-lieshout.com/2009/07/how-big-is-my-vm/
<source lang="powershell">
function Get-VMDiskUsage($vm2do)
{
    #Initialize variables
    $VMDirs =@()
    $VMSize = 0
    $searchSpec = New-Object VMware.Vim.HostDatastoreBrowserSearchSpec
    $searchSpec.details = New-Object VMware.Vim.FileQueryFlags
    $searchSpec.details.fileSize = $TRUE
    Get-View -VIObject $vm2do | % {
        #Create an array with the vm's directories
        $VMDirs += $_.Config.Files.VmPathName.split("/")[0]
        $VMDirs += $_.Config.Files.SnapshotDirectory.split("/")[0]
        $VMDirs += $_.Config.Files.SuspendDirectory.split("/")[0]
        $VMDirs += $_.Config.Files.LogDirectory.split("/")[0]
        #Add directories of the vm's virtual disk files
        foreach ($disk in $_.Layout.Disk) {
            foreach ($diskfile in $disk.diskfile){
                $VMDirs += $diskfile.split("/")[0]
            }
        }
        #Only take unique array items
        $VMDirs = $VMDirs | Sort | Get-Unique
        foreach ($dir in $VMDirs){
            $ds = Get-Datastore ($dir.split("[")[1]).split("]")[0]
            $dsb = Get-View (($ds | get-view).Browser)
            $taskMoRef  = $dsb.SearchDatastoreSubFolders_Task($dir,$searchSpec)
            $task = Get-View $taskMoRef
            while($task.Info.State -eq "running" -or $task.Info.State -eq "queued"){$task = Get-View $taskMoRef }
            foreach ($result in $task.Info.Result){
                foreach ($file in $result.File){
                    $VMSize += $file.FileSize
                }
            }
        }
    }
    # VM disk usage in GB
    $VMSize/1048576000
}
Get-VMDiskUsage (Get-VM "MyVM")
</source>
==== ESX Storage Events ====
Creates a CSV of storage events (eg path up/downs).  Useful in order to be analyse storage events, for example if you need to tally up to SAN logs etc.  Note the <code> get-VIEvent </code> is limited to 1000 results, which will typically get 1 - 2 days worth depending on your infrastructure.  To get a longer history would  require multiple calls to <code> get-VIEvent </code> using the <code>-Start</code> and <code>-Finish</code> parameters
<source lang="Powershell">
$OutputFile = "ESX-EventsStorage.csv"
$Results = @()
$events = Get-VIEvent -MaxSamples 1000 | Where {$_.EventTypeID -like "esx.*.storage.*"}
foreach ($event in $events) {
    $row = "" | Select Date, Host, EventID, Device, Datastore, Path
    $row.Date = $event.CreatedTime
    $row.Host = $event.Host.Name.Split(".")[0]
    $row.EventID = $event.EventTypeID
    # Allow for Argument Keys being mixed up between "problem" aka fault start and "clear" aka fault end events
    $row.Device = $event.Arguments[0].Value
    if ($row.EventID -like "esx.problem.*") {
        $row.Datastore = $event.Arguments[2].Value
        $row.Path = $event.Arguments[1].Value
    } elseif ($row.EventID -like "esx.clear.*") {
        $row.Datastore = $event.Arguments[1].Value
        $row.Path = $event.Arguments[2].Value
    } else {
        Write-Host "ERROR: Unexpected EventTypeID - " $row.EventID
    }
    $Results = $Results + $row
}
$Results | Format-Table *
$Results | Export-Csv -path $OutputFile -NoTypeInformation
</source>
==== ESX Datastore to LUN Mapping (ESX3) ====
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>
==== ESX Datastore to LUN Mapping (ESX4) ====
Produces a similar output to the above, but works for ESX4 servers.
<source lang="powershell">
function Get-DS-LUNs-v4 ($objESXs) {
    foreach ($cluster in (Get-View -ViewType "ClusterComputeResource")) {
      $vmhostsview = $cluster.host | % { Get-View $_ }
      $vmhostview  = $vmhostsview | Select -first 1
      $ScsiLuns    = $vmhostsview | % { $_.Config.StorageDevice.ScsiLun } | Select -unique *
      $UUIDs      = $ScsiLuns | Select -unique UUID
      $Datastores  = $vmhostsview | % { $_.Config.FileSystemVolume.MountInfo } | % { $_.Volume } | Select -Unique *
      $HostLUN    = $vmhostsview | % { $_.Config.StorageDevice.ScsiTopology.Adapter } | % { $_.Target | % { $_.LUN } } | Select -unique *
      foreach ($UUID in $UUIDs) {
        $Lun = $ScsiLuns | ? { $_.UUID -eq $UUID.UUID } | Select -first 1
        $objVolume              = "" | Select Datastore, Make, Model, LUN
        $objVolume.LUN      = ($HostLUN | ? { $_.ScsiLun -eq $Lun.Key } | select -unique LUN).LUN
        $objVolume.Make        = $Lun.Vendor
        $objVolume.Model        = $Lun.Model
        foreach ($vol in $Datastores) {
          if ($vol.extent | % { $_.diskname -eq $Lun.CanonicalName}) {
            $objVolume.Datastore  = $vol.Name
          }
        }
        $objVolume
      }
    }
}
</source>
[[Category:VMware]]
[[Category:VMware]]
[[Category:PowerShell]]
[[Category:PowerShell]]

Navigation menu