Created
May 28, 2014 21:24
-
-
Save jberezanski/01319436ca0c78a60120 to your computer and use it in GitHub Desktop.
Script to setup Chocolatey and install packages in one go, at machine scope, compatible with Azure VM script extension.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#Requires -Version 3 | |
$ErrorActionPreference = 'Stop' | |
Set-StrictMode -Version 3 | |
# setup Chocolatey and apply patches | |
Write-Host 'Installing Chocolatey' | |
$Env:ChocolateyInstall = "$Env:SystemDrive\chocolatey" | |
$Env:ChocolateyBinRoot = "$Env:ChocolateyInstall\tools" | |
[Environment]::SetEnvironmentVariable('ChocolateyInstall', $Env:ChocolateyInstall, 'Machine') | |
[Environment]::SetEnvironmentVariable('ChocolateyBinRoot', $Env:ChocolateyBinRoot, 'Machine') | |
Write-Host "PATH before installing Chocolatey: $Env:PATH" | |
$oldUserPath = [Environment]::GetEnvironmentVariable('PATH', 'User') | |
iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1')) | |
[Environment]::SetEnvironmentVariable('ChocolateyInstall', $null, 'User') | |
$path = [Environment]::GetEnvironmentVariable('PATH', 'Machine') | |
if (-not ($path -like "*$Env:ChocolateyInstall*")) { | |
Write-Host 'Migrating per-user ChocolateyInstall PATH addition to machine scope' | |
$path = "{0};{1};" -f $path.Trim(';'), "$Env:ChocolateyInstall\bin" | |
[Environment]::SetEnvironmentVariable('PATH', $path, 'Machine') | |
} | |
Write-Host 'Restoring initial per-user PATH' | |
[Environment]::SetEnvironmentVariable('PATH', $oldUserPath, 'User') | |
$Env:PATH = [Environment]::GetEnvironmentVariable('PATH') | |
Write-Host "Refreshed PATH after installing Chocolatey: $Env:PATH" | |
Write-Host 'Ensuring NuGet sees Chocolatey as installed' | |
$chocoVer = Get-Content (Join-Path $Env:ChocolateyInstall ChocolateyInstall\chocolatey.ps1) | ? { $_ -match '\$chocVer\s*=\s*''(?<v>[^'']+)''' } | % { $matches['v'] } | |
$chocoPackagePath = Join-Path $Env:ChocolateyInstall "lib\chocolatey.$chocoVer" | |
$chocoZip = Join-Path $Env:TEMP "chocolatey\chocInstall\chocolatey.zip" | |
$chocoNupkg = "$chocoPackagePath\chocolatey.$chocoVer.nupkg" | |
if (-not (Test-Path $chocoPackagePath)) { | |
New-Item -ItemType Directory $chocoPackagePath | Out-Null | |
Write-Host "Copying $chocoZip to $chocoNupkg" | |
Copy-Item -Path $chocoZip -Destination $chocoNupkg | |
} | |
Write-Host 'Ensured NuGet sees Chocolatey as installed' | |
# fixed in https://github.com/chocolatey/chocolatey/pull/476 | |
Write-Host 'Fixing Get-ChocolateyUnzip.ps1 running as SYSTEM on 64-bit OS' | |
$f = Join-Path -Resolve $Env:ChocolateyInstall chocolateyinstall\helpers\functions\Get-ChocolateyUnzip.ps1 | |
$c = Get-Content $f | |
$c | % { | |
if ($_ -like "*Running 'Get-ChocolateyUnzip'*") { | |
@" | |
# 32-bit 7za.exe would not find C:\Windows\system32\config\systemprofile\AppData\Local\Temp | |
if ([IntPtr]::Size -ne 4) { | |
`$fileFullPath = `$fileFullPath -ireplace ([regex]::Escape([Environment]::GetFolderPath('System'))),(Join-Path `$Env:SystemRoot sysnative) | |
} | |
"@ | |
} | |
$_ | |
} | Set-Content $f -Encoding ASCII | |
Write-Host 'Fixed Get-ChocolateyUnzip.ps1' | |
Write-Host 'Fixing Chocolatey-Version in strict mode' | |
$f = Join-Path -Resolve $Env:ChocolateyInstall chocolateyinstall\functions\Chocolatey-Version.ps1 | |
$c = Get-Content $f | |
$c | % { | |
$l = $_ | |
if ($l -like '*$packages.count*') { | |
$l = $l.Replace('$packages.count', '($packages | Measure-Object).Count') | |
} | |
$l.Replace('–', '-') # word processor-style en dashes, U+2013 | |
} | Set-Content $f -Encoding ASCII | |
Write-Host 'Fixed Chocolatey-Version.ps1' | |
function cinst | |
{ | |
[CmdletBinding()] | |
Param ( | |
[Parameter(Mandatory = $true)] | |
[string] $PackageName, | |
[ValidateNotNullOrEmpty()] | |
[string] $Source, | |
[ValidateNotNullOrEmpty()] | |
[string] $Version, | |
[string] $InstallArguments, | |
[switch] $OverrideArgs | |
) | |
$global:LastExitCode = 0 | |
$additionalArguments = @{} | |
if ($Source -ne '') { | |
$additionalArguments.source = $Source | |
} | |
if ($Version -ne '') { | |
$additionalArguments.Version = $Version | |
} | |
if ($InstallArguments -ne '') { | |
$additionalArguments.installArguments = $InstallArguments | |
} | |
if ($OverrideArgs) { | |
$additionalArguments.overrideArgs = $true | |
} | |
"{0:yyyy-MM-dd HH:mm:ss} Installing package $PackageName from source '$Source'" -f (Get-Date) | |
& "$Env:ChocolateyInstall\chocolateyInstall\chocolatey.ps1" install $packageName @additionalArguments | |
if ($LastExitCode -ne 0) { | |
throw "Installation failed (code $LastExitCode) for package $PackageName" | |
} | |
"{0:yyyy-MM-dd HH:mm:ss} Installed package $PackageName from source '$Source'" -f (Get-Date) | |
} | |
# install packages | |
cinst NetFx3 -source windowsfeatures | |
cinst notepad2-mod | |
cinst sysinternals # adds to path in user scope | |
cinst elevate.native | |
cinst javaruntime-preventasktoolbar | |
cinst err | |
cinst fciv | |
# clean up environment | |
if ([Environment]::GetEnvironmentVariable('ChocolateyInstall', 'User') -ne $null) { | |
Write-Host 'Removing per-user ChocolateyInstall again (update of choco adds it)' | |
[Environment]::SetEnvironmentVariable('ChocolateyInstall', $null, 'User') | |
} | |
$curPath = [Environment]::GetEnvironmentVariable('PATH', 'User') | |
if ($curPath -ne $oldUserPath) { | |
Write-Host 'Migrating per-user PATH additions to machine scope' | |
$path = [Environment]::GetEnvironmentVariable('PATH', 'Machine').Trim(';') | |
$pathDelta = $curPath | |
if ($oldUserPath -ne $null) { | |
$pathDelta = $pathDelta -ireplace ([regex]::Escape($oldUserPath.Trim(';'))), $null | |
} | |
$pathDelta -split ';' | ? { $_ -ne '' } | % { | |
$chunk = $_ | |
Write-Host "Migrating path component: $chunk" | |
if (-not ($path -like "*$chunk*")) { | |
$path = "{0};{1};" -f $path.Trim(';'), "$chunk" | |
} | |
} | |
[Environment]::SetEnvironmentVariable('PATH', $path, 'Machine') | |
Write-Host 'Restoring initial per-user PATH' | |
[Environment]::SetEnvironmentVariable('PATH', $oldUserPath, 'User') | |
$Env:PATH = [Environment]::GetEnvironmentVariable('PATH') | |
Write-Host "Refreshed PATH: $Env:PATH" | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment