VMware Cloud Community
JP112
Contributor
Contributor
Jump to solution

Script for attaching multiple existing disk to VM with specific scsi bus number and device number from CSV

Hello,

I was trying to attach multiple vmdk with specific scsi bus number and device number using import-csv method and onyx tools generated code,

Looks like it keep failed and  don't able add scsi bus number and associate device number, i have attached CSV for reference

#File Path

$csvpath = "C:\Users\Desktop\Final\TESt0001.csv"

$vmlist = Import-CSV -path $csvpath

# map the Variables

foreach ($item in $vmlist) {

$vmname = $item.vmname

$vmdk = $item.datastorepath

$busnumber = $item.SCSIBUSNUMBER

$unitnumber = $item.UNITNUMBER

$vm = Get-VM $vmname

Write-Host “Adding Disk “$vmdk” to “$vm” at SCSI ID “$busnumber”:”$unitnumber

$ctrll = Get-ScsiController -VM $vm | ?{$_.extensiondata.busNumber -eq $busnumber}

Write-Host “Controller Key “$ctrll.extensiondata.key

$spec = New-Object VMware.Vim.VirtualMachineConfigSpec

$spec.deviceChange = New-Object VMware.Vim.VirtualDeviceConfigSpec[] (1)

$spec.deviceChange[0] = New-Object VMware.Vim.VirtualDeviceConfigSpec

$spec.deviceChange[0].operation = “add”

$spec.deviceChange[0].device = New-Object VMware.Vim.VirtualDisk

$spec.deviceChange[0].device.key = -100

$spec.deviceChange[0].device.backing = New-Object VMware.Vim.VirtualDiskFlatVer2BackingInfo

$spec.deviceChange[0].device.backing.fileName = “”+$vmdk

$spec.deviceChange[0].device.backing.diskMode = “persistent”

$spec.deviceChange[0].device.connectable = New-Object VMware.Vim.VirtualDeviceConnectInfo

$spec.deviceChange[0].device.controllerKey = [int]$ctrll.extensiondata.Key

$spec.deviceChange[0].device.unitNumber = [int]$unitNumber

$spec.extraConfig = New-Object VMware.Vim.OptionValue[] (1)

$spec.extraConfig[0] = New-Object VMware.Vim.OptionValue

$spec.extraConfig[0].key = “scsi” + $busnumber + “:” + $unitnumber

$vm.ExtensionData.ReconfigVM($spec)

}

Error:

Exception calling "ReconfigVM" with "1" argument(s): "The device '0' is referring to a nonexisting controller '0'."

At line:50 char:1

+ $vm.ExtensionData.ReconfigVM($spec)

+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~

    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException

    + FullyQualifiedErrorId : VimException

Reply
0 Kudos
31 Replies
leomahesh
Enthusiast
Enthusiast
Jump to solution

Hi,

I thought I already did it. Could you please take a look at below and let me know if this will work.

$csvpath = "C:\Users\Desktop\Final\TESt0001.csv"

foreach($vmdk in (Import-CSV -Path $csvpath -UseCulture)){

$vm = Get-VM -Name $vmdk.vmname

$controller = Get-ScsiController -VM $vm | where{$_.ExtensionData.BusNumber -eq $vmdk.SCSIBUSNUMBER}


# Add the VMDK

$spec = New-Object VMware.Vim.VirtualMachineConfigSpec

$device = New-Object VMware.Vim.VirtualDisk

$device.Backing = New-Object VMware.Vim.VirtualDiskFlatVer2BackingInfo

$device.Backing.Filename = $vmdk.datastorepath

$device.UnitNumber = $vmdk.UNITNUMBER

$device.Backing.DiskMode = 'persistent'

$device.ControllerKey = $controller.ExtensionData.Key

$device.Key = -10

$devChange = New-Object VMware.Vim.VirtualDeviceConfigSpec

$devChange[0].Device = $device

$devChange[0].Operation = 'add'

$spec.DeviceChange += $devChange

$vm.ExtensionData.ReconfigVM($spec)

}

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Without knowing what you have in the CSV that will be hard to determine.
If that CSV contains multiple VMDK, the call to ReconfigVM will have to be done outside the foreach loop.


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

Reply
0 Kudos
leomahesh
Enthusiast
Enthusiast
Jump to solution

yes, multiple VMDKs and multiple VMs...

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

If you want to it, one VMDK per one VM, instead of multiple VMDK per one VM, then your script looks ok.


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

Reply
0 Kudos
leomahesh
Enthusiast
Enthusiast
Jump to solution

Yes, thats what I tested just now.

One line per VMDK per VM for multiple VMs, works just fine.

Thanks.

Reply
0 Kudos
leomahesh
Enthusiast
Enthusiast
Jump to solution

Hi Luc,

The above script works just fine, if I am using shared VMDK files. But for RDMs it giving below error.

