VMware Cloud Community
velum1000
Contributor
Contributor

powercli to get drive letter and harddisk number

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

  }

}

3 Replies
LucD
Leadership
Leadership

Not sure if you have read my previous comments in the other thread where you posted this, 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: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

AMZion
Contributor
Contributor

We don't have vsphere 7.0+ yet. So I use this. A lil dirty, but gets the job done.

foreach($vm in (get-vm $ServerName|sort)){
      $nm = $vm.name
      $fqdn = $vm.guest.HostName
      $pwrstat = $vm.powerstate

      IF($pwrstat -match "On"){
          # Get Disk Info from the server
          $OSDriveInfo = Get-DriveInfo -ComputerName $fqdn
          # Get VM HardDisk Info
          $VMDiskInfo = $vm | Get-HardDisk

          #$VMDiskInfo | where{$_.name -match $OSDriveInfo[0].Partition.Split(",")[0].replace("#","") }
          Foreach($vmd in $VMDiskInfo){
              # MATCH THE VM HDD TO THE GUEST OS' LOGIC DRIVE - replace the # in the vm disk name with the actual ordinal# of that disk
              $vmd_OSDriveInfo = $OSDriveInfo | where{$vmd.name.replace($vmd.name.split(" ")[-1],$VMDiskInfo.IndexOf($vmd)) -match $_.Partition.split(",")[0].replace("#","")}

              # make an object of it so it returns more usefully
              New-Object -Type PSCustomObject -Property @{
              Disk = $VMDiskInfo.IndexOf($vmd)
                  VMName = $nm
                  HDDName = $vmd.name
                  TotalGB = $vmd.CapacityGB
                  FreeSpaceGB = [math]::Round(($vm.Guest.Disks | where{$_.Path -match $vmd_OSDriveInfo.DriveLetter}).FreeSpaceGB,1)
                  DriveLetter = $vmd_OSDriveInfo.DriveLetter
              }
          }
      }ELSE{
          Write-Information "FYI: $nm is Powered off. Cannot get local drive info. Returning only VMD info." -InformationAction Continue
          $vm | Get-HardDisk
      }
}

0 Kudos
LucD
Leadership
Leadership

Provided the target VM's Guest OS is Windows and you have the Pscx module installed.
Not to mention the "remote" requirements (WSMan, WMI, ...).

Even then, I suspect you might have issues with multiple partitions on one VMDK.

But yes, if it works in your situation, good script.


Blog: lucd.info  Twitter: @LucD22  Co-author PowerCLI Reference

0 Kudos