Last active
August 6, 2022 04:09
-
-
Save quonic/c9ade3a2f5ee24959306608a5d3cd063 to your computer and use it in GitHub Desktop.
Installs PowerShell/WMF 5.1 for applicable versions of Windows running on PowerShell 2.0 to 4.0. Includes script for getting PowerShell version for NinjaRMM.
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 -Version 2.0 | |
<# | |
.SYNOPSIS | |
Returns the version of PowerShell installed and updates a custom field in NinjaRMM | |
.DESCRIPTION | |
Returns the version of PowerShell installed and updates a custom field in NinjaRMM if the Ninja Agent is installed. | |
.PARAMETER CustomField | |
Sets what custom field to update. | |
Defaults to "PowerShell" if not used. | |
.EXAMPLE | |
-CustomField PowerShellVersion | |
#> | |
[CmdletBinding()] | |
param( | |
[string] | |
$CustomField = "PowerShell" | |
) | |
begin {} | |
process { | |
$Version = "$($PSVersionTable.PSVersion.Major).$($PSVersionTable.PSVersion.Minor)" | |
if ($(Get-Command "Ninja-Property-Set" -ErrorAction SilentlyContinue).Count) { | |
Ninja-Property-Set "$CustomField" "$Version" | |
} | |
Write-Output "PowerShell: $Version" | |
} | |
end {} |
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 -Version 2.0 | |
# Installs PowerShell/WMF 5.1 for applicable versions of Windows running on PowerShell 2.0 to 4.0 | |
# Based on: | |
# https://github.com/MicrosoftDocs/PowerShell-Docs/blob/main/reference/docs-conceptual/windows-powershell/wmf/setup/install-configure.md | |
function Test-Win64() { return [IntPtr]::size -eq 8 } | |
function Test-Win32() { return [IntPtr]::size -eq 4 } | |
function Expand-ZIPFile($File, $Destination) { | |
$shell = New-Object -ComObject shell.application | |
$zip = $shell.NameSpace($File) | |
foreach ($item in $zip.items()) { | |
$shell.Namespace($Destination).copyhere($item) | |
} | |
} | |
$OSVersion = [System.Environment]::OSVersion.Version | |
$Bitness = if (Test-Win64) { "64" }elseif (Test-Win32) { "32" }else { Write-Error "Unknown CPU Arch"; exit 1 } | |
if ($PSVersionTable -ge [Version]::new(5.1)) { Write-Host "Already running PowerShell 5.1"; exit 0 } | |
if ($OSVersion -ge [Version]::new(6, 4)) { Write-Host "Nothing to do."; exit 0 } | |
$TempFolder = $env:TEMP | |
$DotNetUrl = "https://go.microsoft.com/fwlink/?LinkId=397708" | |
$DotNetFile = "NDP452-KB2901907-x86-x64-AllOS-ENU.exe" | |
$ProductName = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\Windows NT\CurrentVersion").ProductName | |
$DotNetRelease = (Get-ItemProperty 'HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Full' -Name Release).Release | |
$IsDesktop = if ($ProductName -like "*Server*") { $false }else { $true } | |
$IsServer = if ($ProductName -like "*Server*") { $true }else { $false } | |
# List of download options | |
$Downloads = @( | |
[PSCustomObject]@{ | |
Url = "https://go.microsoft.com/fwlink/?linkid=839523" | |
Bit = "64" | |
File = "Win7AndW2K8R2-KB3191566-x64.zip" | |
DesktopMajor = 6 | |
DesktopMinor = 1 | |
DesktopMinBuild = 7601 | |
IsDesktop = $true | |
IsServer = $true | |
} | |
[PSCustomObject]@{ | |
Url = "https://go.microsoft.com/fwlink/?linkid=839522" | |
Bit = "32" | |
File = "Win7-KB3191566-x86.zip" | |
DesktopMajor = 6 | |
DesktopMinor = 1 | |
DesktopMinBuild = 7601 | |
IsDesktop = $true | |
IsServer = $false | |
} | |
[PSCustomObject]@{ | |
Url = "https://go.microsoft.com/fwlink/?linkid=839516" | |
Bit = "64" | |
File = "Win8.1AndW2K12R2-KB3191564-x64.msu" | |
DesktopMajor = 6 | |
DesktopMinor = 3 | |
DesktopMinBuild = 7601 | |
IsDesktop = $true | |
IsServer = $true | |
} | |
[PSCustomObject]@{ | |
Url = "https://go.microsoft.com/fwlink/?linkid=839521" | |
Bit = "32" | |
File = "Win8.1-KB3191564-x86.msu" | |
DesktopMajor = 6 | |
DesktopMinor = 3 | |
DesktopMinBuild = 7601 | |
IsDesktop = $true | |
IsServer = $false | |
} | |
[PSCustomObject]@{ | |
Url = "https://go.microsoft.com/fwlink/?linkid=839513" | |
Bit = "64" | |
File = "W2K12-KB3191565-x64.msu" | |
DesktopMajor = 6 | |
DesktopMinor = 2 | |
DesktopMinBuild = 9200 | |
IsDesktop = $true | |
IsServer = $false | |
} | |
[PSCustomObject]@{ | |
Url = "https://go.microsoft.com/fwlink/?linkid=839516" | |
Bit = "64" | |
File = "Win8.1AndW2K12R2-KB3191564-x64.msu" | |
DesktopMajor = 6 | |
DesktopMinor = 3 | |
DesktopMinBuild = 9600 | |
IsDesktop = $true | |
IsServer = $true | |
} | |
) | |
# Install 4.5.2 if current version is older | |
if ($DotNetRelease -lt 379893) { | |
Write-Host "Downloading .Net 4.5.2" | |
$wc = New-Object System.Net.WebClient | |
$output = "$TempFolder\$DotNetFile" | |
$wc.DownloadFile($DotNetUrl, $output) | |
Write-Host "Installing .Net 4.5.2" | |
& $output /q /norestart | |
} | |
# Select the correct installer for our OS | |
$SelectedInstall = $Downloads | Where-Object { | |
$_.Bit -like $Bitness -and | |
$_.DesktopMajor -ge $OSVersion.Major -and | |
$_.DesktopMinor -ge $OSVersion.Minor -and | |
$_.DesktopMinBuild -ge $OSVersion.Build -and | |
(if ($IsDesktop) { $_.IsDesktop -eq $IsDesktop }elseif($IsServer) { $_.IsServer -eq $IsServer }) | |
} | Select-Object -First 1 | |
# Change directory to $TempFolder | |
Push-Location -Path $TempFolder | |
Write-Host "Downloading WMF 5.1" | |
$wc = New-Object System.Net.WebClient | |
$wc.DownloadFile($SelectedInstall.Url, "$TempFolder\$($SelectedInstall.File)") | |
if ($SelectedInstall.File -like "*zip*") { | |
Write-Host "Expanding Zip to $TempFolder" | |
Expand-ZIPFile -File "$TempFolder\$($SelectedInstall.File)" -Destination "$TempFolder\" | |
Write-Host "Installing WMF 5.1" | |
& $TempFolder\Install-WMF5.1.ps1 -AcceptEULA | |
} | |
else { | |
Write-Host "Installing WMF 5.1" | |
& "$TempFolder\$($SelectedInstall.File)" /quiet /norestart | |
} | |
if ($LASTEXITCODE -eq 0) { | |
Write-Host "Installed WMF 5.1!" | |
} | |
else { | |
Write-Host "Failed to install WMF 5.1!" | |
Write-Host "Installer returned exit code of $LASTEXITCODE" | |
} | |
exit $LASTEXITCODE | |
# Go back to previous directory | |
Pop-Location |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment