PowerCLI

 View Only
  • 1.  Create CD/ DVDdrive without shutdow vm

    Posted Jun 20, 2019 02:48 PM

    Hello,

    I would like create a device CD/DRIVE DVD without shutdow vm but there is a error message display : "The VM must be in the following state : Poweredoff"

    i must use this solution because i have a constraint to upgrade vmwaretools.

    The goal is to do a script wich deploy vmtools on the VMs starting to create this device.

    Thank you very much for your help.



  • 2.  RE: Create CD/ DVDdrive without shutdow vm
    Best Answer

    Posted Jun 20, 2019 06:50 PM

    Try like this

    $vmName = 'MyVM'

    $vm = Get-VM -Name $vmName


    $spec = New-Object VMware.Vim.VirtualMachineConfigSpec

    $dev1 = New-Object VMware.Vim.VirtualDeviceConfigSpec

    $dev1.Operation = [VMware.Vim.VirtualDeviceConfigSpecOperation]::add

    $cd = New-Object VMware.Vim.VirtualCdrom

    $cd.Key = -1

    $cd.ControllerKey = -2

    $cd.Backing = New-Object VMware.Vim.VirtualCdromRemotePassthroughBackingInfo

    $cd.Backing.DeviceName = ''

    $dev1.Device = $cd

    $spec.DeviceChange += $dev1


    $dev2 = New-Object VMware.Vim.VirtualDeviceConfigSpec

    $dev2.Operation = [VMware.Vim.VirtualDeviceConfigSpecOperation]::add

    $ctrl = New-Object VMware.Vim.VirtualAHCIController

    $ctrl.Key = -2

    $dev2.Device = $ctrl

    $spec.DeviceChange += $dev2


    $vm.ExtensionData.ReconfigVM($spec)



  • 3.  RE: Create CD/ DVDdrive without shutdow vm

    Posted Jun 21, 2019 09:18 AM

    Hello Luc,

    Thank you very much for your feedback.

    I added following lines at end code to mount iso file :

    Get-VM -Name $vm | Get-CDDrive |

    Set-CDDrive -ISOPath "[datastore] \.iso" -Connected $true -Confirm:$false

    Thank you again.



  • 4.  RE: Create CD/ DVDdrive without shutdow vm

    Posted Jun 21, 2019 09:23 AM

    The CD parameter expects a CDDrive object, not a VM.
    Try like this

    Get-CDDrive -VM $vm |

    Set-CDDrive -ISOPath $Fullpath -Connected $true -Confirm:$false



  • 5.  RE: Create CD/ DVDdrive without shutdow vm

    Posted Jul 24, 2019 09:19 AM

    Hello LucD,

    I come back to you because i have an other question regarding this script. In this case, the script create a new cddrive.

    On some virtual machine i have already a CD-Drive create so i would like add a condition :

    if CD-DRive Create

         add iso and connect CD-Drive

    it's possible?

    I will search. if i find something i will answer on post.

    Thank you.


    Best Regards,



  • 6.  RE: Create CD/ DVDdrive without shutdow vm

    Posted Jul 24, 2019 09:37 AM

    Try something like this

    Get-VM |

    ForEach-Object -Process {

       try

       {

       $cd = Get-CDDrive -VM $_ -ErrorAction Stop

       }

       catch

       {

       $cd = New-CDDrive -VM $_ -Confirm:$false -StartConnected:$true

       }

       Set-CDDrive -CD $cd -IsoPath $iso -Confirm:$false

    }



  • 7.  RE: Create CD/ DVDdrive without shutdow vm

    Posted Jul 24, 2019 01:29 PM

    Thank you for your feedback.

    I added lines at the end script but without success :

    foreach($vmlist in (Get-Content -Path C:\VMware\listvm.txt)){

    $vm = Get-VM -Name $vmlist

    $datastore = "[datastore]"

    $isopath = "Vmware tools/linux.iso"

    $Fullpath = "$datastore $isopath"

    $spec = New-Object VMware.Vim.VirtualMachineConfigSpec

    $dev1 = New-Object VMware.Vim.VirtualDeviceConfigSpec

    $dev1.Operation = [VMware.Vim.VirtualDeviceConfigSpecOperation]::add

    $cd = New-Object VMware.Vim.VirtualCdrom

    $cd.Key = -1

    $cd.ControllerKey = -2

    $cd.Backing = New-Object VMware.Vim.VirtualCdromRemotePassthroughBackingInfo

    $cd.Backing.DeviceName = 'pcc-007349\Vmware tools\linux.iso'

    $dev1.Device = $cd

    $spec.DeviceChange += $dev1

    $dev2 = New-Object VMware.Vim.VirtualDeviceConfigSpec

    $dev2.Operation = [VMware.Vim.VirtualDeviceConfigSpecOperation]::add

    $ctrl = New-Object VMware.Vim.VirtualAHCIController

    $ctrl.Key = -2

    $dev2.Device = $ctrl

    $spec.DeviceChange += $dev2

    $vm.ExtensionData.ReconfigVM($spec)

    Get-VM -Name $vm |

    ForEach-Object -Process {

       try

       {

       $cd = Get-CDDrive -VM $_ -ErrorAction Stop

       }

       catch

       {

       $cd = New-CDDrive -VM $_ -Confirm:$false -StartConnected:$true

       }

       Set-CDDrive -CD $cd -IsoPath "[datastore] Vmware tools\linux.iso" -Confirm:$false

    }

    }



  • 8.  RE: Create CD/ DVDdrive without shutdow vm

    Posted Jul 24, 2019 01:39 PM

    Looks like there is a Question pending.
    Try using the Get-VMquestion and Set-VMquestion. See the provided examples to see how it works.