FunctionsToExport/Function_New-VHDChain.ps1

Function New-VHDChain {

    <#
 
        .SYNOPSIS
        Starts a new VHDChain
 
        .DESCRIPTION
        Starts a new VHDChain
        A VHDChain is a series of differencing VHDx files.
        The recent state of this VHDChain is saved in a simple json formatted file.
        This function creates an initial VHDx File utilizing Hyper-V New-VHD.
 
        .PARAMETER Name
        Specifies the name. default: "VHDChain"
 
        .PARAMETER Location
        ValueFromPipeline
        Specifies the location. default: "$PWD"
 
        .PARAMETER SizeBytes
        Specifies the location. default: "64GB"
 
        .PARAMETER Passthru
        Returns the VHD Object representation (return from New-VHD)
 
        .INPUTS
        Location
 
        .OUTPUTS
        Returns the VHD Object representation (return from New-VHD) if Passthru is switched on
 
    #>


    [CmdletBinding()]    
    Param(

        [Parameter(ValueFromPipeline)]
        [ValidateScript({
                $Item = $_ | Get-Item -ErrorAction SilentlyContinue
                if (-not $Item) { throw 'Item ("{0}") does not exist' -f $_ }
                elseif ($Item.Attributes -notmatch "Directory") { throw 'Item ("{0}") is not a directory' -f $_ }
                $true
            }
        )]
        [string]$Location = $PWD,

        [ValidateNotNullOrEmpty()]
        [string]$Name = "VHDChain",

        [Int64]$SizeBytes = 64GB,

        [switch]$PassThru

    )

    $Splat = @{
        Path      = Join-Path $Location ('{0}.vhdchain.{1}.vhdx' -f $Name, 0)
        SizeBytes = $SizeBytes
        Dynamic   = $true
    }
    $VHD = New-VHD @Splat

    if ($VHD) {

        $Splat = @{
            Name     = $Name
            Location = $Location
            Chain    = @($VHD)
        }
        Set-VHDChain @Splat

        if ($PassThru) { $VHD }

    }

}