Skip to content

Instantly share code, notes, and snippets.

@Hexalon
Created June 15, 2016 15:10
Show Gist options
  • Save Hexalon/130ecc69fdb052007c7b70d2b32bcf83 to your computer and use it in GitHub Desktop.
Save Hexalon/130ecc69fdb052007c7b70d2b32bcf83 to your computer and use it in GitHub Desktop.
Automates installation of PowerShell 2 on Server 2008
<#
.NOTES
===========================================================================
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2015 v4.2.89
Created on: 7/30/2015 12:48 PM
Created by: Colin Squier <hexalon@gmail.com
Filename: Install-WMF2.ps1
===========================================================================
.DESCRIPTION
Automates installation of PowerShell 2.
#>
<#
.SYNOPSIS
Returns the path of the executing script's directory.
.DESCRIPTION
Sapien's implementation of the variable $HostInvocation
causes a conflict the with the system's variable.
.EXAMPLE
PS C:\> Get-ScriptDirectory
.NOTES
Work around for handling Sapien's custom host environment.
#>
function Get-ScriptDirectory
{
if ($HostInvocation -ne $null)
{
Split-Path $HostInvocation.MyCommand.path
}
else
{
#$Invocation = (Get-Variable MyInvocation -Scope 1).Value
#Split-Path $Invocation.MyInvocation.path
Split-Path $script:MyInvocation.MyCommand.Path
}
}
If (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
[Security.Principal.WindowsBuiltInRole] "Administrator"))
{
Write-Warning "You do not have Administrator rights to run this script!`nPlease re-run this script as an Administrator!"
Break
}
$RequiredOSVersion = "6.0.6002"
$OS = (Get-WmiObject -Class Win32_OperatingSystem)
$scriptDirectory = Get-ScriptDirectory
$Source = (Split-Path -Path $scriptDirectory -Parent)
$PackagePath = (Join-Path -Path $Source -ChildPath "ComputerSetup\WMF2")
$File32 = "Windows6.0-KB968930-x86.msu"
$File64 = "Windows6.0-KB968930-x64.msu"
$Product = "Windows Management Framework 2"
$FullPath32 = Join-Path -Path $PackagePath -ChildPath $File32
$FullPath64 = Join-Path -Path $PackagePath -ChildPath $File64
$Version = ([Environment]::Version).Major
If ($Version -ge 2)
{
Write-Host "$Product or later already installed." -ForegroundColor Green
break
}
If ([version]$OS.Version -ne $RequiredOSVersion)
{
Write-Host "The user's computer does not meet system requirements." -ForegroundColor Red
break
}
$HotFix = (Get-HotFix | Where-Object { $_.HotfixID -eq 'KB968930' })
if ($HotFix -ne $null)
{
Write-Host "$Product or later already installed." -ForegroundColor Green
break
}
switch ($OS.OSArchitecture)
{
'32-bit' { $InstallPath = $FullPath32 }
'64-bit' { $InstallPath = $FullPath64 }
}
Write-Host "Installing $Product"
$UpdateFile = Start-Process -FilePath "wusa.exe" -ArgumentList "`"$InstallPath`" /quiet /norestart" -Wait -Passthru
$ExitCode = $UpdateFile.ExitCode
Write-Host "$Product installer exit code: $ExitCode"
switch ($ExitCode)
{
0 { Write-Host "Installation completed successfully." -ForegroundColor Green }
1602 { Write-Host "The user canceled installation." -ForegroundColor White }
1603 { Write-Host "A fatal error occurred during installation." -ForegroundColor Red }
1641 { Write-Host "A restart is required to complete the installation. This message indicates success." -ForegroundColor Yellow }
3010 { Write-Host "A restart is required to complete the installation. This message indicates success." -ForegroundColor Yellow }
5100 { Write-Host "The user's computer does not meet system requirements." -ForegroundColor Red }
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment