Skip to content

VMHardDiskDrive

dscbot edited this page Jun 12, 2022 · 3 revisions

VMHardDiskDrive

Parameters

Parameter Attribute DataType Description Allowed Values
VMName Key String Specifies the name of the virtual machine whose hard disk drive is to be manipulated.
Path Key String Specifies the full path of the VHD file to be manipulated.
ControllerType Write String Specifies the type of controller (IDE/SCSI) to which the hard disk drive is to be set. If not specified, it defaults to SCSI. IDE, SCSI
ControllerNumber Write UInt32 Specifies the number of the controller to which the hard disk drive is to be set. For IDE the possible values are 0 or 1, for SCSI the possible values are 0, 1, 2, or 3. If not specified, it defaults to 0. 0, 1, 2, 3
ControllerLocation Write UInt32 Specifies the number of the location on the controller at which the hard disk drive is to be set. Possible values for IDE are 0 or 1, and for SCSI the possible values are between 0 to 63. If not specified, it defaults to 0.
Ensure Write String Specifies if the hard disk drive must be present (exist) or absent (not exist). If not specified, it defaults to Present. Present, Absent

Description

Manages VHD(X)s attached to a Hyper-V virtual machine.

When parameter ControllerNumber or ControllerLocation is not provided, the same logic as Set-VMHardDiskDrive command is used.

Requirements

  • The Hyper-V Role has to be installed on the machine.
  • The Hyper-V PowerShell module has to be installed on the machine.

Examples

Example 1

VM with an extra disk.

configuration Example
{
    param
    (
        [Parameter()]
        [System.String[]]
        $NodeName = 'localhost',

        [Parameter(Mandatory = $true)]
        [System.String]
        $VMName,

        [Parameter(Mandatory = $true)]
        [System.String]
        $VhdPath
    )

    Import-DscResource -ModuleName 'HyperVDsc'
    Import-DscResource -ModuleName 'PSDesiredStateConfiguration'

    Node $NodeName
    {
        # Install HyperV feature, if not installed - Server SKU only
        $diskNameOS = "$VMName-DiskOS.vhdx"
        $diskNameExtra1 = "$VMName-Disk1.vhdx"

        WindowsFeature HyperV
        {
            Ensure = 'Present'
            Name   = 'Hyper-V'
        }

        WindowsFeature HyperVPowerShell
        {
            Ensure = 'Present'
            Name   = 'Hyper-V-PowerShell'
        }

        VHD DiskOS
        {
            Name             = $diskNameOS
            Path             = $VhdPath
            Generation       = 'vhdx'
            MaximumSizeBytes = 20GB
            Ensure           = 'Present'
            DependsOn        = '[WindowsFeature]HyperV'
        }

        VHD Disk1
        {
            Name             = $diskNameExtra1
            Path             = $VhdPath
            Generation       = 'vhdx'
            MaximumSizeBytes = 20GB
            Ensure           = 'Present'
            DependsOn        = '[WindowsFeature]HyperV'
        }

        VMHyperV NewVM
        {
            Ensure     = 'Present'
            Name       = $VMName
            VhdPath    = Join-Path $VhdPath -ChildPath $diskNameOS
            Generation = 1
            DependsOn  = '[VHD]DiskOS'
        }

        VMHardDiskDrive ExtraDisk
        {
            VMName             = $VMName
            Path               = Join-Path $VhdPath -ChildPath $diskNameExtra1
            ControllerType     = 'IDE'
            ControllerNumber   = 0
            ControllerLocation = 1
            Ensure             = 'Present'
            DependsOn          = '[VHD]Disk1'
        }
    }
}

Example 2

VM with 4 additional disks.

configuration Example
{
    param
    (
        [Parameter()]
        [System.String[]]
        $NodeName = 'localhost',

        [Parameter(Mandatory = $true)]
        [System.String]
        $VMName,

        [Parameter(Mandatory = $true)]
        [System.String]
        $VhdPath
    )

    Import-DscResource -ModuleName 'HyperVDsc'
    Import-DscResource -ModuleName 'PSDesiredStateConfiguration'

    Node $NodeName
    {
        $diskNameOS = "$VMName-OS.vhdx"

        # Install HyperV feature, if not installed - Server SKU only
        WindowsFeature HyperV
        {
            Ensure = 'Present'
            Name   = 'Hyper-V'
        }

        WindowsFeature HyperVPowerShell
        {
            Ensure = 'Present'
            Name   = 'Hyper-V-PowerShell'
        }

        # Create the VHD for the OS
        VHD DiskOS
        {

            Name             = $diskNameOS
            Path             = $VhdPath
            Generation       = 'vhdx'
            MaximumSizeBytes = 20GB
            Ensure           = 'Present'
            DependsOn        = '[WindowsFeature]HyperV'
        }

        # Create the VM
        VMHyperV NewVM
        {
            Name       = $VMName
            VhdPath    = Join-Path $VhdPath -ChildPath $diskNameOS
            Generation = 1
            Ensure     = 'Present'
            DependsOn  = '[VHD]DiskOS'
        }

        # Ensures a SCSI controller exists on the VM
        VMScsiController Controller
        {
            Ensure           = 'Present'
            VMName           = $VMName
            ControllerNumber = 0
            DependsOn        = '[VMHyperV]NewVM'
        }

        foreach ($i in 0 .. 3)
        {
            $diskName = "$VMName-Disk-$i.vhdx"

            # Create the VHD
            VHD "Disk-$i"
            {

                Name             = $diskName
                Path             = $VhdPath
                Generation       = 'vhdx'
                MaximumSizeBytes = 20GB
                Ensure           = 'Present'
                DependsOn        = '[WindowsFeature]HyperV'
            }

            # Attach the VHD
            VMHardDiskDrive "ExtraDisk-$i"
            {
                VMName             = $VMName
                Path               = Join-Path $VhdPath -ChildPath $diskName
                ControllerType     = 'SCSI'
                ControllerLocation = $i
                Ensure             = 'Present'
                DependsOn          = '[VMScsiController]Controller', "[VHD]Disk-$i"
            }
        }
    }
}