Advertisement
Pamiiruq

update-cygwin.ps1

Feb 21st, 2015
591
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Get the ID and security principal of the current user account
  2. $myWindowsID=[System.Security.Principal.WindowsIdentity]::GetCurrent()
  3. $myWindowsPrincipal=new-object System.Security.Principal.WindowsPrincipal($myWindowsID)
  4.  
  5. # Get the security principal for the Administrator role
  6. $adminRole=[System.Security.Principal.WindowsBuiltInRole]::Administrator
  7.  
  8. # Check to see if we are currently running "as Administrator"
  9. if ($myWindowsPrincipal.IsInRole($adminRole)) {
  10.    # We are running "as Administrator" - so change the title and background color to indicate this
  11.    $Host.UI.RawUI.WindowTitle = $myInvocation.MyCommand.Definition + "(Elevated)"
  12.    $Host.UI.RawUI.BackgroundColor = "DarkBlue"
  13.    clear-host
  14. }
  15. else {
  16.    # We are not running "as Administrator" - so relaunch as administrator
  17.    
  18.    # Create a new process object that starts PowerShell
  19.    $newProcess = new-object System.Diagnostics.ProcessStartInfo "PowerShell";
  20.    
  21.    # Specify the current script path and name as a parameter
  22.    $newProcess.Arguments = $myInvocation.MyCommand.Definition;
  23.    
  24.    # Indicate that the process should be elevated
  25.    $newProcess.Verb = "runas";
  26.    
  27.    # Start the new process
  28.    [System.Diagnostics.Process]::Start($newProcess);
  29.    
  30.    # Exit from the current, unelevated, process
  31.    exit
  32. }
  33.  
  34. # Run your code that needs to be elevated here
  35. (new-object System.Net.WebClient).DownloadFile('http://cygwin.com/setup-x86_64.exe','setup-x86_64.exe')
  36.  
  37. if (!$?) {
  38.    Write-Host "Something wrong happened when downloading the Cygwin installer."
  39.    Write-Host -NoNewLine "Press any key to continue..."
  40.    $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
  41.    exit
  42. }
  43.  
  44. $p = Start-Process .\setup-x86_64.exe -ArgumentList "--upgrade-also --quiet-mode" -wait -NoNewWindow -PassThru
  45.  
  46. if ($p.ExitCode -ne 0) {
  47.    Write-Host "Cygwin setup failed with an error!"
  48. }
  49.  
  50. Remove-Item .\setup-x86_64.exe
  51.  
  52. Write-Host -NoNewLine "Press any key to continue..."
  53. $null = $Host.UI.RawUI.ReadKey("NoEcho,IncludeKeyDown")
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement