通过PowerCLI

时间:2016-02-04 09:07:43

标签: powershell vsphere esxi powercli

我有一个问题让我难过,我无法弄清楚如何解决它。这是我的困境......我正在测试一个存储阵列。对于每个测试,我创建一组卷,并将它们呈现给我的ESXi主机。每个主机有2到4个VM,每个VM需要连接4到8个RDM。我编写了一个附加RDM的脚本,它可以很好地与4个RDM配合使用。这是问题...当映射8个RDM时,我需要在虚拟SCSI适配器上均匀分布设备。理想情况下,我想要以下内容:

Hard disk 1  on  SCSI 0:1
Hard disk 2  on  SCSI 0:2
Hard disk 3  on  SCSI 1:1
Hard disk 4  on  SCSI 1:2
Hard disk 5  on  SCSI 2:1
Hard disk 6  on  SCSI 2:2
Hard disk 7  on  SCSI 3:1
Hard disk 8  on  SCSI 3:2

我尝试使用new-harddisk命令创建新设备。对于我创建的每个硬盘,我将标志递增1.这适用于将HD 1置于0:1。然后,如果标志是> 0它创建磁盘然后执行new-scsicontroller命令。这使得HD 2,3和4分别为1:2,2:2和3:2。不完全是我想要的,我可以忍受它。剩余的4个HD,5-8最终在总线0上,因为我可以添加的最大新控制器是4.这里是我的代码:

$devicePointer = 0
foreach($vmhost in $vmhosts){
$localVMs = Get-VM -location $vmhost -name $vmNamingPattern | Sort-Object name
$devices = Get-ScsiLun -VmHost $vmhost -CanonicalName $deviceNamePattern | Sort-Object id
foreach ($localVM in $localVMs){
$flag = 0
    for($i=$devicePointer;$i -lt $devicePointer+$devicesPerVM; $i++){
        $disk = New-HardDisk -VM $localVM -DeviceName $devices[$i].ConsoleDeviceName -DiskType RawPhysical
    if ($flag -ne 0) {
        New-ScsiController -HardDisk $disk -BusSharingMode NoSharing -Type ParaVirtual
    }
    $flag+=1
        write-host $localVM $disk
    }
    $devicePointer = $devicePointer+$devicesPerVM
}

}

我尝试了另一种只为标志值1,2或3添加新scsi控制器的方法。然后我运行了以下内容:

Get-HardDisk -VM $localVM | Where {$_.Name -eq "Hard disk 7" | Set-HardDisk -Controller "SCSI controller 1" -Confirm:$false

这导致单位数递增1.如果它在0:7,它将移动到0:8或2:0或其他。基本上它与指定的值完全相反。

1 个答案:

答案 0 :(得分:0)

$consdevnam = "/vmfs/devices/disks/"
$devicePointer = 0
foreach ($global:Vmhost in $global:my_VM_hosts){
    foreach ($localVM in $global:my_VMs){
        $global:scsi_ctrl = $null
        $global:scsi_ctrl = @()
        $flag = 0
        for($i=$devicePointer;$i -lt $devicePointer+$devicesPerVM; $i++){
            if ($flag -eq 0 -or ($flag % 4) -eq 0){
                $disk = New-HardDisk -VM $localVM -DeviceName ($consdevnam + $naa_ids[$i]) -DiskType RawPhysical -WarningAction SilentlyContinue
            }
            if ($flag -ne 0 -and $flag -lt 4) {
                $disk = New-HardDisk -VM $localVM -DeviceName ($consdevnam + $naa_ids[$i]) -DiskType RawPhysical -WarningAction SilentlyContinue
                $global:scsi_ctrl += New-ScsiController -HardDisk $disk -BusSharingMode NoSharing -Type ParaVirtual -WarningAction SilentlyContinue
            }
            elseif ($flag -ne 0 -and ($flag % 4) -lt 4) {
                $f = ($flag % 4)
                if ($f -eq 1){
                    $disk = New-HardDisk -VM $localVM -DeviceName ($consdevnam + $naa_ids[$i]) -Controller ($global:scsi_ctrl[0]).name -DiskType RawPhysical –Confirm:$false -WarningAction SilentlyContinue
                }                                                                                                                                                              
                if ($f -eq 2){                                                                                                                                                 
                    $disk = New-HardDisk -VM $localVM -DeviceName ($consdevnam + $naa_ids[$i]) -Controller ($global:scsi_ctrl[1]).name -DiskType RawPhysical –Confirm:$false -WarningAction SilentlyContinue
                }                                                                                                                                                              
                if ($f -eq 3){                                                                                                                                                 
                    $disk = New-HardDisk -VM $localVM -DeviceName ($consdevnam + $naa_ids[$i]) -Controller ($global:scsi_ctrl[2]).name -DiskType RawPhysical –Confirm:$false -WarningAction SilentlyContinue
                }
            }
            $flag+=1
            write-host "Added $disk to $localVM"
        }
        $devicePointer = $devicePointer+$devicesPerVM
    }
}