title: Powershell Match virtual hard disks in Virtual Center with their disk labels
body: <p>I would like to match the drive in the windows os to the vCenter <code>*.vmdk</code>.</p>
  
  <p>Here is a <a href="http://echohellowordl.blogspot.ch/2014/02/match-virtual-hard-disks-in-virtual.html" rel="nofollow">link</a> for the informations I need. I find no way to get "Location 192 (Bus Number 0, Target Id 0, LUN 0)" with powershell (info from disk). From the <a href="http://msdn.microsoft.com/en-us/library/aa394132%28v=vs.85%29.aspx" rel="nofollow">WMI</a> I don't get this information... </p>
  
  <p>Can someone help?</p>
  
  <p><br>
  <br></p>
  
  <hr>
  
  <p><br>
  The modified script:</p>
  
  <pre><code>$Vm = "VMName"
  if (($VmView = Get-View -ViewType VirtualMachine -Filter @{"Name" = $Vm})) {
  
      $Out = Get-WmiObject -Class win32_diskdrive -Property Index, SCSIPort, SCSITargetId -ComputerName $Vm 
      #Invoke-VMScript "wmic path win32_diskdrive get Index, SCSIPort, SCSITargetId /format:csv" -vm $VM -scripttype "bat"
          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 SCSIController, DiskName, SCSI_Id, DiskFile,  DiskSize, WindowsDisk
                  $VirtualDisk.SCSIController = $VirtualSCSIController.DeviceInfo.Label
                  $VirtualDisk.DiskName = $VirtualDiskDevice.DeviceInfo.Label
                  $VirtualDisk.SCSI_Id = "$($VirtualSCSIController.BusNumber) : $($VirtualDiskDevice.UnitNumber)"
                  $VirtualDisk.DiskFile = $VirtualDiskDevice.Backing.FileName
                  $VirtualDisk.DiskSize = $VirtualDiskDevice.CapacityInKB * 1KB / 1GB
                  # Match disks based on SCSI ID
                  $DiskMatch = $Out | ?{($_.SCSIPort - 2) -eq $VirtualSCSIController.BusNumber -and $_.SCSITargetID -eq $VirtualDiskDevice.UnitNumber}
                  if ($DiskMatch){
                      $VirtualDisk.WindowsDisk = "Disk $($DiskMatch.Index)"
                  }
                  else {Write-Host "No matching Windows disk found for SCSI id $($VirtualDisk.SCSI_Id)"}
                  $DiskInfo += $VirtualDisk
              }
          }
          $DiskInfo | Out-GridView 
  </code></pre>
  
  <p><br>
  I had to change <code>$DiskMatch = $Out | ?{($_.SCSIPort - 2) -eq</code> &lt;-- Replace - 1 with - 2.<br>
  And put an "-" between SCSI Controller by <code>{$_.DeviceInfo.Label -match "SCSI-Controller"}))</code>.
  Why do I have to subtract 2 from the SCSIPort? </p>