VMware Cloud Community
Mozhartatti
Contributor
Contributor

How to attach multiple existing harddisks to a single VM

Hi,
How can I attach multiple (existing) hard drives to a VM and change their Sharing to "Multiwriter" with ReconfigVM_Task method?

settings.csv contains:

vmdk,scsi
[datastore] VM/VM.vmdk,2

I cannot figure out how to add multiple changes to $spec.deviceChange:

 

 

$vm = Get-VM "MyVM"
$items = Import-Csv c:\settings.csv

$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$spec.deviceChange = New-Object VMware.Vim.VirtualDeviceConfigSpec
$count = 0

$items | ForEach-Object {
    $vmdk = $_.vmdk
    $scsi_controller = $_.scsi

    $vm | New-HardDisk -DiskPath "$vmdk" `
                       -Persistence IndependentPersistent `
                       -Controller "SCSI controller $($scsi_controller)" `
                       -OutVariable hd

    $diskDevice = $hd.ExtensionData
    $diskDeviceBaking = $hd.ExtensionData.backing

    $spec.deviceChange[$count].operation = 'edit'
    $spec.deviceChange[$count].device = New-Object VMware.Vim.VirtualDisk
    $spec.deviceChange[$count].device = $diskDevice
    $spec.DeviceChange[$count].device.backing = New-Object VMware.Vim.VirtualDiskFlatVer2BackingInfo
    $spec.DeviceChange[$count].device.backing = $diskDeviceBaking
    $spec.DeviceChange[$count].device.Backing.Sharing = "sharingMultiWriter"

    $count++
}

Write-Host "`nEnabling Multiwriter flag on on VMDK:" $hd.Name "for VM:" $vm.name
$task = $vm.ExtensionData.ReconfigVM_Task($spec)
$task1 = Get-Task -Id ("Task-$($task.value)")
$task1 | Wait-Task

 

 

 

0 Kudos
2 Replies
Mozhartatti
Contributor
Contributor

Thanks, yes. That previous post isn't clear on the question asked.  I am not sure how to delete it.

I hope current post makes things clearer as to what I am asking

0 Kudos