VMware Cloud Community
noaboa
Contributor
Contributor
Jump to solution

Create/add Sata Controller over powerCLI

I found this post which let's me change the virtual device node of the vm: Re: How to Change a CDDrive to use a SATA Controller but I can't figure out how to create a sata controller. when I run the code it tells me, that the device requires a controller. How can I create a sata controller?

Thanks

Best

Noah

1 Solution

Accepted Solutions
LucD
Leadership
Leadership
Jump to solution

Try like this

$vmName = 'MyVM'

$vm = Get-VM -Name $vmName


$spec = New-Object VMware.Vim.VirtualMachineConfigSpec

$dev = New-Object VMware.Vim.VirtualDeviceConfigSpec

$dev.Operation = [vmware.vim.VirtualDeviceConfigSpecOperation]::add


$ctrl = New-Object VMware.Vim.VirtualAHCIController

$ctrl.BusNumber = 0

$ctrl.UnitNumber = -1

$ctrl.DeviceInfo = New-Object VMware.Vim.Description

$ctrl.DeviceInfo.Label = 'My new SATA controller'

$ctrl.DeviceInfo.Summary = 'My new SATA controller'


$dev.Device = $ctrl

$spec.DeviceChange += $dev


$vm.ExtensionData.ReconfigVM($spec)


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

View solution in original post

9 Replies
LucD
Leadership
Leadership
Jump to solution

Try like this

$vmName = 'MyVM'

$vm = Get-VM -Name $vmName


$spec = New-Object VMware.Vim.VirtualMachineConfigSpec

$dev = New-Object VMware.Vim.VirtualDeviceConfigSpec

$dev.Operation = [vmware.vim.VirtualDeviceConfigSpecOperation]::add


$ctrl = New-Object VMware.Vim.VirtualAHCIController

$ctrl.BusNumber = 0

$ctrl.UnitNumber = -1

$ctrl.DeviceInfo = New-Object VMware.Vim.Description

$ctrl.DeviceInfo.Label = 'My new SATA controller'

$ctrl.DeviceInfo.Summary = 'My new SATA controller'


$dev.Device = $ctrl

$spec.DeviceChange += $dev


$vm.ExtensionData.ReconfigVM($spec)


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

noaboa
Contributor
Contributor
Jump to solution

Thank you very much, the creation of the Sata controller worked. But I'm getting an error and the CD/DVD Driver isn't assigned to the Sata controller.

Ausnahme beim Aufrufen von "ReconfigVM" mit 1 Argument(en):  "Cannot find the device '3,000', which is referenced in the edit or remove device operation."

In C:\TEMP\CreateServer\CreateAndInstallServer.ps1:254 Zeichen:5

+     $currentvm.ExtensionData.ReconfigVM($spec)

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

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

    + FullyQualifiedErrorId : VimException

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

How did you obtain that number 3000?
It should be the Key of the SATA controller.


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

Reply
0 Kudos
noaboa
Contributor
Contributor
Jump to solution

I didn't obtain the number 3000. The code from the other post does that if I understand it correctly.

$cdName = 'CD/DVD drive 1'

    $sataName = 'SATA controller 0'

    $cd = Get-CDDrive -VM $currentvm -Name $cdName

    $sataCtrl = $currentvm.ExtensionData.Config.Hardware.Device | where{$_ -is [VMware.Vim.VirtualAHCIController] -and $_.DeviceInfo.Label -eq $sataName}

    $unitInuse = $currentvm.ExtensionData.Config.Hardware.Device | where{$_.ControllerKey -eq $sataCtrl.Key} | %{$_.UnitNumber}

    $freeUnit = 0..29 | where{$_ -ne $sataCtrl.UnitNumber -and $unitInuse -notcontains $_ -and $_ -ne $cd.ExtensionData.UnitNumber} | Measure-Object -Minimum | select -ExpandProperty Minimum

   

    $dev.Device = $cd.ExtensionData

    $dev.Device.ControllerKey = $sataCtrl.Key

    $dev.Device.UnitNumber = $freeUnit

    $dev.Operation = [VMware.Vim.VirtualDeviceConfigSpecOperation]::edit

    $spec.DeviceChange += $dev

   

    $currentvm.ExtensionData.ReconfigVM($spec)

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

I ran the following, and that seems to work for me.
You are sure that names of the CDdrive and SATA controller are correct?

$vmName = 'MyVM'

$vm = Get-VM -Name $vmName

$cdName = 'CD/DVD drive 1'

$sataName = 'SATA controller 0'

$cd = Get-CDDrive -VM $vm -Name $cdName


$sataCtrl = $vm.ExtensionData.Config.Hardware.Device |

        where{$_ -is [VMware.Vim.VirtualAHCIController] -and $_.DeviceInfo.Label -eq $sataName}

$unitInuse = $vm.ExtensionData.Config.Hardware.Device |

        where{$_.ControllerKey -eq $sataCtrl.Key} | %{$_.UnitNumber}

$freeUnit = 0..29 | where{$_ -ne $sataCtrl.UnitNumber -and $unitInuse -notcontains $_ -and $_ -ne $cd.ExtensionData.UnitNumber} |

        Measure-Object -Minimum | select -ExpandProperty Minimum


$spec = New-Object VMware.Vim.VirtualMachineConfigSpec

$dev.Device = $cd.ExtensionData

$dev.Device.ControllerKey = $sataCtrl.Key

$dev.Device.UnitNumber = $freeUnit

$dev.Operation = [VMware.Vim.VirtualDeviceConfigSpecOperation]::edit


$spec.DeviceChange += $dev

 

$vm.ExtensionData.ReconfigVM($spec)


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

Reply
0 Kudos
noaboa
Contributor
Contributor
Jump to solution

We'll I was stupid and thought, that I didn't need to create a new object twice because I did it in my code above already.

$spec = New-Object VMware.Vim.VirtualMachineConfigSpec

Is there a good source where I can learn how the api stuff works when there is no cmdlet?

Thank you very much. Works perfectly.

Best

Noah

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

There isn't really a book on using the vSphere API from PowerShell/PowerCLI.
There are books in the SDK Documentation for other languages, which you can use to learn the concepts.

There is a chapter in the book in my footer.

And I would advise looking at existing examples, here and in blog posts.
The best way to learn this is to practise.


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

Reply
0 Kudos
noaboa
Contributor
Contributor
Jump to solution

Okay Thanks.

I kind of understand if I see the code but if I don't see it it's hard to find out what I have to do or where I can find the infos I'm looking for. I looked in the API Documentation about creating a Sata controller but didn't make it clearer for me.

The book look great I have to get it sometime.

Yh practise is the best way of learing also I'm a beginner with powershell and everything is self learned from some blog or forum but soon I'll have my first powershell course and hopefully I will learn a lot.

Cheers

Reply
0 Kudos
LucD
Leadership
Leadership
Jump to solution

Just a warning, that book is intended as a Reference, not as a learning guide.

Btw, if you're looking for learning roadmaps for PowerShell and PowerCLI, these are some good oneshttps://thecrazyconsultant.com/powercli-study-guide-core-concepts/

https://thecrazyconsultant.com/powershell-study-guide-core-concepts/

https://thecrazyconsultant.com/powercli-study-guide-core-concepts/


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