Private/GetWindowsScripts.ps1

function GetWindowsScripts {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$True)]
        [string]$Win64PackageUrl,
    
        [Parameter(Mandatory=$True)]
        [string]$Win64PackageName
    )
    
    [System.Collections.ArrayList]$WindowsPMInstallScriptPrep = @(
        'try {'
        " if (`$(Get-Module -ListAvailable).Name -notcontains 'ProgramManagement') {`$null = Install-Module ProgramManagement -ErrorAction Stop}"
        " if (`$(Get-Module).Name -notcontains 'ProgramManagement') {`$null = Import-Module ProgramManagement -ErrorAction Stop}"
        ' $null = Uninstall-Program -ProgramName powershell-core -ErrorAction SilentlyContinue'
        ' $InstallPwshResult = Install-Program -ProgramName powershell-core -CommandName pwsh.exe -ExpectedInstallLocation "$env:ProgramFiles\PowerShell"'
        ' if (!$InstallPwshResult.MainExecutable) {throw "Unable to find pwsh.exe after successful installation!"}'
        ' $RegistrySystemPath = "HKLM:\System\CurrentControlSet\Control\Session Manager\Environment"'
        ' $CurrentSystemPath = $(Get-ItemProperty -Path $RegistrySystemPath -Name PATH).Path'
        ' [System.Collections.Arraylist][array]$CurrentSystemPathArray = $CurrentSystemPath -split ";" | Where-Object {![System.String]::IsNullOrWhiteSpace($_)} | Sort-Object | Get-Unique'
        ' $PwshParentDir = $InstallPwshResult.MainExecutable | Split-Path -Parent'
        ' if ($CurrentSystemPathArray -notcontains $PwshParentDir) {'
        ' $CurrentSystemPathArray.Insert(0,$PwshParentDir)'
        ' $UpdatedSystemPath = $CurrentSystemPathArray -join ";"'
        ' Set-ItemProperty -Path $RegistrySystemPath -Name PATH -Value $UpdatedSystemPath'
        ' }'
        '} catch {'
        ' Write-Error $_'
        " `$global:FunctionResult = '1'"
        ' return'
        '}'
        'echo powershellInstallComplete'
    )
    $InstallPwshBytes = [System.Text.Encoding]::Unicode.GetBytes($($WindowsPMInstallScriptPrep -join "`n"))
    $EncodedCommandInstallPwsh = [Convert]::ToBase64String($InstallPwshBytes)
    [System.Collections.ArrayList]$WindowsPMInstallScript = @("powershell -NoProfile -EncodedCommand $EncodedCommandInstallPwsh")

    [System.Collections.ArrayList]$WindowsManualInstallScriptPrep = @(
        "`$OutFilePath = Join-Path `$HOME 'Downloads\$Win64PackageName'"
        "Invoke-WebRequest -Uri $Win64PackageUrl -OutFile `$OutFilePath"
        '$DateStamp = Get-Date -Format yyyyMMddTHHmmss'
        '$MSIFullPath = $OutFilePath'
        '$MSIParentDir = $MSIFullPath | Split-Path -Parent'
        '$MSIFileName = $MSIFullPath | Split-Path -Leaf'
        "`$MSIFileNameOnly = `$MSIFileName -replace [regex]::Escape('.msi'),''"
        "`$logFile = Join-Path `$MSIParentDir (`$MSIFileNameOnly + `$DateStamp + '.log')"
        '$MSIArguments = @('
        " '/i'"
        ' $MSIFullPath'
        " '/qn'"
        " '/norestart'"
        " '/L*v'"
        ' $logFile'
        ')'
        'Start-Process msiexec.exe -ArgumentList $MSIArguments -Wait -NoNewWindow'
        'echo powershellInstallComplete'
    )
    $InstallPwshBytes = [System.Text.Encoding]::Unicode.GetBytes($($WindowsManualInstallScriptPrep -join "`n"))
    $EncodedCommandInstallPwsh = [Convert]::ToBase64String($InstallPwshBytes)
    [System.Collections.ArrayList]$WindowsManualInstallScript = @("powershell -NoProfile -EncodedCommand $EncodedCommandInstallPwsh")

    [System.Collections.ArrayList]$WindowsUninstallScript = @(
        'try {'
        ' if ($(Get-Module -ListAvailable).Name -notcontains "ProgramManagement") {$null = Install-Module ProgramManagement -ErrorAction Stop}'
        ' if ($(Get-Module).Name -notcontains "ProgramManagement") {$null = Import-Module ProgramManagement -ErrorAction Stop}'
        ' Install-Program -ProgramName powershell-core -CommandName pwsh.exe -ExpectedInstallLocation "$env:ProgramFiles\PowerShell" -ErrorAction Stop'
        '} catch {'
        ' Write-Error $_'
        ' $global:FunctionResult = "1"'
        ' return'
        '}'
        'try {'
        ' Uninstall-Program -ProgramName powershell-core -ErrorAction Stop'
        '} catch {'
        ' Write-Error $_'
        ' $global:FunctionResult = "1"'
        ' return'
        '}'
        'echo pwshConfigComplete'
    )
    $InstallPwshBytes = [System.Text.Encoding]::Unicode.GetBytes($($WindowsUninstallScript -join "`n"))
    $EncodedCommandInstallPwsh = [Convert]::ToBase64String($InstallPwshBytes)
    [System.Collections.ArrayList]$WindowsUninstallScript = @("powershell -NoProfile -EncodedCommand $EncodedCommandInstallPwsh")

    [System.Collections.ArrayList]$WindowsPwshRemotingScript = @(
        'try {'
        " if (`$(Get-Module -ListAvailable).Name -notcontains 'WinSSH') {`$null = Install-Module WinSSH -ErrorAction Stop}"
        " if (`$(Get-Module).Name -notcontains 'WinSSH') {`$null = Import-Module WinSSH -ErrorAction Stop}"
        ' Install-WinSSH -GiveWinSSHBinariesPathPriority -ConfigureSSHDOnLocalHost -DefaultShell pwsh'
        '} catch {'
        ' Write-Error $_'
        " `$global:FunctionResult = '1'"
        ' return'
        '}'
    )
    $InstallPwshBytes = [System.Text.Encoding]::Unicode.GetBytes($($WindowsPwshRemotingScript -join "`n"))
    $EncodedCommandInstallPwsh = [Convert]::ToBase64String($InstallPwshBytes)
    [System.Collections.ArrayList]$WindowsPwshRemotingScript = @("powershell -NoProfile -EncodedCommand $EncodedCommandInstallPwsh")

    [pscustomobject]@{
        PackageManagerInstallScript = [System.Collections.ArrayList]$WindowsPMInstallScript
        ManualInstallScript         = [System.Collections.ArrayList]$WindowsManualInstallScript
        UninstallScript             = [System.Collections.ArrayList]$WindowsUninstallScript
        ConfigurePwshRemotingScript = [System.Collections.ArrayList]$WindowsPwshRemotingScript
    }
}