From 2925b3ae953afa73b1f3df8916944caeeccc668c Mon Sep 17 00:00:00 2001 From: ilia-shipitsin <125650415+ilia-shipitsin@users.noreply.github.com> Date: Fri, 6 Oct 2023 12:03:28 +0200 Subject: [PATCH] [windows] introduce dedicated PowerShell configuration step (#8453) * windows: implement separate step for configuring Powershell * windows: refactor PyPy checksum validation PyPy checksum validation uses PowerHTML module which is now available as a part of powershell profile * reformat * Update images/win/scripts/Installers/Configure-PowerShell.ps1 Co-authored-by: Vasilii Polikarpov <126792224+vpolikarpov-akvelon@users.noreply.github.com> --------- Co-authored-by: Vasilii Polikarpov <126792224+vpolikarpov-akvelon@users.noreply.github.com> --- .../Installers/Configure-PowerShell.ps1 | 29 +++++++++++++++++++ .../Installers/Install-PowerShellModules.ps1 | 8 +---- .../win/scripts/Installers/Install-PyPy.ps1 | 2 -- images/win/windows2019.json | 1 + images/win/windows2022.json | 1 + 5 files changed, 32 insertions(+), 9 deletions(-) create mode 100644 images/win/scripts/Installers/Configure-PowerShell.ps1 diff --git a/images/win/scripts/Installers/Configure-PowerShell.ps1 b/images/win/scripts/Installers/Configure-PowerShell.ps1 new file mode 100644 index 000000000..98cb83055 --- /dev/null +++ b/images/win/scripts/Installers/Configure-PowerShell.ps1 @@ -0,0 +1,29 @@ +################################################################################ +## File: Configure-Powershell.ps1 +## Desc: Manage PowerShell configuration +################################################################################ + +#region System +Write-Host "Setup PowerShellGet" +Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force + +# Specifies the installation policy +Set-PSRepository -InstallationPolicy Trusted -Name PSGallery +#endregion + +#region User (current user, image generation only) +if (-not (Test-Path $profile)) { + New-Item $profile -ItemType File -Force +} + +@" + if ( -not(Get-Module -ListAvailable -Name PowerHTML)) { + Install-Module PowerHTML -Scope CurrentUser + } + + if ( -not(Get-Module -Name PowerHTML)) { + Import-Module PowerHTML + } +"@ | Add-Content -Path $profile -Force + +#endregion diff --git a/images/win/scripts/Installers/Install-PowerShellModules.ps1 b/images/win/scripts/Installers/Install-PowerShellModules.ps1 index f0afb9e6c..b3ab7a35d 100644 --- a/images/win/scripts/Installers/Install-PowerShellModules.ps1 +++ b/images/win/scripts/Installers/Install-PowerShellModules.ps1 @@ -1,12 +1,6 @@ # Set TLS1.2 [Net.ServicePointManager]::SecurityProtocol = [Net.ServicePointManager]::SecurityProtocol -bor "Tls12" -Write-Host "Setup PowerShellGet" -Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force - -# Specifies the installation policy -Set-PSRepository -InstallationPolicy Trusted -Name PSGallery - # Install PowerShell modules $modules = (Get-ToolsetContent).powershellModules @@ -29,4 +23,4 @@ foreach($module in $modules) } Import-Module Pester -Invoke-PesterTests -TestFile "PowerShellModules" -TestName "PowerShellModules" \ No newline at end of file +Invoke-PesterTests -TestFile "PowerShellModules" -TestName "PowerShellModules" diff --git a/images/win/scripts/Installers/Install-PyPy.ps1 b/images/win/scripts/Installers/Install-PyPy.ps1 index fddbf1605..8b570c195 100644 --- a/images/win/scripts/Installers/Install-PyPy.ps1 +++ b/images/win/scripts/Installers/Install-PyPy.ps1 @@ -84,8 +84,6 @@ $toolsetVersions = Get-ToolsetContent | Select-Object -ExpandProperty toolcache $pypyVersions = Invoke-RestMethod https://downloads.python.org/pypy/versions.json # required for html parsing -Install-Module PowerHTML -Scope CurrentUser -Import-Module PowerHTML $checksums = (Invoke-RestMethod -Uri 'https://www.pypy.org/checksums.html' | ConvertFrom-HTML).SelectNodes('//*[@id="content"]/article/div/pre') Write-Host "Starting installation PyPy..." diff --git a/images/win/windows2019.json b/images/win/windows2019.json index 1cb5e2922..07d3a6f02 100644 --- a/images/win/windows2019.json +++ b/images/win/windows2019.json @@ -150,6 +150,7 @@ ], "scripts": [ "{{ template_dir }}/scripts/Installers/Configure-Antivirus.ps1", + "{{ template_dir }}/scripts/Installers/Configure-PowerShell.ps1", "{{ template_dir }}/scripts/Installers/Install-PowerShellModules.ps1", "{{ template_dir }}/scripts/Installers/Install-WindowsFeatures.ps1", "{{ template_dir }}/scripts/Installers/Install-Choco.ps1", diff --git a/images/win/windows2022.json b/images/win/windows2022.json index 7f099c116..db4a671f3 100644 --- a/images/win/windows2022.json +++ b/images/win/windows2022.json @@ -134,6 +134,7 @@ ], "scripts": [ "{{ template_dir }}/scripts/Installers/Configure-Antivirus.ps1", + "{{ template_dir }}/scripts/Installers/Configure-PowerShell.ps1", "{{ template_dir }}/scripts/Installers/Install-PowerShellModules.ps1", "{{ template_dir }}/scripts/Installers/Install-WindowsFeatures.ps1", "{{ template_dir }}/scripts/Installers/Install-Choco.ps1",