2014-10-07 126 views
1

我想將windows操作系統中的驅動器與vCenter *.vmdk相匹配。Powershell將虛擬中心中的虛擬硬盤與其磁盤標籤匹配

這是我需要的信息link。我發現沒有辦法獲得「位置192(總線號0,目標ID 0,LUN 0)」與powershell(從磁盤信息)。從WMI我沒有得到這個信息...

有人可以幫忙嗎?





修改後的腳本:

$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 


我不得不改變$DiskMatch = $Out | ?{($_.SCSIPort - 2) -eq < - 更換 - 1 - 2
,把一個「 - 「之間的SCSI控制器{$_.DeviceInfo.Label -match "SCSI-Controller"}))。 爲什麼我必須從SCSIPort中減去2?

+0

有趣的閱讀:http://www.van-lieshout.com/2009/12/match-vm-and-windows-harddisks-using-powercli/ – 2014-10-07 11:48:24

+0

謝謝你的URL。我看到了這個,但id不會在我的環境中工作。我修改了腳本,它工作。但我認爲有更好的解決方案? – Patrick 2014-10-07 13:15:27

回答

0

以下腳本會將本地windows磁盤與vmWare磁盤相匹配。

Date       : 2017.10.27 21:28:01 
vCenterName     : vCenterName 
vmName      : SERVER 
vmWareSCSIController   : SCSI controller 0 
wmWareSCSIID     : 0 : 0 
vmWareDiskName    : Hard disk 1 
vmWareDiskFile    : [Datastore] vm.vmdk 
vmWareSizeGB     : 40 
WindowsSerialNumber   : WindowsSerialNumber 
WindowsSCSIBus    : 0 
WindowsSCSILogicalUnit  : 0 
WindowsSCSIPort    : 2 
WindowsSCSITargetId   : 0 
WindowsDisk     : \\SERVER\root\cimv2:Win32_DiskDrive.DeviceID="\\\\.\\PHYSICALDRIVE0" 
WindowsDriveLetter   : C: 
WindowsLocicalDiskSizeGB  : 39.9980430603027 
WindowsLocicalDiskFreeSpaceGB : 9.30975723266602 
WindowsLocicalDiskUsedSpaceGB : 30.6882858276367 



#Variables 
$Vm = Get-VM -Name 'VMName' 
$ComputerName = 'ComputerName' 

$obj_DiskDrive = @() 
$obj_LogicalDisk = @() 
$obj_LogicalDiskToPartition = @() 
$obj_DiskDriveToDiskPartition = @() 
$obj_VMView = @() 
$obj_DiskInfos = @() 


#Get wmi objects 
$obj_DiskDrive = Get-WmiObject -Class win32_DiskDrive -ComputerName $ComputerName 
$obj_LogicalDisk = Get-WmiObject -Class Win32_LogicalDisk -ComputerName $ComputerName 
$obj_LogicalDiskToPartition = Get-WmiObject -Class Win32_LogicalDiskToPartition -ComputerName $ComputerName 
$obj_DiskDriveToDiskPartition = Get-WmiObject -Class Win32_DiskDriveToDiskPartition -ComputerName $ComputerName 

#Get vm    
$obj_VMView = Get-View -ViewType VirtualMachine -Filter @{"Name" = "$($Vm.Name)"} 

#Get vm disk 
$obj_VMDisk = Get-HardDisk -VM $Vm 

