Skip to content

Instantly share code, notes, and snippets.

@Hexalon
Created March 15, 2016 19:48
Show Gist options
  • Save Hexalon/41060f32d4058b42ad8d to your computer and use it in GitHub Desktop.
Save Hexalon/41060f32d4058b42ad8d to your computer and use it in GitHub Desktop.
Automates .NET Framework 3.5 installation on Win10
<#
.NOTES
===========================================================================
Created with: SAPIEN Technologies, Inc., PowerShell Studio 2016 v5.2.116
Created on: 3/9/2016 13:59
Created by: Colin Squier <hexalon@gmail.com>
Filename: Install-NetFx3.Win10.ps1
===========================================================================
.DESCRIPTION
Automates .NET Framework 3.5 installation on Win10. The script will
mount a Windows 10 ISO from the same directory as the script. Tested on
the GA release of Windows 10 and version 1511. Script will produce no
non-error output unless verbose output is requested.
#>
[CmdletBinding()]
Param ()
<#
.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
{
Split-Path $script:MyInvocation.MyCommand.Path
}
}
if (!([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole(`
[Security.Principal.WindowsBuiltInRole] "Administrator"))
{
$Arguments = "& '" + $MyInvocation.MyCommand.Definition + "'"
Start-Process powershell -Verb RunAs -ArgumentList $Arguments
Break
}
$RequiredOSVersion = "10.0.10240" #RTM version of Windows 10
$Option = New-CimSessionOption -Protocol Dcom
$Session = New-CimSession -SessionOption $Option -ComputerName $env:COMPUTERNAME
$OS = (Get-CimInstance -ClassName Win32_OperatingSystem -CimSession $Session)
$OSName = $OS.Caption
$OSVersion = $OS.Version
$OSProductType = $OS.ProductType
$OSArch = $OS.OSArchitecture
$scriptDirectory = Get-ScriptDirectory
Write-Verbose -Message "OS edition: $OSName`r OS version: $OSVersion`r OS architecture: $OSArch`r OS product type: $OSProductType"
if (([version]$OSVersion -ge $RequiredOSVersion) -and ($OSProductType -eq 1))
{
$NetFx35 = (Get-WindowsOptionalFeature -FeatureName NetFx3 -Online)
if ($NetFx35.State -ne "Enabled")
{
$WindowsISO = "SW_DVD5_WIN_ENT_10_64BIT_English_MLF_X20-26061.ISO"
$WindowsISOFullPath = Join-Path -Path $scriptDirectory -ChildPath $WindowsISO
Write-Verbose "Installing .NET Framework 3.5"
Write-Verbose "Mounting Windows ISO"
Mount-DiskImage -ImagePath $WindowsISOFullPath -PassThru
$DriveLetter = ((Get-DiskImage $WindowsISOFullPath | Get-Volume).DriveLetter)
$DriveLetter = $DriveLetter + ":"
$FullWindowsSourcePath = Join-Path -Path $DriveLetter -ChildPath "Sources\SxS"
Enable-WindowsOptionalFeature -Online -FeatureName NetFx3 -All -LimitAccess -Source $FullWindowsSourcePath
Write-Verbose "Dismounting Windows ISO"
Dismount-DiskImage -ImagePath $WindowsISOFullPath
}
else
{
Write-Verbose ".Net Framework 3.5 already installed, skipping."
}
}
else
{
Write-Error -Message "The operating system does not meet system requirements." -Category InvalidOperation
}
Remove-CimSession -CimSession $Session
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment