Private/Get-CustomizationsArg.ps1

<#
    ProvisioningTools — Automates creation of Windows provisioning packages.
    Copyright (C) 2022 David Haymond.

    This program is free software: you can redistribute it and/or modify
    it under the terms of the GNU General Public License as published by
    the Free Software Foundation, either version 3 of the License, or
    (at your option) any later version.

    This program is distributed in the hope that it will be useful,
    but WITHOUT ANY WARRANTY; without even the implied warranty of
    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
    GNU General Public License for more details.

    You should have received a copy of the GNU General Public License
    along with this program. If not, see <https://www.gnu.org/licenses/>.
#>


function Get-CustomizationsArg {
    [CmdletBinding()]
    [OutputType([hashtable])]
    param (
        [string] $ComputerName,
        [pscredential] $LocalAdminCredential,
        [string] $DomainName,
        [pscredential] $DomainJoinCredential,
        [System.Object[]] $Application,
        [hashtable[]] $Wifi,
        [string] $KioskXml
    )

    # Save a list of all parameters except Application, Wifi, and KioskXml
    $customizationsArgs = @{ }
    foreach ($key in $PSBoundParameters.Keys) {
        if ($key -notin ('Application', 'Wifi', 'KioskXml')) {
            $customizationsArgs[$key] = $PSBoundParameters[$key]
        }
    }

    if ($KioskXml) {
        if (Test-Path $KioskXml) {
            $customizationsArgs.KioskXml = Resolve-Path $KioskXml
        }
        else {
            Write-Error "The kiosk XML file `"$KioskXml`" cannot be found."
        }
    }

    if ($null -ne $Wifi) {
        $customizationsArgs.Wifi = $Wifi | ForEach-Object -Process {
            $wifiArg = $_ | Copy-Hashtable
            if (-not $wifiArg.Ssid) {
                Write-Error 'The Wi-Fi SSID is missing. Make sure that each Wi-Fi hashtable has an Ssid property.'
            }

            if ($wifiArg.SecurityKey) {$wifiArg.SecurityType = 'WPA2-Personal' }
            else { $wifiArg.SecurityType = 'Open' }

            if ($null -eq $wifiArg.AutoConnect) {
                $wifiArg.AutoConnect = $true
            }

            $wifiArg
        }
    }

    if ($null -ne $Application) {
        $customizationsArgs.Application = $Application | ForEach-Object -Process {
            if ($_ -is [string]) {
                $app = @{ Path = $_ }
            }
            elseif ($_ -is [hashtable]) {
                $app = $_ | Copy-Hashtable
            }
            else { Write-Error 'The application is invalid.' }

            if (!$app.Path) {
                Write-Error 'The application path is missing.'
            }

            $firstPath = $app.Path | Select-Object -First 1 | Resolve-Path

            if (!$app.Name) {
                $app.Name = $firstPath | Split-Path -LeafBase
            }
            if (!$app.Command) {
                $app.Command = 'cmd /c "{0}"' -f ($firstPath | Split-Path -Leaf)
            }
            if ($null -eq $app.ContinueInstall) {
                $app.ContinueInstall = $true
            }
            if ($null -eq $app.RestartRequired) {
                $app.RestartRequired = $false
            }
            if ($null -eq $app.RestartExitCode) {
                $app.RestartExitCode = 3010
            }
            if ($null -eq $app.SuccessExitCode) {
                $app.SuccessExitCode = 0
            }

            $app.Dependencies = $app.Path | ForEach-Object {
                if (-not (Test-Path -Path $_ -PathType Leaf)) {
                    Write-Error ('The application "{0}" cannot be found.' -f $_)
                }

                @{
                    Name = $_ | Split-Path -LeafBase
                    Path = $_ | Resolve-Path
                }
            }

            $batchFilename = [System.IO.Path]::GetRandomFileName() + '.bat'
            $batchPath = Join-Path -Path $script:TEMPDIR -ChildPath $batchFilename
            Set-Content -Path $batchPath -Value @"
set LOGFILE=%SystemDrive%\$($app.Name).log
echo Executing $($app.Command) >> %LOGFILE%
$($app.Command) >> %LOGFILE%
echo Result: %ERRORLEVEL% >> %LOGFILE%
"@

            $app.BatchPath = $batchPath
            $app.BatchCmd = "cmd /c `"$batchFilename`""

            $app
        }
    }
    $customizationsArgs
}