1 2 3 Previous Next 38 Replies Latest reply on Jul 6, 2020 10:37 PM by vmk2014 Go to original post
      • 30. Re: PowerCLI script to lists Windows Drive letter and VM Disk number in Data center ?
        velum1000 Lurker

        Thank you for the information ,

        finally as per your suggesting have made below script , please help to add to scsicanonicalname and export to csv format.

         

        $volTab = @{}

        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 | %{

         

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

                }

            }

         

        }

        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

            }

        }

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

          So not even Vmware Tools v11 and ESXi v7 can get consistent result from PowerCLI?

          • 32. Re: PowerCLI script to lists Windows Drive letter and VM Disk number in Data center ?
            LucD Guru
            User ModeratorsCommunity WarriorsvExpert

            There are also some prerequisites on the Guest OS, although it seems to work on several Linux based Guest OS.

            Another limiting factor is apparently the filesystem type used, I haven't found a definitive list of which types are supported or not.

             

            Are you seeing issues?
            On which Guest OS and which filesystem type?

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

              Hi LucD,

               

              Below scripts work for me. Can you help me modify the below script to get linux machine details.

               

              param ([ string ]$VMName )

               

              #Define computername

              $computer = $VMName

               

               

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

               

              $ServerDiskToVolume = @(

              Get-WmiObject -Class Win32_DiskDrive | foreach {

               

              $Dsk = $_

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

               

              Get-WmiObject -Query $query | foreach {

               

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

               

              Get-WmiObject -Query $query | Select DeviceID,

              VolumeName,

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

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

              }

              }

              )

               

               

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

              {

               

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

               

               

              [pscustomobject]@{

              VM = $VMView.Name

              #HostName = $VMView.Guest.HostName

              DiskFile = $VirtualDiskDevice.Backing.FileName

              DiskName = $VirtualDiskDevice.DeviceInfo.Label

              DiskSizeGB = $VirtualDiskDevice.CapacityInKB / 1024KB

              #SCSIController = $VirtualSCSIController.BusNumber

              #SCSITarget = $VirtualDiskDevice.UnitNumber

              SCSIid = "$($VirtualSCSIController.BusNumber):$($VirtualDiskDevice.UnitNumber)"

              DeviceID = $MatchingDisk.DeviceID

               

              }

              }

              }

               

              $VMDisks | ft -AutoSize

               

              Thanks

              Bikash

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

                I don't know how many times I have to repeat this but there is no foolproof method, before Get-VMGuestDisk, of mapping Guest OS partitions/drives to VMDK.

                Your script "works" for a Windows Guest OS and in your specific situation.

                The script is partially based on some Windows methods and properties that do not exist in a Linux Guest OS.

                There is no straight-forward way to replicate that script for a Linux Guest OS.

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

                  Hi Lucd,

                   

                  pls assist export the result to csv file, this script is working , only required only export outupt file and due to remote execution policy "Win32_DiskDrive.DeviceID " is not working on our premises,

                   

                  $volTab = @{}

                  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 | %{

                   

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

                          }

                      }

                   

                  }

                  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

                      }

                  }

                   

                   

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

                    The foreach statement doesn't place anything in the pipeline.

                    It is easier to use the Foreach-Object cmdlet in combination with the Pipeline parameter.

                    Like this

                     

                    Get-ScsiController -VM $vm -PipelineVariable ctrl |

                    ForEach-Object -Process {

                        Get-HardDisk -VM $vm -PipelineVariable disk | where{$_.ExtensionData.ControllerKey -eq $ctrl.Key} |

                        ForEach-Object -Process {

                            $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

                        }

                    } | Export-Csv -Path .\report.csv -NoTypeInformation -UseCulture

                    Blog: http://lucd.info | Twitter: @LucD22 | PowerCLI Reference co-author: http://tinyurl.com/hkn4glz
                    1 2 3 Previous Next