FunctionsToExport/Function_Set-VHDChain.ps1

Function Set-VHDChain {

    <#
 
        .SYNOPSIS
        Sets (saves) the Chain Object to its json file
 
        .DESCRIPTION
        Sets (saves) the Chain Object to its json file
 
        .PARAMETER Name
        Specifies the name. default: "VHDChain"
 
        .PARAMETER Location
        Specifies the location. default: "$PWD"
 
        .PARAMETER Chain
        Mandatory,ValueFromPipeline
        PSObject-Representation of the VHDChain
 
        .INPUTS
        Chain
 
        .OUTPUTS
        A VHDChain file - a Json-Representation of the VHDChain
 
    #>


    [CmdletBinding()]
    Param(

        [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",

        [Parameter(Mandatory, ValueFromPipeline)]
        [object[]]$Chain,

        [string]$Encoding = "UTF8"

    )

    $Property = @(
        @{
            Name       = "Index"
            Expression = { 
                if ($_.Path -match '\.vhdchain\.(\d)*\.vhdx') { [int]($Matches[1]) }
            }
        }
        'Path'      
    )

    $Splat = @{
        Path     = Join-Path $Location ($Name + '.vhdchain.json')
        Value    = ConvertTo-Json @($Chain | Select-Object $Property)
        Encoding = $Encoding 
    }

    Set-Content @Splat

}