daileycomputer

Install PowerShell 4.0

Dec 4th, 2014
945
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. <#
  2. 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.
  3.  
  4. 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.
  5. 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.
  6.  
  7. You can download them from here:
  8. Windows Management Framework 4.0: http://www.microsoft.com/en-us/download/details.aspx?id=40855
  9. .Net Framework 4.5.1: http://www.microsoft.com/en-us/download/details.aspx?id=40779
  10. #>
  11.  
  12. #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.
  13. $DotNetUrl = "http://yoururl.com"
  14. $WMFUrl = "http://yoururl.com"
  15.  
  16. #Functions start here
  17. #**********************************************************************************************************************************************
  18.  
  19. #PS 2.0 download function
  20. Function get-FileFromUri {
  21. # This function downloads a file in PowerShell 2.0.
  22. # Example: get-FileFromUri http://example.com/url/of/example/file C:\example-folder
  23. param(
  24. [parameter(Mandatory=$true, Position=0, ValueFromPipeline=$true, ValueFromPipelineByPropertyName=$true)]
  25. [string]
  26. [Alias('Uri')]
  27. $Url,
  28. [parameter(Mandatory=$false, Position=1)]
  29. [string]
  30. [Alias('Folder')]
  31. $FolderPath
  32. )
  33. process {
  34. try {
  35. # resolve short URLs
  36. $req = [System.Net.HttpWebRequest]::Create($Url)
  37. $req.Method = "HEAD"
  38. $response = $req.GetResponse()
  39. $fUri = $response.ResponseUri
  40. $filename = [System.IO.Path]::GetFileName($fUri.LocalPath);
  41. $response.Close()
  42. # download file
  43. $destination = (Get-Item -Path ".\" -Verbose).FullName
  44. if($FolderPath) { $destination = $FolderPath }
  45. if ($destination.EndsWith('\')) {
  46. $destination += $filename
  47. } else {
  48. $destination += '\' + $filename
  49. }
  50. $webclient = New-Object System.Net.webclient
  51. $webclient.downloadfile($fUri.AbsoluteUri, $destination)
  52. write-host -ForegroundColor DarkGreen "downloaded '$($fUri.AbsoluteUri)' to '$($destination)'"
  53. } catch {
  54. write-host -ForegroundColor DarkRed $_.Exception.Message
  55. }
  56. }
  57. }
  58. #*****************************************************************************************************************************************************
  59. #End of functions
  60.  
  61. #Script Start
  62.  
  63. #Determines Powershell version. Exits the script if it's 4.0
  64. if ($PSVersionTable.PSVersion.Major -ge 4) {
  65. $Date = Get-Date
  66. Write-Host "$Date - Powershell upgrade not needed. Exiting script."
  67. Exit
  68. }
  69.  
  70. #Determines if DotNet 4.5 is installed, downloads and installs if it is not.
  71. $DotNetVer = (Get-ItemProperty -Path  "HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Client").Version
  72. If ($DotNetVer -lt 4.5){
  73.     $FileUrl = "$DotNetUrl/NDP451-KB2858728-x86-x64-AllOS-ENU.exe"
  74.     $FileDest = "$env:Temp"
  75.     $Date = Get-Date
  76.     Write-Host "$Date - Downloading the .Net Framework 4.5 installer"
  77.     Get-FileFromUri $FileUrl $FileDest
  78.     Set-Location "$env:Temp"
  79.     $Date = Get-Date
  80.     Write-Host "$Date - Installing .Net Framework 4.5. This can take some time. The script will continue when it's finished."
  81.     .\NDP451-KB2858728-x86-x64-AllOS-ENU.exe /q /norestart | Out-Null
  82.     #Checks to make sure it installed, exits with an error if not
  83.     $DotNetVer = (Get-ItemProperty -Path  "HKLM:\SOFTWARE\Microsoft\NET Framework Setup\NDP\v4\Client").Version
  84.     If ($DotNetVer -lt 4.5){
  85.         $Date = Get-Date
  86.         Write-Host "$Date - Install of Dotnet Framework 4.5 failed with error code: $LastExitCode - Exiting the script."
  87.         Exit 1000
  88.         }
  89.     Else {
  90.     $Date = Get-Date
  91.     Write-Host "$Date - Install of Dotnet Framework 4.5 succeeded"
  92.     }
  93. }
  94.  
  95. #Determines the architecture of the machine
  96. $Arch = (Get-WmiObject Win32_OperatingSystem).OSArchitecture
  97. #Sets the name of the Windows Management Framework installed based on architecture
  98. If($Arch -eq "64-Bit") {$WMF = "Windows6.1-KB2819745-x64-MultiPkg.msu"}
  99.     Else {$WMF = "Windows6.1-KB2819745-x86-MultiPkg.msu"}
  100.  
  101. #Downloads and silently installs Windows Management Package
  102. $FileUrl = "$WMFUrl/$WMF"
  103. $FileDest = "$env:Temp"
  104. $Date = Get-Date
  105. Write-Host "$Date - Downloading the Windows Management Package installer."
  106. Get-FileFromUri $FileUrl $FileDest
  107. Set-Location "$env:Temp"
  108. $Date = Get-Date
  109. Write-Host "$Date - Installing the Windows Management Package."
  110. wusa.exe "$WMF" /quiet /norestart | Out-Null
  111.  
  112. #Checks to see if the installed succeeded. When it succeeds, it returns error 3010, which means the computer needs to be rebooted.
  113. If($LastExitCode -eq 3010) {
  114. $Date = Get-Date
  115. Write-Host "$Date - The install of Powershell 4.0 finished. The workstation will need to be rebooted before the changes apply."
  116. }
  117.     Else {
  118.     $Date = Get-Date
  119.     Write-Host "$Date - The install of Powershell 4.0 failed with error code: $LastExitCode - You may need to manually install."
  120.     Exit 1000
  121.     }
Advertisement
Add Comment
Please, Sign In to add comment