Skip to main content

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
}

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

Post a Comment

Popular posts from this blog

Excel document for pinging list of computers VBA

Here is an excel document I created that will ping a list of nodes in column A and give results in column B. There are much better tools that can be used such as angry ip scanner  http://www.angryip.org/w/Home . I just wrote this as an example Requirements: Tested with Office 2010 Download: Download

Powershell - Com+ Application Recycle

Needed a script to recycle a com+ application nightly and this is what i came up with. This script will write each recycle it does to the event log under application. Run locally or via a scheduled task. #Recycle COM+ Application and write to the event log the status # 1.0 Release # Run script locally # Write to the event log ######################################## #Configurable ######################################## #Com+ ApplicationName $ComPlusLikeAppName = "Put the name of Com+ Application here a like statement is used to eval so you can get away with putting part of it" #EventLog to write to. $eventlog = "Application" #Source for eventlog. $source = "RecycleComObject" #Successful Event ID $SEventID = 0 #Error Event ID $EEventID = 666 #Process that COM+ runs under $process = "dllhost.exe". ######################################## #Clear $CurrentMemory = $null $PRocessID = $null $Commandline = $null $GUID = $null $AppID = $null $Messag

Powershell : Certutil Find Expired Certs on CA server

Wrote this to get certificate expiration information for certificates that expired 5 days ago to ones that expire in 90 days. Wrap an invoke-command around this for remote query. $Before = (get-date).adddays(90).ToString("MM/dd/yyyy") $After = (get-date).AddDays(-5).ToString("MM/dd/yyyy") <# https://blogs.technet.microsoft.com/poshchap/2016/01/01/powershell-and-certutil-exe/ We create a date range with $Before, i.e. certificates expiring before this date, and $After, i.e. certificates expiring after this date. These values are converted into something that certutil can understand - $Restrict. This is then used with the certutil -restrict parameter. #> $Restrict = "NotAfter<=$Before,NotAfter>=$After" $Report = @() $cmd = & certutil.exe -view -restrict $Restrict -out "RequesterName,CommonName,Certificate Expiration Date","Certificate Template" $SplitLines = $cmd.Split("`n`r") $Index = 0 foreach ($line in $Sp