Thursday, February 21, 2013

Powershell - Find VM raw disk path to netapp lun


Purpose:
I needed a way get raw disk mappings from a vmware vm to the lun it is using on a netapp filer. One thing that needs to be edited is the SearchFilers function. Just add your runtime names and what filers they correspond to.Run this from powercli. and this requires the dataontap module which you can get from your now.netapp.com site Note: this script could be improved by gather all lun information from each filer instead of what i did the in the search filer function

              
param([string]$VC)
Import-Module DataONTAP
#########################
####Tony Unger
#########################


#################Functions##############################################
function GetNetappPath([string]$VMHost,[string]$VMHDLunID,[string]$RuntimeName) {
$stor = get-view (Get-VMHostStorage -VMHost $VMHost)
$IscsiWWN = $stor.StorageDeviceInfo.HostBusAdapter | where {$_.GetType().Name -eq "HostInternetScsiHba"} | Select -First 1 -expandproperty IScsiName
Write-Host "Found ISCSI NAME: $IscsiWWN on Host $VMHost"
$c = SearchFilers $IscsiWWN $RuntimeName
Write-Host "Netapp path: $c"
return $c
}

function SearchFilers([string]$IscsiWWN,[string]$RuntimeName){

switch -wildcard ($RuntimeName) 
    { 
       #Add all you runtime names here with what filer
        "vmhba40:C0:T0*" {$NetappFiler = "x.x.x.x"} 
        
        default {Write-host "Error! determining filer - $RuntimeName not found";read-host}
    }
 
 Write-host "Connecting to $NetappFiler"
connect-nacontroller $NetappFiler | out-null

$Igroup = get-nalun | Get-Nalunmap | Select Name,Initiators | Where {$_.Initiators -like $IscsiWWN} | Select -First 1 -expandproperty Name
$a = get-nalunbyigroup $Igroup $VMHDLunID | Select -ExpandProperty Path
Write-Host "SearchFilers function: $a"
$a = "$NetappFiler$a"
 return $a
 }
 
#################Functions##############################################
########################
$PathtoCSV = "C:\temp\VMlunID.csv"
########################
If ($VC){$strVC = $VC}
ELSE{$strVC = Read-Host "What is the Vcenter hostname?"}
If (!$strVC){Write-Host "Error: Vcenter not entered";exit}
Connect-VIServer -Server $strVC  #Enter your vCenter Server
"Hostname,DiskID,CapacityG,HD Path,ScsiCanonicalName,LunID,ESX Host,Netapp Path" > $PathtoCSV
Write-Host "Getting VM Information from Vcenter. This can take awhile based on how many VM you have"
$Disks = Get-VM | Get-HardDisk | Where {"RawPhysical","RawVirtual" -contains $_.DiskType}
Write-Host "Completed getting VM Information from Vcenter"
Write-Host ""
Write-Host "Querying SCSI Information for Vcenter"
#Adding all hosts Canonicalnames and runtimes names to a hash table 
#thanks http://communities.vmware.com/people/LucD
$lunTab = @{}
Get-VMHost  | Get-ScsiLun | %{
  $lunTab.Add($_.VMHost.Name + $_.CanonicalName, $_.RuntimeName)
}
Write-Host "Completed SCSI Information for Vcenter"

Foreach ($Disk in $Disks) {
$VMName = $Disk.Parent
write-host "VM Name: $VMName"
$VMHDname = $Disk.Name
write-host "Hard drive ID: $VMHDname"
$VMCapacityGB = $Disk.capacityGB
write-host "Lun Capacity(GB): $VMCapacityGB"
$VMHDPath = $Disk.filename
write-host "RAW VMDK Path: $VMHDPath"
$VMScsiCanonicalName = $Disk.ScsiCanonicalName
write-host "$ScsiCanonicalName: $VMScsiCanonicalName"
$VMHost = Get-VM $VMName | Select -ExpandProperty VMHost
write-host "ESX Host: $VMHost"
$RuntimeName = $lunTab[$VMHost.Name + $Disk.SCSICanonicalName]
write-host "RuntimeName: $RuntimeName"
$VMHDLunID = $RuntimeName.Substring($RuntimeName.LastIndexof(“L”)+1)
Write-Host "LunID: $VMHDLunID"
$NetappPath = GetNetappPath $VMHost $VMHDLunID $RuntimeName
Write-host "PathtoNetapp: $NetappPath"
$Combine = $VMName,$VMHDname,$VMCapacityGB,$VMHDPath,$VMScsiCanonicalName,$VMHDLunID,$VMHost,$NetappPath
Write-Host "Writing to $PathtoCSV"
$Combine -join "," >> $PathtoCSV
}

3 comments:

  1. Hi Tony,
    I've tried this script. But get "Error! determining filer" error. Where am I wrong. And LunPath / NetappPath empty.
    Thank You.

    ReplyDelete
    Replies
    1. #Add all you runtime names here with what filer
      "vmhba40:C0:T0*" {$NetappFiler = "x.x.x.x"}

      In the script you have to add all the runtime names of your filers.

      Delete
  2. Hi Tony,
    I've tried this script. But get "Error!

    ReplyDelete