I add the RDM to first VM and when adding to the second VM as existing disk getting below error.

Exception calling "ReconfigVM" with "1" argument(s): "Incompatible device backing specified for device '0'."
At D:\script\gump\AddExistingDisks\AddExistingDisks.ps1:22 char:1
+ $vm.ExtensionData.ReconfigVM($spec)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : VimException

Contents of the input.csv file:

vmname,datastorepath,SCSIBUSNUMBER,UNITNUMBER
RDMTEST02,[Datastore11] RDMTEST01/RDMTEST01_1.vmdk,1,1

The script I am using is as below.

$vcenter="VCHostname"
Connect-VIServer -server $vcenter

$csvpath = "D:\script\AddExistingDisks\input.csv"
foreach($vmdk in (Import-CSV -Path $csvpath -UseCulture)){
$vm = Get-VM -Name $vmdk.vmname
$controller = Get-ScsiController -VM $vm | where{$_.ExtensionData.BusNumber -eq $vmdk.SCSIBUSNUMBER}

# Add the VMDK
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$device = New-Object VMware.Vim.VirtualDisk
$device.Backing = New-Object VMware.Vim.VirtualDiskFlatVer2BackingInfo
$device.Backing.Filename = $vmdk.datastorepath
$device.UnitNumber = $vmdk.UNITNUMBER
$device.Backing.DiskMode = 'independent_persistent'
$device.ControllerKey = $controller.ExtensionData.Key
$device.Key = -10
$devChange = New-Object VMware.Vim.VirtualDeviceConfigSpec
$devChange[0].Device = $device
$devChange[0].Operation = 'add'
$spec.DeviceChange += $devChange
$vm.ExtensionData.ReconfigVM($spec)
}
Disconnect-VIServer $vcenter -Force -Confirm:$false

 

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

This is normal since the type of Backing used in this snippet is not intended for RDM disks.
For adding RDMs have a look at Solved: Re: Add existing RDM to 2 clustered MSCS VM's - VMware Technology Network VMTN


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

Reply
0 Kudos
leomahesh
Enthusiast
Enthusiast
Jump to solution

Yes, was testing it.

Just changed one line for the backing and it worked.

Thanks for your response anyways.

 

 

Reply
0 Kudos
leomahesh
Enthusiast
Enthusiast
Jump to solution

For anyone who might find this useful, below is the working script.

$vcenter="VCHostname"
Connect-VIServer -server $vcenter

$csvpath = "D:\script\AddExistingDisks\input.csv"
foreach($vmdk in (Import-CSV -Path $csvpath -UseCulture)){
$vm = Get-VM -Name $vmdk.vmname
$controller = Get-ScsiController -VM $vm | where{$_.ExtensionData.BusNumber -eq $vmdk.SCSIBUSNUMBER}

# Add the VMDK
$spec = New-Object VMware.Vim.VirtualMachineConfigSpec
$device = New-Object VMware.Vim.VirtualDisk
$device.Backing = New-Object VMware.Vim.VirtualDiskRawDiskMappingVer1BackingInfo
$device.Backing.Filename = $vmdk.datastorepath
$device.UnitNumber = $vmdk.UNITNUMBER
$device.Backing.DiskMode = 'independent_persistent'
$device.ControllerKey = $controller.ExtensionData.Key
$device.Key = -10
$devChange = New-Object VMware.Vim.VirtualDeviceConfigSpec
$devChange[0].Device = $device
$devChange[0].Operation = 'add'
$spec.DeviceChange += $devChange
$vm.ExtensionData.ReconfigVM($spec)
}
Disconnect-VIServer $vcenter -Force -Confirm:$false

Reply
0 Kudos
Sunshine_S
Enthusiast
Enthusiast
Jump to solution

How can we change the sharing mode to multiwriter for existing disks?

Code capture shows this:

$spec.DeviceChange[0].Device.Backing.Sharing = 'sharingMultiWriter'

But I don't know how to put that in a script.

I need to this for 20 disks using a script.

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Please don't cross-post (duplicate in How can we change the disk sharing mode to multi-w... - VMware Technology Network VMTN)


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

Reply
0 Kudos
jagsana240485
Contributor
Contributor
Jump to solution

This script is working for normal vmdk. any adjustments needed for RDM disks? I am trying to add RDM pointer disks to a VM using the same scripts but it is throwing below error.

 

Exception calling "ReconfigVM" with "1" argument(s): "Incompatible device backing specified for device '0'."
At line:61 char:5
+ $vm.ExtensionData.ReconfigVM($spec)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : VimException

Exception calling "ReconfigVM" with "1" argument(s): "Incompatible device backing specified for device '0'."
At line:61 char:5
+ $vm.ExtensionData.ReconfigVM($spec)
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [], MethodInvocationException
+ FullyQualifiedErrorId : VimException

Reply
0 Kudos