Skip to content

Instantly share code, notes, and snippets.

@jasonrush
Last active May 14, 2020 03:02
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jasonrush/b4d8b768eac8e3bd0f83c522b1de7987 to your computer and use it in GitHub Desktop.
Save jasonrush/b4d8b768eac8e3bd0f83c522b1de7987 to your computer and use it in GitHub Desktop.
Install or upgrade Pester (including PowerShell 2.0 on Windows 7 and Server 2008 r2)
Write-Output "=== Checking if Pester module is installed ==="
if ( (get-module -ListAvailable pester).count -gt 0 ) {
write-Output "`tPester is installed. Checking version(s)..."
if ( (get-module -ListAvailable pester | where-object { $_.version.Major -ge 4} ).count -eq 0 ) {
write-Output "`tReinstalling latest version of Pester"
$module = "C:\Program Files\WindowsPowerShell\Modules\Pester"
takeown /F $module /A /R
icacls $module /reset
icacls $module /grant "*S-1-5-32-544:F" /inheritance:d /T
Remove-Item -Path $module -Recurse -Force -Confirm:$false
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
Install-Module -Name Pester -Force
}
} else {
write-Output "`tPester is not installed. Installing..."
if( $psversiontable.psversion -ge [version]'5.0') {
Install-PackageProvider -Name NuGet -MinimumVersion 2.8.5.201 -Force
Install-Module -Name Pester -Force
}else{
# Set to use TLS1.2
$p = [Enum]::ToObject([System.Net.SecurityProtocolType], 3072);
[System.Net.ServicePointManager]::SecurityProtocol = $p;
# Download PsGet module (to get Install-Module functionality in PowerShell 2.0)
$tempPath = 'C:\source\temp\psget'
New-Item $tempPath -ItemType Directory -Force | Out-Null
$client = (New-Object Net.WebClient)
$client.DownloadFile('https://raw.githubusercontent.com/psget/psget/master/PsGet/PsGet.psm1', "$tempPath\PsGet.psm1")
Import-Module "$tempPath\PsGet.psm1"
Install-Module -Module Pester
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment