1 2 3 Previous Next 36 Replies Latest reply on May 3, 2020 4:05 AM by LucD Go to original post
      • 15. Re: PowerCLI script to lists Windows Drive letter and VM Disk number in Data center ?
        de2rfg Enthusiast

        I've now a solution that returns data for VM's with two SCSI Controller. Funny thing is that for at least one VM I still get wrong results. This VM has 4 controllers, but only 3 have attached disks. This seems to confuse the scrip.Not sure which other VM's might have false results.

         

         

        VM:

        HD1     SCSI  0 0

        HD2     SCSI 1 0

        HD3     SCSI 3 0  (note: not 2 0)

         

         

         

        $ServerDiskToVolume: SCSI Bus is wrong for D drive. It should be 0 3.

         

        DeviceID                                            VolumeName                                                                                  SCSITarget                                            SCSIBus

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

        D:                                                  xxxx_D                                                                                           0                                                  2

        E:                                                  xxxx_E                                                                                           0                                                  1

        C:                                                  xxxx_C                                                                                           0                                                  0

         

        Thus in the script result D:is missing as Device ID.

         

        VM       HostName           DiskFile                                               DiskName         DiskSize SCSIController SCSITarget DeviceID

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

        xxxx xxxx [xxxx_LUN048] xxxx/xxxx.vmdk   Hard disk 1   53687091200              0          0 C:     

        xxxx xxxx [xxxx_LUN048] xxxx/xxxx_1.vmdk Hard disk 2 1503238553600              1          0 E:     

        xxxx xxxx [xxxx_LUN048] xxxx/xxxx_3.vmdk Hard disk 3 1099511627776              3          0        

         

        #Example snippet to tie a Windows Volume to a VMware

        clear

        $cred = Get-Credential -Credential "xxxx"

         

        #Define computername

        $computers = "xxxx"

         

         

        foreach ($computer in $computers) {

        Write $computer

        #Change this as needed. Our standard display name is the hostname followed by a space...

        $VMView = Get-VM -Name $computer | Get-View

         

        $session = New-CimSession –ComputerName $computer -Credential $cred

         

        $ServerDiskToVolume = @(

        foreach($disk in (Get-CimInstance -CimSession $session -ClassName Win32_DiskDrive)){

            #Write-Output "Disk: " $disk

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

                #Write-Output "Partition: " $partition

         

          Get-CimAssociatedInstance -CimSession $session -InputObject $partition -ResultClassName Win32_LogicalDisk | select DeviceID, VolumeName,

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

          @{ label = "SCSITarget"; expression = {$disk.SCSITargetId} },

          @{ label = "SCSIBus"; expression = {$disk.SCSIPort-2} }

                }

            }

         

        Remove-CimSession -Name  *

        )

         

         

        <# Commented out for later use.

        $ServerDiskToVolume = @(

          Get-WmiObject -Credential $cred -Class Win32_DiskDrive -ComputerName $computer | foreach {

         

         

          $Dsk = $_

          $query = "ASSOCIATORS OF {Win32_DiskDrive.DeviceID='$($_.DeviceID)'} WHERE ResultClass=Win32_DiskPartition"

         

         

          Get-WmiObject -Credential $cred -Query $query -ComputerName $computer | foreach {

         

         

          $query = "ASSOCIATORS OF {Win32_DiskPartition.DeviceID='$($_.DeviceID)'} WHERE ResultClass=Win32_LogicalDisk"

         

         

          Get-WmiObject -Credential $cred -Query $query -ComputerName $computer | Select DeviceID,VolumeName,

          @{ label = "SCSITarget"; expression = {$dsk.SCSITargetId} },

          @{ label = "SCSIBus"; expression = {$dsk.SCSIPort-2} }

          }

          }

        )

        #>

         

         

        Write-Output $ServerDiskToVolume

         

         

        # 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})) {

          #Match up this VM to a logical disk

          $MatchingDisk = @( $ServerDiskToVolume | Where {$_.SCSITarget -eq $VirtualDiskDevice.UnitNumber -and $_.SCSIBus -eq $VirtualSCSIController.BusNumber} )

          #Build a custom object to hold this. We use PS3 language...

          [pscustomobject]@{

          VM = $VMView.Name

          HostName = $VMView.Guest.HostName

          DiskFile = $VirtualDiskDevice.Backing.FileName

          DiskName = $VirtualDiskDevice.DeviceInfo.Label

          DiskSize = $VirtualDiskDevice.CapacityInKB * 1KB

          SCSIController = $VirtualSCSIController.BusNumber

          SCSITarget = $VirtualDiskDevice.UnitNumber

          DeviceID = $MatchingDisk.DeviceID

          }

         

         

          }

        }

        $VMDisks | ft -AutoSize #| out-file -append c:\Users\rfg\vm_disk.txt

         

        }

        • 16. Re: PowerCLI script to lists Windows Drive letter and VM Disk number in Data center ?
          FMON Novice

          Look at something I posed a couple days ago:  How to trace a VM disk to a guest disk.

           

          That should help you associate VMware disk to OS disk - should work any number of scsi adapters.  The methodology should work for any OS although the script I provided is for Windows.

           

          That script does not address your requirement for needing to correlate drive letters to a disk.  But I do have some stuff that will do that in Windows as long as psremoting is enabled on your guest (drive letters don't need psremoting, but mountpoints do).  If you need help with that then post a new question and I'll try to assist this evening.

          • 17. Re: PowerCLI script to lists Windows Drive letter and VM Disk number in Data center ?
            bikashyadav Novice

            Hi LuCD,

            In my environment CIM instance & WMI is not working as they are not enabled..

            I tried the invoke-vmscript and it gives me the output without CIM Instance & WMI.

            How can we modify the script and use 'Invoke-VMScript -VM $VM -ScriptType Powershell "Get-WmiObject -Class Win32_DiskDrive"'. 

            Thanks

            Bikash

            • 18. Re: PowerCLI script to lists Windows Drive letter and VM Disk number in Data center ?
              LucD Guru
              Community WarriorsUser ModeratorsvExpert

              Do you mean remote WMI is disabled?

              Otherwise it will not work via Invoke-VMScript either.

               

              Btw, it should probably (and possible also provide guest credentials)

               

              Invoke-VMScript -VM $VM -ScriptType Powershell -ScriptText "Get-WmiObject -Class Win32_DiskDrive"
              Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
              • 19. Re: PowerCLI script to lists Windows Drive letter and VM Disk number in Data center ?
                bikashyadav Novice

                HI LucD,

                Below is the output i am getting. Only Invoke-VMScript is working. How can we modify the script and use Invoke-VMScript to have the same output.

                 

                PS C:\Users\Documents> Get-CimInstance -ComputerName $VM -ClassName Win32_DiskDrive
                Get-CimInstance : The connection to the specified remote host was refused. Verify that the WS-Management service is running on the remote host and configured to listen
                for requests on the correct port and HTTP URL.
                At line:1 char:1
                + Get-CimInstance -ComputerName $VM -ClassName Win32_DiskDrive
                + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                    + CategoryInfo          : ResourceBusy: (root\cimv2:Win32_DiskDrive:String) [Get-CimInstance], CimException
                    + FullyQualifiedErrorId : HRESULT 0x80338112,Microsoft.Management.Infrastructure.CimCmdlets.GetCimInstanceCommand
                    + PSComputerName        : XXXXX

                 

                PS C:\Users\Documents> Get-WmiObject -Class Win32_DiskDrive -ComputerName $VM
                Get-WmiObject : The RPC server is unavailable. (Exception from HRESULT: 0x800706BA)
                At line:1 char:1
                + Get-WmiObject -Class Win32_DiskDrive -ComputerName $VM
                + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
                    + CategoryInfo          : InvalidOperation: (:) [Get-WmiObject], COMException
                    + FullyQualifiedErrorId : GetWMICOMException,Microsoft.PowerShell.Commands.GetWmiObjectCommand

                 

                PS C:\Users\Documents> Invoke-Command -ComputerName $VM -ScriptBlock {Get-WmiObject -Class Win32_DiskDrive}
                [XXXXX] Connecting to remote server XXXXX failed with the following error message : The connection to the specified remote host was refused. Verify that the
                WS-Management service is running on the remote host and configured to listen for requests on the correct port and HTTP URL. For more information, see the
                about_Remote_Troubleshooting Help topic.
                    + CategoryInfo          : OpenError: (XXXXX:String) [], PSRemotingTransportException
                    + FullyQualifiedErrorId : CannotConnectWinRMService,PSSessionStateBroken


                PS C:\Users\Documents> Invoke-VMScript -VM $VM -ScriptType Powershell "Get-WmiObject -Class Win32_DiskDrive"

                ScriptOutput
                ---------------------------------------------------------------------------------------------------------------------------------------------------------------------------
                |
                |
                |  Partitions : 1
                |  DeviceID   : \\.\PHYSICALDRIVE5
                |  Model      : VMware Virtual disk SCSI Disk Device
                |  Size       : 536864025600
                |  Caption    : VMware Virtual disk SCSI Disk Device
                |
                |  Partitions : 1
                |  DeviceID   : \\.\PHYSICALDRIVE7
                |  Model      : VMware Virtual disk SCSI Disk Device
                |  Size       : 644245056000
                |  Caption    : VMware Virtual disk SCSI Disk Device
                |
                |  Partitions : 1
                |  DeviceID   : \\.\PHYSICALDRIVE1
                |  Model      : VMware Virtual disk SCSI Disk Device
                |  Size       : 64420392960
                |  Caption    : VMware Virtual disk SCSI Disk Device
                |
                |  Partitions : 1
                |  DeviceID   : \\.\PHYSICALDRIVE10
                |  Model      : VMware Virtual disk SCSI Disk Device
                |  Size       : 1649267343360
                |  Caption    : VMware Virtual disk SCSI Disk Device
                |
                |  Partitions : 1
                |  DeviceID   : \\.\PHYSICALDRIVE4
                |  Model      : VMware Virtual disk SCSI Disk Device
                |  Size       : 53686402560
                |  Caption    : VMware Virtual disk SCSI Disk Device

                 

                Thanks

                Bikash

                • 20. Re: PowerCLI script to lists Windows Drive letter and VM Disk number in Data center ?
                  LucD Guru
                  Community WarriorsUser ModeratorsvExpert

                  What you are seeing is the default output from that WMI object.
                  If you want to see different properties, you will have to use the Select-Object cmdlet

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

                    Hi LucD,

                     

                    Since you saw the output result n it's clear that only invoke-vmscript gives me a valid output.if I run Get-Ciminstance for a remote computer it doesn't work n I have to use invoke-vmscript to get Get-Ciminstance output.

                    So how do I modify this script as and use invoke-vmscript.

                     

                    $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

                        }

                    }

                    • 22. Re: PowerCLI script to lists Windows Drive letter and VM Disk number in Data center ?
                      LucD Guru
                      vExpertCommunity WarriorsUser Moderators

                      Instead of transferring the complete WMI object, you can do the extraction of the required properties on the target station.

                      Use this as an example.
                      It transfers the required data in CSV format, which on the calling side is then converted back to an object.

                      Which you can use in your script

                       

                      $vmName = 'MyVM'

                       

                      $user = 'domain\user'

                      $pswd = 'VMware1!'


                      $secPswd = ConvertTo-SecureString -String $pswd -AsPlainText -Force

                      $cred = New-Object System.Management.Automation.PSCredential ($user, $secPswd)


                      $code = @'

                      $info = foreach($disk in (Get-CimInstance -ClassName Win32_DiskDrive)){

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

                              Get-CimAssociatedInstance -InputObject $partition -ResultClassName Win32_LogicalDisk | select DeviceID, VolumeName,

                                  @{ label = "SCSITarget"; expression = {$disk.SCSITargetId} },

                                  @{ label = "SCSIBus"; expression = {$disk.SCSIPort-2} }

                          }

                      }

                      $info | ConvertTo-Csv -NoTypeInformation -UseCulture

                      '@


                      $vm = Get-VM -Name $vmName


                      $sInvoke = @{

                          VM = $VM

                          ScriptType = 'Powershell'

                          ScriptText = $code

                          GuestCredential = $cred

                      }

                      Invoke-VMScript @sInvoke |

                      Select -ExpandProperty ScriptOutput |

                      ConvertFrom-Csv -UseCulture

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

                        I too wanted to determine the association between vmware scsi devices and their windows partitions and drive letters. 

                        Starting with the information gleaned from  Map VMware virtual disks and Windows drive volumes with a PowerShell script – 4sysops I knew I could not reliably match Windows and VMWare SCSI bus numbers.  I was on my own.  I started by generating detailed information from both VMWare and Windows environments.  I then summarized each environment's SCSI controller information based on number of SCSI target IDs and total disk space used on all drives.  I discovered that once I sorted these summaries, I could see a one-to-one mapping between the VMWare and Windows SCSI controllers.  Once I had a valid mapping, I could then update my VMWare report with the missing Windows information (serial number, disk size, number of partitions, drive letters). 

                         

                        If WMI is accessible for the VMs in question, the script will map VMWare to Windows partitions.  If WMI is not available, it will at least show you the VMWare side of the equation.

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

                          You might want to have a look at the Get-VMGuestDisk cmdlet, new in PowerCLI 12.0.

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

                            Get-VMGuestDisk requires vSphere version 7.0 and later.

                            • 26. Re: PowerCLI script to lists Windows Drive letter and VM Disk number in Data center ?
                              bikashyadav Novice

                              Seems like Get-VMGuestDisk requires vsphere 7 and later plus vmtool 11.x

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

                                That is correct, those are some of the requirements.

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

                                  Hi LucD,

                                   

                                  Still stuggling to get the Drive letter and disk number on single report.

                                  was tried for class-  Win32_DiskDrive command to get the report got failed "WinRM error"

                                  there is any possibility t get the report using vm.Guest.Disks command along with harddisk number

                                   

                                   

                                   

                                  $DiskInfo= @()

                                  foreach ($VMview in Get-VM VMname | Get-View){

                                  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})) {

                                  $VirtualDisk = "" | Select VMname, SCSIController, DiskName, SCSI_ID, DeviceName, DiskFile, DiskSize

                                  $VirtualDisk.VMname = $VMview.Name

                                  $VirtualDisk.SCSIController = $VirtualSCSIController.DeviceInfo.Label

                                  $VirtualDisk.DiskName = $VirtualDiskDevice.DeviceInfo.Label

                                  $VirtualDisk.SCSI_ID = "$($VirtualSCSIController.BusNumber) : $($VirtualDiskDevice.UnitNumber)"

                                  $VirtualDisk.DeviceName = $VirtualDiskDevice.Backing.DeviceName

                                  $VirtualDisk.DiskFile = $VirtualDiskDevice.Backing.FileName

                                  $VirtualDisk.DiskSize = $VirtualDiskDevice.CapacityInKB * 1KB / 1GB

                                  $DiskInfo +=$VirtualDisk

                                  }}}

                                  $DiskInfo | sort VMname, Diskname | Export-Csv -Path C:\temp\server.csv

                                   

                                   

                                  ForEach-Object {$vm.Guest.Disks | ForEach-Object {

                                      $VirtualDisk1 = "" | Select-Object -Property Path,capacity

                                      $VirtualDisk1.Path = $_.Path

                                      $VirtualDisk1.Capacity = $_.Capacity

                                  $DiskInfo +=$VirtualDisk1

                                    }

                                  }

                                  • 29. Re: PowerCLI script to lists Windows Drive letter and VM Disk number in Data center ?
                                    LucD Guru
                                    vExpertCommunity WarriorsUser Moderators

                                    Not sure if you have read my previous comments, but there is no fool-proof method to map Guest OS partitions/drives to VMDK files.

                                    With Get-VMGuestDisk this has changed, but there are prerequisites.

                                    Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                                    1 person found this helpful