VMwareHomeLab.psm1

#Region '.\Private\Get-ModuleBase.ps1' 0
function Get-ModuleBase
{
    [CmdletBinding()]
    [OutputType([System.String])]
    param (
    )

    return $MyInvocation.MyCommand.Module.ModuleBase
}
#EndRegion '.\Private\Get-ModuleBase.ps1' 10
#Region '.\Private\Join-JsonMaterial.ps1' 0
Function Join-JsonMaterial
{
    [CmdletBinding()]

    Param(
        [Parameter(Mandatory = $true)]
        [String]$sourceDir
    )
    # Regenerate the file All.json list for all appliances
    Remove-Item -Path $sourceDir\All.json  -Force -ErrorAction SilentlyContinue | Out-Null

    # check if the source directory is present
    if (!(Test-Path $sourceDir))
    {
        Write-Host -Message "ERROR: Path not found.Check the path and try again!" -ForegroundColor Red
    }
    else
    {
        $allFiles = @()

        # get the list of files in the source directory
        ForEach ($i in Get-ChildItem $sourceDir -Filter *.json)
        {
            $fileName = $i.Name
            $data = Get-Content -Path $sourceDir\$fileName -Raw | ConvertFrom-Json
            $allFiles += $data
        }
        # write the output to a file
        $allFiles | ConvertTo-Json -Depth 2  | Out-File -FilePath $sourceDir\All.json
    }
}
#EndRegion '.\Private\Join-JsonMaterial.ps1' 32
#Region '.\Public\Get-VMwareHomeLab.ps1' 0
function Get-VMwareHomeLab
{
    [OutputType([System.IO.FileInfo])]
    [CmdletBinding()]

    param
  ()

    DynamicParam
    {
        $folderBase = Get-ModuleBase
        $FolderJson = Join-path $folderBase -ChildPath 'Material'
        $FolderAria2c = Join-path $folderBase -ChildPath "Aria2c"
        Join-JsonMaterial -sourceDir $FolderJson
        $ParamAttrib = New-Object System.Management.Automation.ParameterAttribute

        $ParamAttrib.Mandatory = $true

        $ParamAttrib.ParameterSetName = '__AllParameterSets'
        $AttribColl = New-Object  System.Collections.ObjectModel.Collection[System.Attribute]
        $AttribColl.Add($ParamAttrib)
        $configurationFileNames = (Get-ChildItem -Path $FolderJson -file -Filter *.json).BaseName
        $AttribColl.Add((New-Object  System.Management.Automation.ValidateSetAttribute($configurationFileNames)))

        $RuntimeParam = New-Object System.Management.Automation.RuntimeDefinedParameter('ApplianceVersion', [string], $AttribColl)

        $RuntimeParamDic = New-Object System.Management.Automation.RuntimeDefinedParameterDictionary

        $RuntimeParamDic.Add('ApplianceVersion', $RuntimeParam)

        return  $RuntimeParamDic
    }

    process
    {
        $folderBase = Get-ModuleBase
        $FolderJson = Join-path $folderBase -ChildPath 'Material'
        $FolderAria2c = Join-path $folderBase -ChildPath "Aria2c"
        $folderDestination = "c:\VMwareHomeLan\Material"

        if (!(Test-Path $folderDestination))
        {
            New-Item -ItemType Directory -Path $folderDestination -force | out-null
        }

        write-host  "$($PSBoundParameters.ApplianceVersion) appliance" -ForegroundColor Green -NoNewline
        write-host  " is selected for download"

        $json = Get-Content -path  "$FolderJson\$($PSBoundParameters.ApplianceVersion).json" -Raw | ConvertFrom-Json
        New-Item -Path $FolderJson -Name donwloads.txt -Force | Out-Null

        #Create the aria2c script for the download
        for ($i = 0; $i -lt $json.Count; $i++)
        {

            #Extract URI for Appliance
            $json[$i].URL_public | Out-File "$FolderJson\donwloads.txt" -Append

            #Extract FileName for Appliance FileName
            $json[$i].FileName | % { $out = " out=" + "$_" | Out-File "$FolderJson\donwloads.txt" -Append }
            $json[$i].Hash -split " "  | % { $out = " checksum=" + "sha-256=" + "$_" | Out-File "$FolderJson\donwloads.txt" -Append }
            $endfile = "`n" | Out-File "$FolderJson\donwloads.txt" -Append
        }

        $Aria2cSource = "$FolderAria2c\aria2c.exe"
        $ArgumentList = "--no-conf --log-level=info --download-result=hide --optimize-concurrent-downloads --file-allocation=none --log=TrueNasdowload.log"
        $ArgumentList1 = "-x16 -s16 -j3 -c -V -R -d"
        $ArgumentList2 = "-i"

        $file = "$FolderJson\donwloads.txt"

        Start-Process -FilePath $Aria2cSource -ArgumentList "$ArgumentList $ArgumentList1 $folderDestination $ArgumentList2 $file" -Wait -NoNewWindow

        remove-item -Path "$FolderJson\donwloads.txt" -Force -ErrorAction SilentlyContinue | Out-Null

        write-host  "$($PSBoundParameters.ApplianceVersion) appliance" -ForegroundColor Green -NoNewline
        write-host  " is available in $folderDestination"
        write-host "The default password for appliance is"  -NoNewline
        write-host  " TrueNas123*" -ForegroundColor Green

    }
}
#EndRegion '.\Public\Get-VMwareHomeLab.ps1' 83