Posted in Computers and Internet

PowerShell: List iSCSI and Fiber Channel LUNs on a Windows Server

I wrote this script to collect LUNs information (mostly Size and Free Space) on our SQL and Exchange servers.

If we assign the LUN from Storage system as NTFS mount point, you can’t really see the Total size and Free disk space in GUI (File Explorer). So I wrote this script to collect and monitor (with different script) about Free Space inside the LUN.

Download this script HERE.

    Script Name: Get-LunInfo.ps1
    Purpose: Get iSCSI and Fiber Channel LUNs info

    Writen By: Anand, the Awesome, Venkatachalapathy

    How to Run: Just run the script in PowerShell, no parameters required.

#Empty Array
$LUNi = @()

#Get the Logical Disks using WMI query
Get-WMIObject Win32_LogicalDisk | Foreach-Object {

  $diskinfo = Get-WmiObject -Query `
    “Associators of {Win32_LogicalDisk.DeviceID=’$($_.DeviceID)’} WHERE ResultRole=Antecedent”
  #Collect the required info in variables
  $size = “{0:f2}” -f (($_.Size)/1GB)
  $freespace = “{0:f2}” -f (($_.freespace)/1GB)
  $VolName = $_.VolumeName
  $DeviceId = $_.DeviceID
  $Name = $diskinfo.Name
  #Create Hash Table
  $LUNs = @{“DiskName” = “$DeviceID”;”Name” = “$Name”;
  “VolumeName” = “$VolName”;”Size”=”$size”;
  “FreeSpace”=”$freespace”  }
  #Assign the hash table to the Array
  $LUNi += $LUNs

#Store the info in CSV file
“Disk Name`tDevice ID`tVolume Name`tSize in GB`tFree Space in GB” | `
       Out-File -FilePath .\LunInfo.csv

foreach ($LUN in $LUNi)
    $LUN.DiskName + “`t” + $LUN.Name + “`t” + $LUN.DiskName + “`t” + `
    $LUN.VolumeName + “`t” + $LUN.Size + “`t” + $LUN.FreeSpace  | `
    Out-File -FilePath .\LunInfo.csv -Append

#Display the results in a Table
Import-Csv -Path .\LunInfo.csv  -Delimiter “`t” | ft -AutoSize

#Display the results in Grid view (remove this if you don’t want
#this view
Import-Csv -Path .\LunInfo.csv  -Delimiter “`t” | Out-GridView

                   * * * The End * * *


Being in Silicon Valley for years, I became a IT Infrastructure Geek by experience and surrounded by other Geeks everywhere. I try to help others by the solutions I found on odd occasions.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s