VMware Cloud Community
binoche
VMware Employee
VMware Employee

hot adding ahci controller to vm using powercli?

Hi Experts,

sorry I am new to powercli, and try to work out a powercli script to hot add ahci controller to vm,

unfortunately my script failed to work, would you please shed a light what was wrong? really appreciate

Function New-AHCIController {

    Param (

        [Parameter(Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True)]

        $VM

    )

    Process {

        $VMSpec = New-Object VMware.Vim.VirtualMachineConfigSpec

        $VMSpec.deviceChange = New-Object VMware.Vim.VirtualDeviceConfigSpec

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

        $VMSpec.deviceChange[0].operation = "add"

        $VMSpec.deviceChange[0].device = New-Object VMware.Vim.VirtualAHCIController

        $VMspec.deviceChange[0].device.backing = New-Object VMware.Vim.VirtualDeviceBackingInfo

        $VMspec.deviceChange[0].device.busnumber = 0

        $VMspec.deviceChange[0].device.controllerkey = 100

        $VMspec.deviceChange[0].device.key = 15001

        $VMspec.deviceChange[0].device.unitnumber = 25

        $VMspec.deviceChange[0].device.device = 16000

        $VMspec.deviceChange[0].device.slotinfo = New-Object VMware.Vim.VirtualDeviceBusSlotInfo

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

        $VM.ExtensionData.ReconfigVM($VMSpec)        

    }

}

$vcserver="192.168.1.100"
Connect-VIServer -server $vcserver -username root -password vmware

$vm=get-vm|where {$_.name -eq "ahci-SLES11"}

$vm|New-AHCIController

Exception calling "ReconfigVM" with "1" argument(s): "Invalid configuration for device '0'."

At line:20 char:37

+         $VM.ExtensionData.ReconfigVM <<<< ($VMSpec)

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

    + FullyQualifiedErrorId : DotNetMethodException

Tags (2)
0 Kudos
5 Replies
LucD
Leadership
Leadership

That code seems to be generated by Onyx.

The disadvantage of Onyx is that it captures a specific call to a method, and that Onyx will use the parameter values from that specific call in the generated code.

To create a general function based on the generated code, you will have to add code to get the correct values for the parameters, or use the correct, general values.

In your code I see the following issues:

  • the ESXi will assign the unique Key property. Use a negative number ! This allows you to refer to the new device in your function
  • Make sure you are using a unique UnitNumber on a specific controller. If you don't need devices to have specific unitnumbers, leave out the assignment
  • device.device is used to specify which devices are attached to the controller you are creating. Make sure that this is/are (a) key(s) for (an) existing devices. Add some code to find the correct device(s)
  • device.busnumber: make sure this is a unique number. Again add some code to check which busnumbers are already in use on the VM, and pick the next free one
  • controllerKey, I'm not sure you need to specify a controllerkey for a controller. Try leaving out this assignment


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

0 Kudos
binoche
VMware Employee
VMware Employee

thanks LucD for your kind and detailed reply, please see inline, I will have a retry soon,

"

That code seems to be generated by Onyx.

The disadvantage of Onyx is that it captures a specific call to a method, and that Onyx will use the parameter values from that specific call in the generated code.

To create a general function based on the generated code, you will have to add code to get the correct values for the parameters, or use the correct, general values.

[simon] I did not know Onyx before Smiley Happy, my code was mostly from internet,

In your code I see the following issues:

  • the ESXi will assign the unique Key property. Use a negative number ! This allows you to refer to the new device in your function

[simon] thanks, $VMspec.deviceChange[0].device.key = -1

  • Make sure you are using a unique UnitNumber on a specific controller. If you don't need devices to have specific unitnumbers, leave out the assignment

[simon] # $VMspec.deviceChange[0].device.unitnumber = 25

  • device.device is used to specify which devices are attached to the controller you are creating. Make sure that this is/are (a) key(s) for (an) existing devices. Add some code to find the correct device(s)

[simon] I still can not understand, I guess I should define a new virtualahcicontroller here?

  • device.busnumber: make sure this is a unique number. Again add some code to check which busnumbers are already in use on the VM, and pick the next free one

[simon] thanks, let me try with $VMspec.deviceChange[0].device.busnumber = 1

  • controllerKey, I'm not sure you need to specify a controllerkey for a controller. Try leaving out this assignment

[simon] thanks, # $VMspec.deviceChange[0].device.controllerkey = 100

"

0 Kudos
LucD
Leadership
Leadership

An additional tip, if you get the dreaded "Invalid configuration for device 0", have a look at the vpxd log on the vCenter.

In some cases the error messages in there say which specific property is considered incorrect.

And you get an over view of the complete Spec as it is seen by vCenter.


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

binoche
VMware Employee
VMware Employee

thanks LucD, really appreicate, and I have got another guru's help and my script finally works,

Function New-AHCIController {

    Param (

        [Parameter(Mandatory=$True,ValueFromPipeline=$True,ValueFromPipelinebyPropertyName=$True)]

        $VM

    )

    Process {

        $global:VMSpec = New-Object VMware.Vim.VirtualMachineConfigSpec

        $VMSpec.deviceChange = New-Object VMware.Vim.VirtualDeviceConfigSpec

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

        $VMSpec.deviceChange[0].operation = "add"

        $VMSpec.deviceChange[0].device = New-Object VMware.Vim.VirtualAHCIController

        $Busnumber = ($VM.ExtensionData.Config.Hardware.device | Where { $_.gettype().Name -eq "VirtualAHCIController" } | Select -ExpandProperty busnumber | Sort-Object -Descending | Select -First 1) +1

        $VMspec.deviceChange[0].device.busnumber = $Busnumber

        $VMspec.deviceChange[0].device.key = -1

        $VM.ExtensionData.ReconfigVM($VMSpec)       

    }

}

0 Kudos
friedman1966
Contributor
Contributor

This is great - just what I was looking for.

Now I need a way to add a new disk to the AHCI controller!

0 Kudos