<#
.SYNOPSIS
    This Script will Install a PSGallery Package Manager
.DESCRIPTION
    This Script will Install a PSGallery Package Manager
    on Windows 10 Local Computer
.NOTES
    File name: NIT.Install-WinGetatPS.ps1
    VERSION: 1.0.0a
    AUTHOR: New Internet Technologies Inc.
    Created:  2025-03-13
    Licensed under the BSD license.
    Please credit me if you find this script useful and do some cool things with it.
.VERSION HISTORY:
    1.0.0 - (2025-03-13) Script created
    1.0.1 - 
#>
param([switch]$Elevated)

#### Set Variables

### Add Global Variables

# Set Script File
$aFile00 = 'System32\reg.exe'

### Set Path
$ProfileFolder="C:\Users\WDAGUtilityAccount"
$CHOCODIR= Join-Path $env:ALLUSERSPROFILE -ChildPath "chocolatey\bin"
$CommonFolder001=Join-Path -Path $ProfileFolder -ChildPath "Desktop\WindowsSandBox"

$scriptDIR = Split-Path -parent $MyInvocation.MyCommand.Definition
$msfRootPath = $CommonFolder001
$msfRootPath = Resolve-Path $msfRootPath

### Set Functions
#

function Test-Admin {
    $currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
    $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
}

### Run Payloads

if ((Test-Admin) -eq $false)  {
    if ($elevated) {
        # tried to elevate, did not work, aborting
    } else {
#        Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -Window Normal -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
        Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -Window Hidden -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
    }
    exit
}

# 'running with full privileges'

Install-PackageProvider -Name NuGet -Force | Out-Null
Install-Module -Name Microsoft.WinGet.Client -Force -Repository PSGallery | Out-Null
Repair-WinGetPackageManager -IncludePrerelease -AllUsers

### Set Derivative Variables
#
Write-Host "Success a Script has executed at $scriptDIR"
Exit 0
