Debian Codegraph @

test/lib/ansible_test/_util/target/setup/bootstrap.ps1

raw
1 using namespace System.IO
2 using namespace System.Net
3 using namespace System.Reflection
4 using namespace System.Text
5
6 [CmdletBinding()]
7 param (
8 [Parameter(Mandatory)]
9 [string]
10 $PowerShellVersion,
11 [Parameter(Mandatory)]
12 [string]
13 $PowerShellDownloadUri
14 )
15
16 $ErrorActionPreference = 'Stop'
17 $ProgressPreference = 'SilentlyContinue'
18
19 $installDir = [Path]::Combine($env:ProgramFiles, "Ansible", "PowerShell", $PowerShellVersion)
20 $pwshExe = [Path]::Combine($installDir, 'pwsh.exe')
21
22 if (Test-Path -LiteralPath $pwshExe) {
23 return $pwshExe
24 }
25
26 # We use the zip installation method to allow side-by-side installs without
27 # affecting existing PowerShell installations.
28 $zipFilename = "PowerShell-$PowerShellVersion.zip"
29 $zipPath = [Path]::Combine([Path]::GetTempPath(), $zipFilename)
30
31 $currentProtocol = [ServicePointManager]::SecurityProtocol
32 if ([SecurityProtocolType].GetMember("Tls13")) {
33 # If the Tls13 member is present we are on .NET Framework 4.8+ so using
34 # the SystemDefault setting will use the OS policies. If it's not set
35 # to SystemDefault already we are running in a PSRemoting WSMan host
36 # and need some reflection to reconfigure the policies to get it to use
37 # the OS policies.
38
39 if ($currentProtocol -ne 'SystemDefault') {
40 # https://learn.microsoft.com/en-us/dotnet/framework/network-programming/tls#switchsystemnetdontenablesystemdefaulttlsversions
41 $disableSystemTlsField = [ServicePointManager].GetField(
42 's_disableSystemDefaultTlsVersions',
43 [BindingFlags]'NonPublic, Static')
44 if ($disableSystemTlsField -and $disableSystemTlsField.GetValue($null)) {
45 $disableSystemTlsField.SetValue($null, $false)
46 }
47
48 [ServicePointManager]::SecurityProtocol = [SecurityProtocolType]::SystemDefault
49 }
50 }
51 else {
52 # We are on .NET 4.7 or older, as TLS 1.2 is the max version we can
53 # use here regardless of the OS, manually enable the protocols known to
54 # the runtime.
55 if ([SecurityProtocolType].GetMember("Tls11")) {
56 $currentProtocol = $currentProtocol -bor [SecurityProtocolType]::Tls11
57 }
58 if ([SecurityProtocolType].GetMember("Tls12")) {
59 $currentProtocol = $currentProtocol -bor [SecurityProtocolType]::Tls12
60 }
61 [ServicePointManager]::SecurityProtocol = $currentProtocol
62 }
63
64 if (-not (Test-Path -LiteralPath $zipPath)) {
65 $attempts = 0
66
67 while ($true) {
68 try {
69 Invoke-WebRequest -Uri $PowerShellDownloadUri -OutFile $zipPath -UseBasicParsing
70 }
71
72 catch {
73 $attempts++
74
75 if ($attempts -gt 5) {
76 throw "Failed to download PowerShell from $PowerShellDownloadUri after $attempts attempts."
77 }
78
79 Start-Sleep -Seconds 5
80 continue
81 }
82
83 break
84 }
85 }
86
87 if (-not (Test-Path -LiteralPath $installDir)) {
88 New-Item -ItemType Directory -Path $installDir -Force | Out-Null
89 }
90
91 Expand-Archive -LiteralPath $zipPath -DestinationPath $installDir -Force
92
93 $null = & $pwshExe -Command "exit"
94
95 if ($LASTEXITCODE -ne 0) {
96 throw "PowerShell installation verification failed with exit code $LASTEXITCODE"
97 }
98
99 return $pwshExe