Highlighted
Contributor
Contributor

Add a new PV SCSI controller via API

Jump to solution

I'm trying to add a new PV SCSI controller to a VM without having to power off the VM and use new-scsicontroller.

I can't get my head around the correct items I need to provide to the spec using "New-Object = VMware.Vim.VirtualMachineConfigSpec" etc, I've found a few helpful articles online but it always seems to just attempt to modify scsi0 and throws an error, rather than adding a new controller.

Can anyone help?

1 Solution

Accepted Solutions
Highlighted
User Moderator
User Moderator

Try this variation

$vmName = 'MyVM'

$vm = Get-VM -Name $vmName

$newBusNumber = ($vm.ExtensionData.Config.Hardware.Device |

    where{$_ -is [VMware.Vim.VirtualSCSIController]} |

    Select -ExpandProperty BusNumber |

    Measure-Object -Maximum |

    Select -ExpandProperty Maximum) + 1

$spec = New-Object VMware.Vim.VirtualMachineConfigSpec

$device = New-Object VMware.Vim.VirtualDeviceConfigSpec

$device.Operation = [VMware.Vim.VirtualDeviceConfigSpecOperation]::add

$device.Device = New-Object VMware.Vim.ParaVirtualSCSIController

$device.Device.BusNumber = $newBusNumber

$spec.DeviceChange += $device

$vm.ExtensionData.ReconfigVM($spec)


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

View solution in original post

0 Kudos
9 Replies
Highlighted
User Moderator
User Moderator

In it's simplest form it could look like this.

$vmName = 'MyVM'

$vm = Get-VM -Name $vmName

$spec = New-Object VMware.Vim.VirtualMachineConfigSpec

$device = New-Object VMware.Vim.VirtualDeviceConfigSpec

$device.Operation = [VMware.Vim.VirtualDeviceConfigSpecOperation]::add

$device.Device = New-Object VMware.Vim.ParaVirtualSCSIController

$device.Device.UnitNumber = -1

$spec.DeviceChange += $device

$vm.ExtensionData.ReconfigVM($spec)

But if there is no disk attached the controller will be automatically removed.

So add a disk and attach it to the controller.


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

Highlighted
Contributor
Contributor

Hi Luc,

This is still returning the same error I was getting before:

Exception calling "ReconfigVM" with "1" argument(s): "Cannot modify existing device 'scsi0'. Cannot modify existing device 'scsi0'."

The VM in question only has a single LSI Logic SAS controller to begin with.

0 Kudos
Highlighted
User Moderator
User Moderator

Try this variation

$vmName = 'MyVM'

$vm = Get-VM -Name $vmName

$newBusNumber = ($vm.ExtensionData.Config.Hardware.Device |

    where{$_ -is [VMware.Vim.VirtualSCSIController]} |

    Select -ExpandProperty BusNumber |

    Measure-Object -Maximum |

    Select -ExpandProperty Maximum) + 1

$spec = New-Object VMware.Vim.VirtualMachineConfigSpec

$device = New-Object VMware.Vim.VirtualDeviceConfigSpec

$device.Operation = [VMware.Vim.VirtualDeviceConfigSpecOperation]::add

$device.Device = New-Object VMware.Vim.ParaVirtualSCSIController

$device.Device.BusNumber = $newBusNumber

$spec.DeviceChange += $device

$vm.ExtensionData.ReconfigVM($spec)


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

View solution in original post

0 Kudos
Highlighted
Contributor
Contributor

That seems to have done the trick, I can then add a disk using New-HardDisk with the -controller parameter.

Doing it this way seem to resolve the issue of using New-HardDisk | New-ScsiController where it doesn't add the disk to node 1.

Thanks for the help Luc!

0 Kudos
Highlighted
Expert
Expert

I knew if I searched long enough I'd find a post where @lucd had the answer.

I've been working on this a couple days and saw multiple posts where this wasn't possible. Just tested and this works great.

Thanks for helping everyone Luc!!

VCP5-DV twitter - @ericblee6 blog - http://vEric.me
0 Kudos
Highlighted
Contributor
Contributor

Hi LucD,

I've seen many your advices about powerCLI code for many vCenter/VM operations.

I'm looking for the correct way to add an LSI Logical SAS SCSI adapter.

The code here create a new ParaVirtual SCSI adapter but I need a LSI one.

I tried to configure the device = New-object VMware.Vim.VirtualSCSIController

and then configured all the parameters I need: bus, key, unit number, sharing mode, etc

but when i execute the reconfigvm operation I receive an error, saying the there is an incoorect parameter but I can't figure out what it can be.

But first of all, I'm not sure that the device i set to create the object is the right one.

Thansk in advance.

Paolo

0 Kudos
Highlighted
User Moderator
User Moderator

That should probably be

$device.Device = New-Object VMware.Vim.VirtualLsiLogicSASController

or

$device.Device = New-Object VMware.Vim.VirtualLsiLogicController

I'm not sure what other properties you tried to change.
I would need to see your script for that.


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

0 Kudos
Highlighted
Contributor
Contributor

Thanks LucD,

I'm at home now, tomorrow morning I'll try using your suggestions and I'll copy here the code and the output from my script.

I'm writing a script to perform a svMotion of VM's with RDM disks and I want mantain the same SCSI id after migration.

The script delete all the raw device from the VM, but losing all the vdisks, also the controller goes away.

I found the code to reconnect all the RDM after creating the controller from the vCenter web client, but the goal is to perform the whole operation with a script, and I worked the whole day trying to find the right way to configure the parameters, starting from the ones I get from the VM before deleting the RDM, so I'm more or less sure they are right, and the only doubt is about the right object I'm creating.

Thanks again and tomorrow I'll answer you.

Good night.

Paolo

0 Kudos
Highlighted
Contributor
Contributor

Hi LucD,

I tried the first one and it worked.

I set the parameters of the scsi controller exactly the same it was before removing the RDM:

PS C:\> $spec5.deviceChange.device

HotAddRemove       :

SharedBus          : physicalSharing

ScsiCtlrUnitNumber : 7

BusNumber          : 1

Device             :

Key                : 1001

DeviceInfo         :

Backing            :

Connectable        :

SlotInfo           :

ControllerKey      : 100

UnitNumber         : 4

and then I made the vm reconfiguration and the scsi controller has been created.

Thanks again.

Have A Nice Day

Paolo

0 Kudos