Created
June 14, 2024 10:48
-
-
Save indented-automation/66f0e1afa902fe1548e679f1d3d671df to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Requires -PSEdition Desktop | |
[CmdletBinding()] | |
param ( | |
[ValidateSet('AllUsers', 'CurrentUser')] | |
[string] | |
$Scope = 'CurrentUser' | |
) | |
$ErrorActionPreference = 'Stop' | |
[System.Net.ServicePointManager]::SecurityProtocol = [System.Net.ServicePointManager]::SecurityProtocol -bor 'Tls12' | |
if ($Scope -eq 'AllUsers') { | |
$basePath = $env:ProgramFiles | |
} else { | |
$basePath = [Environment]::GetFolderPath('MyDocuments') | |
} | |
$modulePath = Join-Path -Path $basePath -ChildPath 'WindowsPowerShell\Modules' | |
$packages = @( | |
@{ Name = 'PackageManagement'; Version = '1.4.8.1' } | |
@{ Name = 'PowerShellGet'; Version = '2.2.5' } | |
) | |
foreach ($package in $packages) { | |
try { | |
$params = @{ | |
Uri = 'https://psg-prod-eastus.azureedge.net/packages/{0}.{1}.nupkg' -f @( | |
$package['Name'].ToLower() | |
$package['Version'] | |
) | |
OutFile = Join-Path -Path $env:TEMP -ChildPath ('{0}.zip' -f $package['Name']) | |
} | |
Write-Verbose ('Downloading {0} from {1}' -f $package['Name'], $params['Uri']) | |
Invoke-WebRequest @params | |
$params = @{ | |
Path = $params['OutFile'] | |
DestinationPath = Join-Path -Path $modulePath -ChildPath $package['Name'] | | |
Join-Path -ChildPath $package['Version'] | |
Force = $true | |
} | |
if (-not (Test-Path -LiteralPath $params['DestinationPath'])) { | |
New-Item -Path $params['DestinationPath'] -ItemType Directory | |
} | |
Write-Verbose ('Extracting {0} to {1}' -f $package['Name'], $params['DestinationPath']) | |
Expand-Archive @params | |
Remove-Item -LiteralPath $params['Path'] | |
Write-Verbose ('Cleaning package {0}' -f $package['Name']) | |
$params = @{ | |
Path = $params['DestinationPath'] | |
Depth = 0 | |
Include = @( | |
'_rels' | |
'*Content_Types*' | |
'package' | |
'*.nuspec' | |
) | |
} | |
Get-ChildItem @params | Remove-Item -Recurse | |
} catch { | |
Write-Error -ErrorRecord $_ | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment