Skip to content

Add-HyperVVMDvdDrive

Hyper-V: Adds a virtual DVD drive to a virtual machine

#Requires -Version 5.1
#Requires -Modules Hyper-V

[CmdletBinding()]
Param (
    [string]$ComputerName = "localhost",

    [PSCredential]$Credential,

    [Parameter(Mandatory = $true)]
    [string]$Name,

    [ValidateSet('SCSI', 'IDE')]
    [string]$ControllerType = "SCSI",

    [int]$ControllerNumber = 0,

    [string]$Path
)

Process {
    try {
        $params = @{
            'ComputerName' = $ComputerName
            'ErrorAction'  = 'Stop'
        }
        if ($Credential) { $params.Add('Credential', $Credential) }

        $vm = Get-VM @params | Where-Object { $_.Name -eq $Name -or $_.Id -eq $Name }

        if (-not $vm) {
            throw "Virtual machine '$Name' not found on '$ComputerName'."
        }

        $addParams = @{ 'VM' = $vm; 'ControllerType' = $ControllerType; 'ControllerNumber' = $ControllerNumber; 'ErrorAction' = 'Stop' }
        if ($Path) { $addParams.Add('Path', $Path) }

        Add-VMDvdDrive @addParams

        $dvdDrives = Get-VMDvdDrive -VM $vm | Where-Object { $_.ControllerType -eq $ControllerType -and $_.ControllerNumber -eq $ControllerNumber }
        
        $result = [PSCustomObject]@{
            VMName           = $vm.Name
            ControllerType   = $ControllerType
            ControllerNumber = $ControllerNumber
            DvdDriveCount    = $dvdDrives.Count
            Action           = "DvdDriveAdded"
            Status           = "Success"
            Timestamp        = Get-Date -Format "yyyy-MM-dd HH:mm:ss"
        }

        Write-Output $result
    }
    catch {
        throw
    }
}

Specifies the name of the Hyper-V host. Defaults to the local machine.

Specifies the credentials to use for the remote connection.

Specifies the name or ID of the virtual machine.

Optional. Specifies the controller type (SCSI or IDE). Defaults to SCSI.

Optional. Specifies the controller number. Defaults to 0.

Optional. Specifies the path to an ISO file to mount upon creation.

An interactive directory of PowerShell scripts.