PowerCLI to get information about RDM connected to VM.

In my work environment when we use MS SQL, so we have to build failover clusters.  This means the use of RDM (Raw Device Mappings) LUNs.  So you have to connect the RDM LUNs to the first system and them connected them to the second system using the disk file names.  This is not a big deal, but it is nice to speed this process of mapping the drive on the second system, and cut and paste is your friend.

$VM = "VMNameHere"
Get-vm $VM | Get-HardDisk | Where {$_.Disktype -eq "RawPhysical"} | Select CapacityGB, Filename
Sample output from basic script
Sample output from basic script

This will get you the basic information that is needed to map the drives on the second node of the cluster, but it would be nice to get some additional information.

$VM = "VMNameHere"
$VmView = Get-View -ViewType VirtualMachine -Filter @{"Name" = $VM}
foreach ($VirtualSCSIController in ($VMView.Config.Hardware.Device | where {$_.DeviceInfo.Label -match "SCSI Controller"})) {
            foreach ($VirtualDiskDevice in ($VMView.Config.Hardware.Device | where {$_.ControllerKey -eq $VirtualSCSIController.Key})) {
                Get-VM $VM | Select Name, @{N="DiskName";E={$VirtualDiskDevice.DeviceInfo.Label}}, @{N="CanonicalName";E={(Get-VM $VM | Get-Harddisk -Name ($VirtualDiskDevice.DeviceInfo.Label)).ScsiCanonicalName}}, @{N="SCSI_Id";E={"$($VirtualSCSIController.BusNumber) : $($VirtualDiskDevice.UnitNumber)"}}, @{N="DiskFile";E={$VirtualDiskDevice.Backing.FileName}}, @{N="DiskSize(GB)";E={$VirtualDiskDevice.CapacityInKB * 1KB / 1GB}}
            }
}

 

Sample output of script
Sample output of script

With this it will get the name of the server you are working with, the disk file name, SCSI canonical name, SCSI ID, disk file name, and the disk size.  With this output, you can easily cut and paste the disk file name into the second node without having to browse the datastore to find the VMDK pointer file to attach.  Also have the SCSI node ID so you make make sure that they match on both nodes.

-Stuart