#Match the informations  
foreach ($obj_vmWareSCSIController in ($obj_VMView.Config.Hardware.Device | Where-Object -FilterScript {$_.DeviceInfo.Label -match "SCSI"})) 
{ 
    foreach ($obj_vmWareDiskDevice in ($obj_VMView.Config.Hardware.Device | Where-Object -FilterScript {$_.ControllerKey -eq $obj_vmWareSCSIController.Key})) 
    {          
     $obj_tempDiskInfos = "" | Select-Object -Property Date, vCenterName, vmName, vmWareSCSIController, wmWareSCSIID, vmWareDiskName, vmWareDiskFile, 
      vmWareSizeGB, WindowsSerialNumber, WindowsSCSIBus, WindowsSCSILogicalUnit, WindowsSCSIPort, WindowsSCSITargetId, WindowsDisk, WindowsDriveLetter, 
      WindowsLocicalDiskSizeGB, WindowsLocicalDiskFreeSpaceGB, WindowsLocicalDiskUsedSpaceGB 

     #Select WMI object 
     $obj_currentDiskDrive = @() 
     $obj_currentDiskDrive = $obj_DiskDrive | Where-Object -FilterScript {$_.SerialNumber -eq $obj_vmWareDiskDevice.Backing.Uuid.Replace("-","")} 

     $obj_currentDiskDriveToDiskPartition = @() 
     $obj_currentDiskDriveToDiskPartition = $obj_DiskDriveToDiskPartition | Where-Object -FilterScript {$_.Antecedent -eq $obj_currentDiskDrive.Path} 

     $obj_currentLogicalDiskToPartition = @() 
     $obj_currentLogicalDiskToPartition = $obj_LogicalDiskToPartition | Where-Object -FilterScript {$_.Antecedent -eq $obj_currentDiskDriveToDiskPartition.Dependent} 

     $obj_currentLogicalDisk = @() 
     $obj_currentLogicalDisk = $obj_LogicalDisk | Where-Object -FilterScript {$_.Path.Path -eq $obj_currentLogicalDiskToPartition.Dependent} 

     #Select vmWare object 
     $obj_CurrentvmWareHarddisk = @() 
     $obj_CurrentvmWareHarddisk = $obj_VMDisk | Where-Object -FilterScript {$_.Name -eq $obj_vmWareDiskDevice.DeviceInfo.Label} 

     #Generate output 
     $obj_tempDiskInfos.Date = Get-Date -Format "yyyy.MM.dd HH:mm:ss" 
     $obj_tempDiskInfos.vCenterName = $defaultVIServer.Name 
     $obj_tempDiskInfos.vmName = $Vm.Name 
     $obj_tempDiskInfos.vmWareSCSIController = $obj_vmWareSCSIController.DeviceInfo.Label 
     $obj_tempDiskInfos.wmWareSCSIID = "$($obj_vmWareSCSIController.BusNumber) : $($obj_vmWareDiskDevice.UnitNumber)" 
     $obj_tempDiskInfos.vmWareDiskName = $obj_vmWareDiskDevice.DeviceInfo.Label 
     $obj_tempDiskInfos.vmWareDiskFile = $obj_vmWareDiskDevice.Backing.FileName 
     $obj_tempDiskInfos.vmWareSizeGB = $obj_CurrentvmWareHarddisk.CapacityGB    
     $obj_tempDiskInfos.WindowsSerialNumber = $obj_currentDiskDrive.SerialNumber 
     $obj_tempDiskInfos.WindowsSCSIBus = $obj_currentDiskDrive.SCSIBus 
     $obj_tempDiskInfos.WindowsSCSILogicalUnit = $obj_currentDiskDrive.SCSILogicalUnit 
     $obj_tempDiskInfos.WindowsSCSIPort = $obj_currentDiskDrive.SCSIPort 
     $obj_tempDiskInfos.WindowsSCSITargetId = $obj_currentDiskDrive.SCSITargetId 
     $obj_tempDiskInfos.WindowsDisk = $obj_currentDiskDrive.Path.Path 
     $obj_tempDiskInfos.WindowsDriveLetter = ($obj_currentLogicalDisk).Caption 
     $obj_tempDiskInfos.WindowsLocicalDiskSizeGB = $obj_currentLogicalDisk.Size/1GB 
     $obj_tempDiskInfos.WindowsLocicalDiskFreeSpaceGB = $obj_currentLogicalDisk.FreeSpace/1GB 
     $obj_tempDiskInfos.WindowsLocicalDiskUsedSpaceGB = ($obj_currentLogicalDisk.Size/1GB) - ($obj_currentLogicalDisk.FreeSpace/1GB) 

     $obj_DiskInfos += $obj_tempDiskInfos 
    } 
} 

$obj_DiskInfos