Enthusiast
Enthusiast

How to Change a CDDrive to use a SATA Controller

Jump to solution

I have figured out how to ADD a SATA Controller in PowerCLI, but I can't figure out how to change a CDDrive to use it.

When you add a CDDrive with New-CDDrive, it by default attaches it to IDE(0:0). I want to change it to SATA(0:0) (You can assume the Sata controller is already created)..

Attached is the place in the Web GUI.

Thanks!

Aaron

1 Solution

Accepted Solutions
Leadership
Leadership

No cmdlet support for SATA controllers yet, you'll have to fall back to the API methods.

Try something like the following.

The UnitNumber got me stick for a while. I came to the following conclusions, but could find no documented justification for all of this.

  • A SATA controll can have 30 devices (documented)
  • A SATA controller uses a unitnumber (documented)
  • Devices already connected to the SATA controller use unitnumbers which can not be used (documented)
  • The IDE unitnumber seems to fail when reused for the SATA unitnumber (no clue why that is, but the script excludes it)

$vmName = 'MyVM'

$cdName = 'CD/DVD drive 1'

$sataName = 'SATA controller 0'

$vm = Get-VM -Name $vmName

$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

# Point CD/DVD to SATA ctrl

$dev = New-Object VMware.Vim.VirtualDeviceConfigSpec

$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

View solution in original post

0 Kudos
5 Replies
Leadership
Leadership

No cmdlet support for SATA controllers yet, you'll have to fall back to the API methods.

Try something like the following.

The UnitNumber got me stick for a while. I came to the following conclusions, but could find no documented justification for all of this.

  • A SATA controll can have 30 devices (documented)
  • A SATA controller uses a unitnumber (documented)
  • Devices already connected to the SATA controller use unitnumbers which can not be used (documented)
  • The IDE unitnumber seems to fail when reused for the SATA unitnumber (no clue why that is, but the script excludes it)

$vmName = 'MyVM'

$cdName = 'CD/DVD drive 1'

$sataName = 'SATA controller 0'

$vm = Get-VM -Name $vmName

$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

# Point CD/DVD to SATA ctrl

$dev = New-Object VMware.Vim.VirtualDeviceConfigSpec

$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

View solution in original post

0 Kudos
Enthusiast
Enthusiast

Thanks LucD!

This worked perfectly!

AK

0 Kudos
Enthusiast
Enthusiast

An alternative method, if you consider you're creating a new VM with no CD drive or SATA controller, here $cfg.iso_path holds the path to the ISO I wanted to connect to the CD drive, and $new_vm is the newly created VM (without -CD)

$vmConfigSpec = New-Object VMware.Vim.VirtualMachineConfigSpec

$vmConfigSpec.DeviceChange = New-Object VMware.Vim.VirtualDeviceConfigSpec[] (2)

$vmConfigSpec.DeviceChange[0] = New-Object VMware.Vim.VirtualDeviceConfigSpec

$vmConfigSpec.DeviceChange[0].Device = New-Object VMware.Vim.VirtualAHCIController

$vmConfigSpec.DeviceChange[0].Device.DeviceInfo = New-Object VMware.Vim.Description

$vmConfigSpec.DeviceChange[0].Device.DeviceInfo.Summary = 'New SATA Controller'

$vmConfigSpec.DeviceChange[0].Device.DeviceInfo.Label = 'New SATA Controller'

$vmConfigSpec.DeviceChange[0].Device.Key = -103

$vmConfigSpec.DeviceChange[0].Device.BusNumber = 0

$vmConfigSpec.DeviceChange[0].Operation = 'add'

$vmConfigSpec.DeviceChange[1] = New-Object VMware.Vim.VirtualDeviceConfigSpec

$vmConfigSpec.DeviceChange[1].Device = New-Object VMware.Vim.VirtualCdrom

$vmConfigSpec.DeviceChange[1].Device.Connectable = New-Object VMware.Vim.VirtualDeviceConnectInfo

$vmConfigSpec.DeviceChange[1].Device.Connectable.Connected = $false

$vmConfigSpec.DeviceChange[1].Device.Connectable.AllowGuestControl = $true

$vmConfigSpec.DeviceChange[1].Device.Connectable.StartConnected = $true

$vmConfigSpec.DeviceChange[1].Device.Backing = New-Object VMware.Vim.VirtualCdromIsoBackingInfo

$vmConfigSpec.DeviceChange[1].Device.Backing.FileName = $cfg.iso_path

$vmConfigSpec.DeviceChange[1].Device.ControllerKey = -103

$vmConfigSpec.DeviceChange[1].Device.UnitNumber = 0

$vmConfigSpec.DeviceChange[1].Device.DeviceInfo = New-Object VMware.Vim.Description

$vmConfigSpec.DeviceChange[1].Device.DeviceInfo.Summary = 'New CD/DVD Drive'

$vmConfigSpec.DeviceChange[1].Device.DeviceInfo.Label = 'New CD/DVD Drive'

$vmConfigSpec.DeviceChange[1].Device.Key = -104

$vmConfigSpec.DeviceChange[1].Operation = 'add'

$vm_view = $new_vm | Get-View

$vm_view.reconfigVM($vmConfigSpec)

add-sata-cd.ps1 · GitHub

Contributor
Contributor

How did you add a new sata controller over Powercli? I can't find anything in google.

0 Kudos
Leadership
Leadership

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

0 Kudos