Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- <#
- This script checks for the PowerShell version, and if it's not 4, downloads and installs PS 4.0. Uses the PS 2 download function instead of the usual one.
- You need both the x64 and x86 versions of Windows Management Framework 4.0, and the .Net Framework 4.5.1 from the URLs you specify below.
- I have a private URL where I put downloads for my scripts, so I download them from MS and upload them to my FTP server. You may be able to download directly from MS in the scripts.
- You can download them from here:
- Windows Management Framework 4.0: http://www.microsoft.com/en-us/download/details.aspx?id=40855
- .Net Framework 4.5.1: http://www.microsoft.com/en-us/download/details.aspx?id=40779
- #>
- #Set these variables to the URL to download .Net 4.5 and MS Management. Don't fill in the filenames and don't put a trailing slash.
- $DotNetUrl = "http://yoururl.com"
- $WMFUrl = "http://yoururl.com"
- #Functions start here
- #**********************************************************************************************************************************************
- #PS 2.0 download function
- Function get-FileFromUri {
- # This function downloads a file in PowerShell 2.0.
- # Example: get-FileFromUri http://example.com/url/of/example/file C:\example-folder
- param(
- [parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
- [string]
- [Alias('Uri')]
- $Url,
- [parameter(Mandatory=$false, Position=1)]
- [string]
- [Alias('Folder')]
- $FolderPath
- )
- process {
- try {
- # resolve short URLs
- $req = [System.Net.HttpWebRequest]::Create($Url)
- $req.Method = "HEAD"
- $response = $req.GetResponse()
- $fUri = $response.ResponseUri
- $filename = [System.IO.Path]::GetFileName($fUri.LocalPath);
- $response.Close()
- # download file
- $destination = (Get-Item -Path ".\" -Verbose).FullName
- if($FolderPath) { $destination = $FolderPath }
- if ($destination.EndsWith('\')) {
- $destination += $filename
- } else {
- $destination += '\' + $filename
- }
- $webclient = New-Object System.Net.webclient
- $webclient.downloadfile($fUri.AbsoluteUri, $destination)
- write-host -ForegroundColor DarkGreen "downloaded '$($fUri.AbsoluteUri)' to '$($destination)'"
- } catch {
- write-host -ForegroundColor DarkRed $_.Exception.Message
- }
- }
- }
- #*****************************************************************************************************************************************************
- #End of functions
- #Script Start
- #Determines Powershell version. Exits the script if it's 4.0
- if ($PSVersionTable.PSVersion.Major -ge 4) {
- $Date = Get-Date
- Write-Host "$Date - Powershell upgrade not needed. Exiting script."
- Exit
- }
- #Determines if DotNet 4.5 is installed, downloads and installs if it is not.
- $DotNetVer = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Client").Version
- If ($DotNetVer -lt 4.5){
- $FileUrl = "$DotNetUrl/NDP451-KB2858728-x86-x64-AllOS-ENU.exe"
- $FileDest = "$env:Temp"
- $Date = Get-Date
- Write-Host "$Date - Downloading the .Net Framework 4.5 installer"
- Get-FileFromUri $FileUrl $FileDest
- Set-Location "$env:Temp"
- $Date = Get-Date
- Write-Host "$Date - Installing .Net Framework 4.5. This can take some time. The script will continue when it's finished."
- .\NDP451-KB2858728-x86-x64-AllOS-ENU.exe /q /norestart | Out-Null
- #Checks to make sure it installed, exits with an error if not
- $DotNetVer = (Get-ItemProperty -Path "HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Client").Version
- If ($DotNetVer -lt 4.5){
- $Date = Get-Date
- Write-Host "$Date - Install of Dotnet Framework 4.5 failed with error code: $LastExitCode - Exiting the script."
- Exit 1000
- }
- Else {
- $Date = Get-Date
- Write-Host "$Date - Install of Dotnet Framework 4.5 succeeded"
- }
- }
- #Determines the architecture of the machine
- $Arch = (Get-WmiObject Win32_OperatingSystem).OSArchitecture
- #Sets the name of the Windows Management Framework installed based on architecture
- If($Arch -eq "64-Bit") {$WMF = "Windows6.1-KB2819745-x64-MultiPkg.msu"}
- Else {$WMF = "Windows6.1-KB2819745-x86-MultiPkg.msu"}
- #Downloads and silently installs Windows Management Package
- $FileUrl = "$WMFUrl/$WMF"
- $FileDest = "$env:Temp"
- $Date = Get-Date
- Write-Host "$Date - Downloading the Windows Management Package installer."
- Get-FileFromUri $FileUrl $FileDest
- Set-Location "$env:Temp"
- $Date = Get-Date
- Write-Host "$Date - Installing the Windows Management Package."
- wusa.exe "$WMF" /quiet /norestart | Out-Null
- #Checks to see if the installed succeeded. When it succeeds, it returns error 3010, which means the computer needs to be rebooted.
- If($LastExitCode -eq 3010) {
- $Date = Get-Date
- Write-Host "$Date - The install of Powershell 4.0 finished. The workstation will need to be rebooted before the changes apply."
- }
- Else {
- $Date = Get-Date
- Write-Host "$Date - The install of Powershell 4.0 failed with error code: $LastExitCode - You may need to manually install."
- Exit 1000
- }
Advertisement
Add Comment
Please, Sign In to add comment