Skip to content

New-CDDrive

VMware: Creates a new virtual CD drive

#Requires -Version 5.1
#Requires -Modules VMware.VimAutomation.Core

[CmdletBinding()]
Param(
    [Parameter(Mandatory = $true)]
    [string]$VIServer,
    [Parameter(Mandatory = $true)]
    [pscredential]$VICredential,
    [Parameter(Mandatory = $true)]
    [string]$VMName,
    [string]$HostDevice,
    [string]$IsoPath,
    [switch]$StartConnected
)

Process {
    try {
        $vmServer = Connect-VIServer -Server $VIServer -Credential $VICredential -ErrorAction Stop
        $machine = Get-VM -Server $vmServer -Name $VMName -ErrorAction Stop
        $drive = New-CDDrive -Server $vmServer -VM $machine -StartConnected:$StartConnected -Confirm:$false -ErrorAction Stop
        if ($PSBoundParameters.ContainsKey('IsoPath')) { $null = Set-CDDrive -CD $drive -IsoPath $IsoPath -Confirm:$false -ErrorAction Stop }
        if ($PSBoundParameters.ContainsKey('HostDevice')) { $null = Set-CDDrive -CD $drive -HostDevice $HostDevice -Confirm:$false -ErrorAction Stop }
        $result = Get-CDDrive -Server $vmServer -VM $machine -ErrorAction Stop | Select-Object *
        if ($null -ne $result) { $result | Add-Member -NotePropertyName Timestamp -NotePropertyValue (Get-Date -Format "yyyy-MM-dd HH:mm:ss") -PassThru -Force }
    }
    catch { throw }
    finally { if ($null -ne $vmServer) { Disconnect-VIServer -Server $vmServer -Force -Confirm:$false -ErrorAction SilentlyContinue } }
}

IP address or DNS name of the vSphere server

PSCredential object for authenticating with the server

Virtual machine to which to attach the new CD drive

Path to the CD drive on the host backing the virtual CD drive

Datastore path to the ISO file backing the virtual CD drive

Off

CD drive starts connected when the VM powers on

An interactive directory of PowerShell scripts.