Functions/FileSystem/Set-TextFileTree.ps1

Function Set-TextFileTree
{
    [CmdletBinding()]
    Param ()
    DynamicParam
    {
        # Instantiate Runtime Parameter Dictionary, Attach Runtime Parameters, and return
        $RuntimeParameterDictionary = [System.Management.Automation.RuntimeDefinedParameterDictionary]::new()
        $RuntimeParameterDictionary.Add('TextFileTree', (New-StaticParameter -ParamName 'TextFileTree' -ValueType psobject[] -Mandatory $true))
        $RuntimeParameterDictionary.Add('OutputPath', (New-StaticParameter -ParamName 'OutputPath' -ValueType string -Mandatory $true))
        return $RuntimeParameterDictionary
    }

    Begin
    {
        # Convert Runtime Parameter Dictionary into Available Constants
        foreach ($key in $RuntimeParameterDictionary.keys) {New-Variable -Name $key -Value $RuntimeParameterDictionary.$key.value}
    }

    Process
    {
        $NewTree = Foreach ($Node in $TextFileTree)
        {
            $NewPath = "$($OutputPath)\$($Node.Chain)"
            $ExistingTest = Test-Path $NewPath
            $ShouldCreate = if($ExistingTest){if((Get-FileHash -Path $NewPath -Algorithm MD5).Hash -ne $Node.MD5){$TRUE}else{$FALSE;write-verbose "$($Node.chain) already exists with this data [$($node.MD5)]"}}else{$TRUE}
            if($ShouldCreate){($Node.Base64 | Convert-Base64ToString) | Out-File -FilePath $NewPath -Encoding }
            New-Item -Path $NewPath -ItemType File -Value ($Node.Base64 | Convert-Base64ToString) -Force
        }
        $NewTree
    }
}