VMware Cloud Community
sp93
Contributor
Contributor

looking to set connected, and connect at power on to unchecked for Powered on and Powered off VMs

Trying to set the device status on the CD/DVD Drive 1 to be not connected, and not to connect at power on.

The following works on powered on VMs, but not powered off. I understand that the powered off state of a VM doesn't apply to the "connected" state. However what happens is that it errors, and doesn't change the "connect at power on" value for powered off VMs.

get-vm | get-cddrive | set-cddrive -connected $false -startconnected $false -confirm:$false

any help would be greatly appreciated.

Reply
0 Kudos
1 Reply
LucD
Leadership
Leadership

For powered off VMs you can use the ReconfigVM_Task method of the VirtualMachine object.

You check all the devices that are connected to the VM and compare the device label with the label of the device you want to change.

You can give the complete label ("CD/DVD Drive 1") or the first few characters of the label ("CD/DVD").

When the device is found the StartConnected property is set to $false.

$vmName = <VM-name>
$deviceName = "CD/DVD"

$spec = New-Object VMware.Vim.VirtualMachineConfigSpec

$vm = Get-VM $vmName | Get-View
foreach($dev in $vm.Config.Hardware.Device){
	if($dev.DeviceInfo.Label.startswith($deviceName)){
		$tempDev = New-Object VMware.Vim.VirtualDeviceConfigSpec
		$tempDev.operation = "edit"
		$tempDev.device = $dev
		$tempDev.device.Connectable.StartConnected = $false
		$spec.deviceChange += $tempDev
	}
}

$taskMoRef = $vm.ReconfigVM_Task($spec)

$task = Get-View $taskMoRef
while($task.info.state -eq "running" -or $task.info.state -eq "queued"){$task = Get-View $taskMoRef}


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

Reply
0 Kudos