1 2 Previous Next 22 Replies Latest reply on Jan 20, 2020 9:34 AM by LucD

    PowerCLI script to lists Windows Drive letter and VM Disk number in Data center ?

    AlbertWT Master

      Hi All,

       

      Can anyone here please assist me with the powershell script if possible to list the VM information like the following format or in CSV format is OK:

       

      SCSI (0:0) VM Hard Disk 1 [R5D23] PRODFSCLUS01-VM/PRODFSCLUS01-VM.vmdk - Windows C: drive - 60 GB

      SCSI (1:1) VM Hard Disk 2 [NAS-D76] PRODFSCLUS01-VM/PRODFSCLUS01-VM.vmdk - Windows Q: drive - 1 GB

      SCSI (1:2) VM Hard Disk 3 [NAS-D23] PRODFSCLUS01-VM/PRODFSCLUS01-VM.vmdk - Windows G: drive - 1.77 TB

      SCSI (1:3) VM Hard Disk 4 [R5D52] PRODFSCLUS01-VM/PRODFSCLUS01-VM.vmdk - Windows H: drive - 1.77 GB

      SCSI (1:4) VM Hard Disk 5 [NAS-D19] PRODFSCLUS01-VM/PRODFSCLUS01-VM.vmdk - Windows T: drive - 60 GB

      Thanks in advance.

        • 1. Re: PowerCLI script to lists Windows Drive letter and VM Disk number in Data center ?
          Prakash Enthusiast

          The below pieces would help to grab the info

           

          $VM = Get-VM

          $VM.HardDisks | Select Name,Filename

           

          $vmg = $VM | Get-VMGuest

          $vmg.Disks | select Path,CapacityGB,FreeGB

           

          • 2. Re: PowerCLI script to lists Windows Drive letter and VM Disk number in Data center ?
            LucD Guru
            User ModeratorsvExpertCommunity Warriors

            If you don't have more than one guest OS partition on a VMDK, you can use something like this

             

            $compName = 'MyVM'

             

            $volTab = @{}

             

            foreach($disk in (Get-CimInstance -ComputerName $compName -ClassName Win32_DiskDrive)){

                foreach($partition in (Get-CimAssociatedInstance -InputObject $disk -ResultClassName Win32_DiskPartition)){

                    Get-CimAssociatedInstance -InputObject $partition -ResultClassName Win32_LogicalDisk | %{

                        $volTab.Add("$($disk.SCSIBus):$($disk.SCSITargetId)",($_.DeviceID,$_.VolumeName -join '|'))

                    }

                }

            }

             

            $vm = Get-VM -Name $compName

             

            foreach($ctrl in Get-ScsiController -VM $vm){

                foreach($disk in (Get-HardDisk -VM $vm | where{$_.ExtensionData.ControllerKey -eq $ctrl.Key})){

                    $obj = [ordered]@{

                        VM = $vm.name

                        HD = $disk.Name

                        VMDK = $disk.Filename

                        Device = "$($ctrl.ExtensionData.BusNumber):$($disk.ExtensionData.UnitNumber)"

                        Drive = ""

                        Label = ""

                    }

                    if($volTab.ContainsKey("$($ctrl.ExtensionData.BusNumber):$($disk.ExtensionData.UnitNumber)")){

                        $obj.Drive = $volTab["$($ctrl.ExtensionData.BusNumber):$($disk.ExtensionData.UnitNumber)"].Split('|')[0]

                        $obj.Label = $volTab["$($ctrl.ExtensionData.BusNumber):$($disk.ExtensionData.UnitNumber)"].Split('|')[1]

                    }

                    New-Object -TypeName PSObject -Property $obj

                }

            }

             

            Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
            • 3. Re: PowerCLI script to lists Windows Drive letter and VM Disk number in Data center ?
              AlbertWT Master

              yes that's the one almost. .

               

              Somehow the Drive letter and label is still not populated ?

              • 4. Re: PowerCLI script to lists Windows Drive letter and VM Disk number in Data center ?
                AlbertWT Master

                I've tried using:

                 

                
                
                #Example snippet to tie a Windows Volume to a VMware 
                
                
                #Define computername
                    $computer = "PRODFS03-VM"
                
                
                #Change this as needed.  Our standard display name is the hostname followed by a space...
                    $VMView = Get-View -ViewType VirtualMachine -Filter @{'Name' = "$computer "}
                
                
                #Thanks go to Richard Siddaway for the basic queries to tie diskdrive>partition>logicaldisk.
                #http://itknowledgeexchange.techtarget.com/powershell/mapping-physical-drives-to-logical-drives-part-3/
                    $ServerDiskToVolume = @(
                        Get-WmiObject -Class Win32_DiskDrive -ComputerName $computer | foreach {
                        
                            $Dsk = $_
                            $query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='$($_.DeviceID)'} WHERE ResultClass=Win32_DiskPartition" 
                        
                            Get-WmiObject -Query $query -ComputerName $computer | foreach { 
                            
                                $query = "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='$($_.DeviceID)'} WHERE ResultClass=Win32_LogicalDisk" 
                            
                                Get-WmiObject -Query $query -ComputerName $computer | Select DeviceID,
                                    VolumeName,
                                    @{ label = "SCSITarget"; expression = {$dsk.SCSITargetId} },
                                    @{ label = "SCSIBus"; expression = {$dsk.SCSIBus} }
                            }
                        }
                    )
                
                
                
                
                # Now loop thru all the SCSI controllers on the VM and find those that match the Controller and Target
                    $VMDisks = 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}))
                        {
                
                
                            #Build a custom object to hold this.  We use PS3 language...
                            [pscustomobject]@{
                                VM = $VM.Name
                                HostName = $VMView.Guest.HostName
                                PowerState = $VM.PowerState
                                DiskFile = $VirtualDiskDevice.Backing.FileName
                                DiskName = $VirtualDiskDevice.DeviceInfo.Label
                                DiskSize = $VirtualDiskDevice.CapacityInKB * 1KB
                                SCSIController = $VirtualSCSIController.BusNumber
                                SCSITarget = $VirtualDiskDevice.UnitNumber
                                DeviceID = $null
                            }
                        
                            #Match up this VM to a logical disk
                                $MatchingDisk = @( $ServerDiskToVolume | Where {$_.SCSITarget -like $VMSummary.SCSITarget -and $_.SCSIBus -like $VMSummary.SCSIController} )
                            
                            #Shouldn't happen, but just in case..
                                if($MatchingDisk.count -gt 1)
                                {
                                    Write-Error "too many matches: $($MatchingDisk | select -property deviceid, partitions, SCSI* | out-string)"
                                    $VMSummary.DeviceID = "Error: Too Many"
                                }
                                elseif($MatchingDisk.count -eq 1)
                                {
                                    $VMSummary.DeviceID = $MatchingDisk.DeviceID
                                }
                                else
                                {
                                    Write-Error "no match found"
                                    $VMSummary.DeviceID = "Error: None found"
                                }
                
                
                            $VMSummary
                        }
                    }
                

                 

                 

                but it is not returning any result ?

                • 5. Re: PowerCLI script to lists Windows Drive letter and VM Disk number in Data center ?
                  LucD Guru
                  User ModeratorsvExpertCommunity Warriors

                  That is in fact the script I replied to earlier, but that thread seems to have disappeared overnight

                   

                  The script you mention is the pre-PSv3 way of doing it, in PSv3 the Get-CimAssociatedInstance  cmdlet was introdcued, which avoids these awful ASSOCIATORS queries.

                   

                  If the drive letter and label are not there, that would mean that the WMI queries don't find the info.

                  What does this show for that specific VM ?

                   

                  $compName = 'MyVM'

                   

                  $volTab = @{}

                   

                  foreach($disk in (Get-CimInstance -ComputerName $compName -ClassName Win32_DiskDrive)){

                      foreach($partition in (Get-CimAssociatedInstance -InputObject $disk -ResultClassName Win32_DiskPartition)){

                          Get-CimAssociatedInstance -InputObject $partition -ResultClassName Win32_LogicalDisk | %{

                              $volTab.Add("$($disk.SCSIBus):$($disk.SCSITargetId)",($_.DeviceID,$_.VolumeName -join '|'))

                          }

                      }

                  }

                  $volTab.GetEnumerator()

                   

                  Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                  • 6. Re: PowerCLI script to lists Windows Drive letter and VM Disk number in Data center ?
                    AlbertWT Master

                    LucD it returns as follows:

                     

                    Exception calling "Add" with "2" argument(s): "Item has already been added. Key in dictionary: '0:0'  Key being added: '0:0'"
                    At C:\Users\Admin\AppData\Local\Temp\b19a36b6-f1ed-4bff-bc63-f411d95c083d.ps1:8 char:45
                    + ...            $volTab.Add("$($disk.SCSIBus):$($disk.SCSITargetId)",($_.D ...
                    +                                                ~~~~~~~~~~~~~~~~~~
                        + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
                        + FullyQualifiedErrorId : ArgumentException
                    
                    
                    
                    Name                           Value                                                                                                                                                                                                   
                    ----                           -----                                                                                                                                                                                                   
                    0:0                            C:|C-PRODFS03-VM     
                    
                    • 7. Re: PowerCLI script to lists Windows Drive letter and VM Disk number in Data center ?
                      LucD Guru
                      Community WarriorsvExpertUser Moderators

                      That seems to indicate that you have duplicate bus:scsiid entries on a VM, which shouldn't be possible.

                      What does this produce for that VM ?

                       

                      $compName = 'MyVM'

                       

                      foreach($disk in (Get-CimInstance -ComputerName $compName -ClassName Win32_DiskDrive)){

                          foreach($partition in (Get-CimAssociatedInstance -InputObject $disk -ResultClassName Win32_DiskPartition)){

                              Get-CimAssociatedInstance -InputObject $partition -ResultClassName Win32_LogicalDisk | %{

                                  Write-Output "$($disk.SCSIBus):$($disk.SCSITargetId) $($_.DeviceID) $($_.VolumeName)"

                              }

                          }

                      }

                       

                      Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                      • 8. Re: PowerCLI script to lists Windows Drive letter and VM Disk number in Data center ?
                        AlbertWT Master

                        LucD the result is:

                         

                        0:0 C: C-PRODFS03-VM

                        0:0 E: E-PRODFS03-VM

                         

                        from the vSphere console I can see that the E: drive is on SCSI 1:0 not 0:0

                         

                        same thing with the other VM:

                         

                        0:0 C: C-PRODPXE01-VM

                        0:0 D: D-OS IMAGES

                         

                        All returns 0:0

                        • 9. Re: PowerCLI script to lists Windows Drive letter and VM Disk number in Data center ?
                          LucD Guru
                          vExpertUser ModeratorsCommunity Warriors

                          Can you check this variation ?

                           

                          $compName = 'MyVM'

                           

                          foreach($disk in (Get-CimInstance -ComputerName $compName -ClassName Win32_DiskDrive)){

                              foreach($partition in (Get-CimAssociatedInstance -InputObject $disk -ResultClassName Win32_DiskPartition)){

                                  Get-CimAssociatedInstance -InputObject $partition -ResultClassName Win32_LogicalDisk | %{

                                      Write-Output "$($disk.SCSIPort-2):$($disk.SCSITargetId) $($_.DeviceID) $($_.VolumeName)"

                                  }

                              }

                          }

                           

                          Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                          • 10. Re: PowerCLI script to lists Windows Drive letter and VM Disk number in Data center ?
                            AlbertWT Master

                            LucD Yes it is getting better Luc,

                             

                            The result now shows the SCSI ID better:

                            0:0 C: C-PRODFS03-VM

                            1:0 D: D-PRODFS03-VM

                             

                            Previously the D: drive is the same 0:0

                            • 11. Re: PowerCLI script to lists Windows Drive letter and VM Disk number in Data center ?
                              AlbertWT Master

                              LucD this is the script that I've found somehwere in this forum:

                               

                              $report = @()
                              foreach($vm in Get-VM -Name "PRODFS03-VM","PRODSQLDB02-VM" ){
                                  Get-HardDisk -VM $vm | ForEach-Object {
                                      $HardDisk = $_
                                      $row = "" | Select Hostname, VM, GuestName, Datastore, VMXpath, HardDisk, DiskType, CapacityGB, DiskUsage
                                                  $row.Hostname = $vm.Host.Name
                                                  $row.VM = $VM.Name
                                                  $row.GuestName = $vm.Guest.HostName
                                                  $row.Datastore = $HardDisk.Filename.Split("]")[0].TrimStart("[")
                                                  $row.VMXpath = $HardDisk.FileName
                                                  $row.HardDisk = $HardDisk.Name
                                                  $row.CapacityGB = ("{0:f1}" -f ($HardDisk.CapacityKB/1MB))
                                $row.DiskType = $HardDisk.get_DiskType()
                                $row.DiskUsage = $vm.get_UsedSpaceGB()
                                                  $report += $row
                                 }
                                
                              }
                              $report | ft -AutoSize
                              
                              

                               

                              This is the result from the script above:

                              What missing is the drive letter column that I'm interested to know if it is possible.

                              Result.JPG

                               

                              
                              

                               

                              Thanks so far for the assistance.

                              • 12. Re: PowerCLI script to lists Windows Drive letter and VM Disk number in Data center ?
                                LucD Guru
                                vExpertUser ModeratorsCommunity Warriors

                                Did you already combine that with the last script I gave in this thread ?

                                Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                                • 13. Re: PowerCLI script to lists Windows Drive letter and VM Disk number in Data center ?
                                  AlbertWT Master

                                  LucD ah I'm not sure how to combine it with your script hence I was just asking if it is possible

                                  • 14. Re: PowerCLI script to lists Windows Drive letter and VM Disk number in Data center ?
                                    de2rfg Enthusiast

                                    I'm also having problems to find or write a script that reports the VMDK, SCSI ID, Windows drive letters. The best I could find is the following;

                                     

                                    VMware: matching guest drives to .vmdk files? - Ars Technica OpenForum

                                     

                                    It works nicely as long as the VM has only a one SCSI controller. Any idea to modify the script to work with multipe SCSI controller?

                                     

                                    VM       HostName              DiskFile                                      DiskName       DiskSize SCSIController SCSITarget DeviceID

                                    --       --------              --------                                      --------       -------- -------------- ---------- --------

                                    xxxx2019 xxxx2019      [xx_Tiering_LUN038] xxxx2019/xxxx2019.vmdk    Hard disk 1 42949672960              0          0 {D:, C:}

                                    xxxx2019 xxxx2019      [xx_Tiering_LUN038] xxxx2019/xxxx2019_1.vmdk Hard disk 2 21474836480              1          0       

                                    xxxx2019 xxxx2019      [xx_Tiering_LUN038] xxxx2019/xxxx2019_2.vmdk Hard disk 3 75161927680              1          1       

                                     

                                    1 2 Previous Next