8 thoughts on “PowerCLI to get information about RDM connected to VM.”

    1. Hello Jay,
      I glad that you found this useful. Below is the modified script that will output the results to a CSV file.

      $report = @()
      $VMs = @("VM_Name_01","VM_Name_02","VM_Name_03")
      
      Foreach ($VM in $VMs){
          $LUN = ""
          $VmView = Get-View -ViewType VirtualMachine -Filter @{"Name" = $VM}
              foreach ($VirtualSCSIController in ($VMView.Config.Hardware.Device | where {$_.DeviceInfo.Label -match "SCSI Controller"})) {
                      foreach ($VirtualDiskDevice in ($VMView.Config.Hardware.Device | where {$_.ControllerKey -eq $VirtualSCSIController.Key})) {
                          $VMinfo               = Get-VM $VM
                          $LUN                  = Get-Scsilun ($VMinfo | Get-Harddisk -Name ($VirtualDiskDevice.DeviceInfo.Label)).ScsiCanonicalName -VmHost ($VMinfo).VMHost
                          $line                 = "" | Select 'Name', 'ClusterName', 'DiskName', 'LUNID','LUN_ID', 'CanonicalName', 'SCSI_ID', 'DiskFile', 'DiskSize(GB)'
                          $line.'Name'          = $VMinfo.name 
                          $line.'ClusterName'   = $VMInfo.VMhost.Parent
                          $line.'DiskName'      = $VirtualDiskDevice.DeviceInfo.Label
                          $line.'LUN_ID'        = $LUN.RuntimeName.Substring($LUN.RuntimeName.LastIndexOf("L")+1)
                          $line.'CanonicalName' = ($VMinfo | Get-Harddisk -Name ($VirtualDiskDevice.DeviceInfo.Label)).ScsiCanonicalName
                          $line.'SCSI_ID'       = "$($VirtualSCSIController.BusNumber) : $($VirtualDiskDevice.UnitNumber)"
                          $line.'DiskFile'      = $VirtualDiskDevice.Backing.FileName
                          $line.'DiskSize(GB)'  = ($VirtualDiskDevice.CapacityInKB * 1KB / 1GB)
                          $Report              += $line
                      }
              }
      }
      $report | Export-csv C:\scripts\logs\RDMs_3-12-2016.csv -NoTypeInformation -UseCulture
      
      1. Hi Stuart,

        Thank you for the script. I tried this script by adding &{ } and I was able to import the output to csv. My question is how if i want to get details of multiple servers from list using Get-Content serverlist.txt

        $VM = "VMName"
        $VmView = Get-View -ViewType VirtualMachine -Filter @{"Name" = $VM}
            {foreach ($VirtualSCSIController in ($VMView.Config.Hardware.Device | where {$_.DeviceInfo.Label -match "SCSI Controller"})) {
                    foreach ($VirtualDiskDevice in ($VMView.Config.Hardware.Device | where {$_.ControllerKey -eq $VirtualSCSIController.Key})) {
                        Get-VM $VM | Select Name, @{N="DiskName";E={$VirtualDiskDevice.DeviceInfo.Label}}, @{N="CanonicalName";E={(Get-VM $VM | Get-Harddisk -Name ($VirtualDiskDevice.DeviceInfo.Label)).ScsiCanonicalName}}, @{N="SCSI_Id";E={"$($VirtualSCSIController.BusNumber) : $($VirtualDiskDevice.UnitNumber)"}}, @{N="DiskFile";E={$VirtualDiskDevice.Backing.FileName}}, @{N="DiskSize(GB)";E={$VirtualDiskDevice.CapacityInKB * 1KB / 1GB}}
                    }
        }} | Export-Csv -Path C:\tmp\storage.csv -NoTypeInformation
        
        1. Update i manage to solve it getting from server list. New question is how can we include the VMs advance settings to get the scsiID settings if they are ctkenabled and sharing as multiwriter. Thanks

          $report = @()
          $server = Get-Content servers.txt
          
          $VMs = @("$server")
           
          Foreach ($VM in $VMs){
              #$LUN = ""
              $VmView = Get-View -ViewType VirtualMachine -Filter @{"Name" = $VM}
                  foreach ($VirtualSCSIController in ($VMView.Config.Hardware.Device | where {$_.DeviceInfo.Label -match "SCSI Controller"})) {
                          foreach ($VirtualDiskDevice in ($VMView.Config.Hardware.Device | where {$_.ControllerKey -eq $VirtualSCSIController.Key})) {
                              $VMinfo               = Get-VM $VM
                              $line                 = "" | Select 'Name', 'ClusterName', 'DiskName', 'SCSI_ID', 'DiskFile', 'DiskSize(GB)'
                              $line.'Name'          = $VMinfo.name 
                              $line.'ClusterName'   = $VMInfo.VMhost.Parent
                              $line.'DiskName'      = $VirtualDiskDevice.DeviceInfo.Label
                              $line.'SCSI_ID'       = "$($VirtualSCSIController.BusNumber) : $($VirtualDiskDevice.UnitNumber)"
                              $line.'DiskFile'      = $VirtualDiskDevice.Backing.FileName
                              $line.'DiskSize(GB)'  = ($VirtualDiskDevice.CapacityInKB * 1KB / 1GB)
                              $Report              += $line
                          }
                  }
          }
          $report | Export-csv C:\tmp\SD.csv -NoTypeInformation -UseCulture
          
          1. Just change the lines:

            $server = Get-Content servers.txt
            
            $VMs = @(“$server”)
            

            to the following:

            #$server = Get-Content servers.txt
            
            $VMs = Get-content C:\locationofthefile\servers.txt
            
  1. Unfortunately, The canonical name output fails so the script doesn’t show you the output of the lun Id as consequence.
    how can it be fix?

    1. Pepe,
      Sorry to hear that this script isn’t working for you. Would you provide the error message from the script?

      -Stuart

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.