Advertisement
Lynngineer

boxstarter-p50.ps1

Mar 26th, 2019
117
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. function Get-Boxstarter {
  2.     # . { iwr -useb https://boxstarter.org/bootstrapper.ps1 } | iex; get-boxstarter -Force
  3.     #
  4.     # . C:\downloads_temp\bootstrapper-filename.ps1; iex Get-Boxstarter -Force
  5.         Param(
  6.             [string] $Version = "2.12.0",
  7.             [switch] $Force
  8.         )
  9.  
  10.         $bootstrapperFile = ${function:Get-Boxstarter}.File
  11.         Write-Host "#############################################################################################"
  12.         Write-Host = "RUNNING: $bootstrapperFile"
  13.         Write-Host "#############################################################################################"
  14.  
  15.         if(!(Test-Admin)) {
  16.             # $bootstrapperFile = ${function:Get-Boxstarter}.File
  17.             if($bootstrapperFile) {
  18.                 Write-Host "User is not running with administrative rights. Attempting to elevate..."
  19.                 $command = "-ExecutionPolicy bypass -noexit -command . '$bootstrapperFile';Get-Boxstarter $($args)"
  20.                 Start-Process powershell -verb runas -argumentlist $command
  21.             }
  22.             else {
  23.                 Write-Host "User is not running with administrative rights.`nPlease open a PowerShell console as administrator and try again."
  24.             }
  25.             return
  26.         }
  27.  
  28.         $badPolicy = $false
  29.         @("Restricted", "AllSigned") | ? { $_ -eq (Get-ExecutionPolicy).ToString() } | % {
  30.             Write-Host "Your current PowerShell Execution Policy is set to '$(Get-ExecutionPolicy)' and will prohibit boxstarter from operating properly."
  31.             Write-Host "Please use Set-ExecutionPolicy to change the policy to RemoteSigned or Unrestricted."
  32.             $badPolicy = $true
  33.         }
  34.         if($badPolicy) { return }
  35.  
  36.         Write-Output "Welcome to the Boxstarter Module installer!"
  37.         if(Check-Chocolatey -Force:$Force){
  38.  
  39.             $message = "Chocolatey extensions..."
  40.             Write-Host $message
  41.             # configure license correctly
  42.             # https://chocolatey.org/docs/installation-licensed
  43.             # https://chocolatey.org/docs/features-install-directory-override
  44.             $command = "choco install chocolatey.extension --install-directory=$env:ChocolateyToolsLocation -y"
  45.             Invoke-Expression $command
  46.  
  47.             # Create python directory. Install to it.
  48.             # New-Item c:\python -Force -type directory | Out-Null
  49.             # $command = "choco install python --install-directory=c:\python -y"
  50.             # Invoke-Expression $command
  51.  
  52.             $command = "choco list -lo"
  53.             Invoke-Expression $command
  54.  
  55.             # Write-Output "Chocolatey installed, licensed, and configured. Boxstarter modules commented out."
  56.             # Write-Output "Fin"
  57.  
  58.             Write-Output "Chocolatey installed, Installing Boxstarter Modules."
  59.     # Alt+Shift, Shift+#
  60.             $env:chocolateyVersion = 0.10.11
  61.     #        $chocoVersion  = "2.9.17"
  62.            try {
  63.                New-Object -TypeName Version -ArgumentList $chocoVersion.split('-')[0] | Out-Null
  64.                $command = "cinst Boxstarter -y"
  65.            }
  66.            catch{
  67.                # if there is no -v then its an older version with no -y
  68.                $command = "cinst Boxstarter"
  69.            }
  70.            $command += " --version $version"
  71.            Invoke-Expression $command
  72.            Import-Module "$env:ProgramData\boxstarter\boxstarter.chocolatey\boxstarter.chocolatey.psd1" -Force
  73.            $Message = "Boxstarter Module Installer completed"
  74.  
  75.  
  76.         }
  77.         else {
  78.             $Message = "Did not detect Chocolatey and unable to install. Installation of Boxstarter has been aborted."
  79.         }
  80.         if($Force) {
  81.             Write-Host $Message
  82.         }
  83.         else {
  84.             Read-Host $Message
  85.         }
  86.     }
  87.  
  88.     function Check-Chocolatey {
  89.         Param(
  90.             [switch] $Force
  91.         )
  92.  
  93.         $dirChocolateyInstall = "c:\choco"
  94.         $dirChocolateyBinRoot = "$dirChocolateyInstall"
  95.         $dirChocolateyLicense = "$dirChocolateyInstall\license"
  96.         $dirChocolateyToolsLocation = "c:\tools"
  97.         $fileChocolateyLicense = "$dirChocolateyLicense\chocolatey.license.xml"
  98.  
  99.         if(-not $env:ChocolateyInstall){
  100.             $env:ChocolateyInstall = "c:\choco"
  101.             if(-not (Test-Path "$env:ChocolateyInstall")){
  102.                 New-Item $env:ChocolateyInstall -Force -type directory | Out-Null
  103.             }
  104.         }
  105.         if(-not $env:ChocolateyBinRoot){
  106.             $env:ChocolateyBinRoot = "$dirChocolateyBinRoot"
  107.         }
  108.         if(-not $env:ChocolateyToolsLocation){
  109.             $env:ChocolateyToolsLocation = "$dirChocolateyToolsLocation"
  110.             if(-not (Test-Path "$env:ChocolateyToolsLocation")){
  111.                 New-Item $env:ChocolateyToolsLocation -Force -type directory | Out-Null
  112.             }
  113.         }
  114.         if(-not (Test-Path "$fileChocolateyLicense")){
  115.             Write-Host "COPY IN THE LICENSE"
  116.             # Copy chocolatey license file
  117.             Copy-Item -Path C:\downloads_temp\chocolatey.license.xml -Destination $fileChocolateyLicense
  118.         }
  119.         # TODO: Add cygwin, etc
  120.  
  121.  
  122.         if(-not $env:ChocolateyInstall -or -not (Test-Path "$env:ChocolateyInstall\bin\choco.exe")){
  123.  
  124.             $message = "Chocolatey not installed. Also .NET Framework Version 4 or greater."
  125.             Write-Host $message
  126.             if($Force -OR (Confirm-Install)){
  127.                 $exitCode = Enable-Net40
  128.                 if($exitCode -ne 0) {
  129.                     Write-Warning ".net install returned $exitCode. You likely need to reboot your computer before proceeding with the install."
  130.                     return $false
  131.                 }
  132.  
  133.                 $message = "WOULD BE INSTALLING CHOCOLATEY"
  134.                 Write-Host $message
  135.  
  136.                 # $message = "Chocolatey downloading..."
  137.                 # Write-Host $message
  138.                 # $url="https://chocolatey.org/api/v2/package/chocolatey/"
  139.                 # $wc=new-object net.webclient
  140.                 # $wp=[system.net.WebProxy]::GetDefaultProxy()
  141.                 # $wp.UseDefaultCredentials=$true
  142.                 # $wc.Proxy=$wp
  143.  
  144.                 # $message = "Chocolatey installing..."
  145.                 # Write-Host $message
  146.                 # # install chocolatey
  147.                 # iex ($wc.DownloadString("https://chocolatey.org/install.ps1"))
  148.  
  149.                 # $message = "Chocolatey added to bin.."
  150.                 # Write-Host $message
  151.                 # # add to path
  152.                 # $env:path="$env:path;$env:ChocolateyInstall\bin"
  153.                 # Write-Host "env: $env:path"
  154.  
  155.             }
  156.             else{
  157.                 return $false
  158.             }
  159.         }
  160.         return $true
  161.     }
  162.  
  163.     function Is64Bit {  [IntPtr]::Size -eq 8  }
  164.  
  165.     function Enable-Net40 {
  166.         Write-Host "Enable-Net40"
  167.         if(Is64Bit) {$fx="framework64"} else {$fx="framework"}
  168.         if(!(test-path "$env:windir\Microsoft.Net\$fx\v4.0.30319")) {
  169.             Write-Host "Downloading .net 4.5..."
  170.             Get-HttpToFile "https://download.microsoft.com/download/b/a/4/ba4a7e71-2906-4b2d-a0e1-80cf16844f5f/dotnetfx45_full_x86_x64.exe" "$env:temp\net45.exe"
  171.             # Write-Host "Installing .net 4.5..."
  172.             # $pinfo = New-Object System.Diagnostics.ProcessStartInfo
  173.             # $pinfo.FileName = "$env:temp\net45.exe"
  174.             # $pinfo.Verb="runas"
  175.             # $pinfo.Arguments = "/quiet /norestart /log $env:temp\net45.log"
  176.             # $p = New-Object System.Diagnostics.Process
  177.             # $p.StartInfo = $pinfo
  178.             # $p.Start() | Out-Null
  179.             # $p.WaitForExit()
  180.             # $e = $p.ExitCode
  181.             if($e -ne 0){
  182.                 Write-Host "Installer exited with $e"
  183.             }
  184.             return $e
  185.         }
  186.         return 0
  187.     }
  188.  
  189.     function Get-HttpToFile ($url, $file){
  190.         Write-Verbose "Downloading $url to $file"
  191.         if(Test-Path $file){Remove-Item $file -Force}
  192.         $downloader=new-object net.webclient
  193.         $wp=[system.net.WebProxy]::GetDefaultProxy()
  194.         $wp.UseDefaultCredentials=$true
  195.         $downloader.Proxy=$wp
  196.         try {
  197.             $downloader.DownloadFile($url, $file)
  198.         }
  199.         catch{
  200.             if($VerbosePreference -eq "Continue"){
  201.                 Write-Error $($_.Exception | fl * -Force | Out-String)
  202.             }
  203.             throw $_
  204.         }
  205.     }
  206.  
  207.     function Confirm-Install {
  208.         $caption = "Installing Chocolatey"
  209.         $message = "Do you want to proceed?"
  210.         $yes = new-Object System.Management.Automation.Host.ChoiceDescription "&Yes","Yes";
  211.         $no = new-Object System.Management.Automation.Host.ChoiceDescription "&No","No";
  212.         $choices = [System.Management.Automation.Host.ChoiceDescription[]]($yes,$no);
  213.         $answer = $host.ui.PromptForChoice($caption,$message,$choices,0)
  214.  
  215.         switch ($answer){
  216.             0 {return $true; break}
  217.             1 {return $false; break}
  218.         }
  219.     }
  220.  
  221.     function Test-Admin {
  222.         $identity  = [System.Security.Principal.WindowsIdentity]::GetCurrent()
  223.         $principal = New-Object System.Security.Principal.WindowsPrincipal( $identity )
  224.         return $principal.IsInRole( [System.Security.Principal.WindowsBuiltInRole]::Administrator )
  225.     }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement