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
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:
Blog: lucd.info Twitter: @LucD22 Co-author PowerCLI Reference
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 , my code was mostly from internet,
In your code I see the following issues:
[simon] thanks, $VMspec.deviceChange[0].device.key = -1
[simon] # $VMspec.deviceChange[0].device.unitnumber = 25
[simon] I still can not understand, I guess I should define a new virtualahcicontroller here?
[simon] thanks, let me try with $VMspec.deviceChange[0].device.busnumber = 1
[simon] thanks, # $VMspec.deviceChange[0].device.controllerkey = 100
"
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
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)
}
}
This is great - just what I was looking for.
Now I need a way to add a new disk to the AHCI controller!