1 # =====================================================================
2 # Copyright 2011 - Present RealDimensions Software, LLC, and the
3 # original authors/contributors from ChocolateyGallery
4 # at https://github.com/chocolatey/chocolatey.org
6 # Licensed under the Apache License, Version 2.0 (the "License");
7 # you may not use this file except in compliance with the License.
8 # You may obtain a copy of the License at
10 # http://www.apache.org/licenses/LICENSE-2.0
12 # Unless required by applicable law or agreed to in writing, software
13 # distributed under the License is distributed on an "AS IS" BASIS,
14 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 # See the License for the specific language governing permissions and
16 # limitations under the License.
17 # =====================================================================
19 # Environment Variables, specified as $env:NAME in PowerShell.exe and %NAME% in cmd.exe.
20 # For explicit proxy, please set $env:chocolateyProxyLocation and optionally $env:chocolateyProxyUser and $env:chocolateyProxyPassword
21 # For an explicit version of Chocolatey, please set $env:chocolateyVersion = 'versionnumber'
22 # To target a different url for chocolatey.nupkg, please set $env:chocolateyDownloadUrl = 'full url to nupkg file'
23 # NOTE: $env:chocolateyDownloadUrl does not work with $env:chocolateyVersion.
24 # To use 7zip (requires additional download) instead of built-in compression, please set $env:chocolateyUseWindowsCompression = 'false'
25 # To bypass the use of any proxy, please set $env:chocolateyIgnoreProxy = 'true'
27 #specifically use the API to get the latest version (below)
30 $chocolateyVersion = $env:chocolateyVersion
31 if (![string
]::IsNullOrEmpty
($chocolateyVersion)){
32 Write-Output "Downloading specific version of Chocolatey: $chocolateyVersion"
33 $url = "https://chocolatey.org/api/v2/package/chocolatey/$chocolateyVersion"
36 $chocolateyDownloadUrl = $env:chocolateyDownloadUrl
37 if (![string
]::IsNullOrEmpty
($chocolateyDownloadUrl)){
38 Write-Output "Downloading Chocolatey from : $chocolateyDownloadUrl"
39 $url = "$chocolateyDownloadUrl"
42 if ($env:TEMP
-eq
$null) {
43 $env:TEMP
= Join-Path $env:SystemDrive
'temp'
45 $chocTempDir = Join-Path $env:TEMP
"chocolatey"
46 $tempDir = Join-Path $chocTempDir "chocInstall"
47 if (![System
.IO
.Directory
]::Exists
($tempDir)) {[System
.IO
.Directory
]::CreateDirectory
($tempDir)}
48 $file = Join-Path $tempDir "chocolatey.zip"
50 # PowerShell v2/3 caches the output stream. Then it throws errors due
# to the FileStream not being what is expected. Fixes "The OS handle's
# position is not what FileStream expected. Do not use a handle
# simultaneously in one FileStream and in Win32 code or another
# FileStream."
51 function Fix-PowerShellOutputRedirectionBug
{
52 $poshMajorVerion = $PSVersionTable.PSVersion
.Major
54 if ($poshMajorVerion -lt
4) {
56 # http://www.leeholmes.com/blog/2008/07/30/workaround-the-os-handles-position-is-not-what-filestream-expected/ plus comments
57 $bindingFlags = [Reflection
.BindingFlags
] "Instance,NonPublic,GetField"
58 $objectRef = $host.GetType
().GetField
("externalHostRef", $bindingFlags).GetValue
($host)
59 $bindingFlags = [Reflection
.BindingFlags
] "Instance,NonPublic,GetProperty"
60 $consoleHost = $objectRef.GetType
().GetProperty
("Value", $bindingFlags).GetValue
($objectRef, @
())
61 [void
] $consoleHost.GetType
().GetProperty
("IsStandardOutputRedirected", $bindingFlags).GetValue
($consoleHost, @
())
62 $bindingFlags = [Reflection
.BindingFlags
] "Instance,NonPublic,GetField"
63 $field = $consoleHost.GetType
().GetField
("standardOutputWriter", $bindingFlags)
64 $field.SetValue
($consoleHost, [Console
]::Out
)
65 [void
] $consoleHost.GetType
().GetProperty
("IsStandardErrorRedirected", $bindingFlags).GetValue
($consoleHost, @
())
66 $field2 = $consoleHost.GetType
().GetField
("standardErrorWriter", $bindingFlags)
67 $field2.SetValue
($consoleHost, [Console
]::Error
)
69 Write-Output "Unable to apply redirection fix."
74 Fix-PowerShellOutputRedirectionBug
76 function Get-Downloader
{
81 $downloader = new-object System
.Net
.WebClient
83 $defaultCreds = [System
.Net
.CredentialCache
]::DefaultCredentials
84 if ($defaultCreds -ne
$null) {
85 $downloader.Credentials
= $defaultCreds
88 $ignoreProxy = $env:chocolateyIgnoreProxy
89 if ($ignoreProxy -ne
$null -and
$ignoreProxy -eq
'true') {
90 Write-Debug "Explicitly bypassing proxy due to user environment variable"
91 $downloader.Proxy
= [System
.Net
.GlobalProxySelection
]::GetEmptyWebProxy
()
93 # check if a proxy is required
94 $explicitProxy = $env:chocolateyProxyLocation
95 $explicitProxyUser = $env:chocolateyProxyUser
96 $explicitProxyPassword = $env:chocolateyProxyPassword
97 if ($explicitProxy -ne
$null -and
$explicitProxy -ne
'') {
99 $proxy = New-Object System
.Net
.WebProxy
($explicitProxy, $true)
100 if ($explicitProxyPassword -ne
$null -and
$explicitProxyPassword -ne
'') {
101 $passwd = ConvertTo-SecureString $explicitProxyPassword -AsPlainText
-Force
102 $proxy.Credentials
= New-Object System
.Management
.Automation
.PSCredential
($explicitProxyUser, $passwd)
105 Write-Debug "Using explicit proxy server '$explicitProxy'."
106 $downloader.Proxy
= $proxy
108 } elseif
(!$downloader.Proxy
.IsBypassed
($url)) {
109 # system proxy (pass through)
110 $creds = $defaultCreds
111 if ($creds -eq
$null) {
112 Write-Debug "Default credentials were null. Attempting backup method"
113 $cred = get-credential
114 $creds = $cred.GetNetworkCredential
();
117 $proxyaddress = $downloader.Proxy
.GetProxy
($url).Authority
118 Write-Debug "Using system proxy server '$proxyaddress'."
119 $proxy = New-Object System
.Net
.WebProxy
($proxyaddress)
120 $proxy.Credentials
= $creds
121 $downloader.Proxy
= $proxy
128 function Download-String
{
132 $downloader = Get-Downloader
$url
134 return $downloader.DownloadString
($url)
137 function Download-File
{
142 Write-Output "Downloading $url to $file"
143 $downloader = Get-Downloader
$url
145 $downloader.DownloadFile
($url, $file)
148 if ($url -eq
$null -or
$url -eq
'') {
149 Write-Output "Getting latest version of the Chocolatey package for download."
150 $url = 'https://chocolatey.org/api/v2/Packages()?$filter=((Id%20eq%20%27chocolatey%27)%20and%20(not%20IsPrerelease))%20and%20IsLatestVersion'
151 [xml
]$result = Download-String
$url
152 $url = $result.feed
.entry
.content
.src
155 # Download the Chocolatey package
156 Download-File
$url $file
158 # Determine unzipping method
159 $unzipMethod = 'builtin'
160 $useWindowsCompression = $env:chocolateyUseWindowsCompression
161 if ($useWindowsCompression -ne
$null -and
$useWindowsCompression -eq
'false') {
162 Write-Output 'Using 7zip to unzip.'
163 $unzipMethod = '7zip'
167 Write-Output "Extracting $file to $tempDir..."
168 if ($unzipMethod -eq
'7zip') {
170 Write-Output "Downloading 7-Zip commandline tool first"
171 $7zaExe = Join-Path $tempDir '7za.exe'
172 Download-File
'https://chocolatey.org/7za.exe' "$7zaExe"
173 Start-Process
"$7zaExe" -ArgumentList
"x -o`"$tempDir`
" -y `"$file`
"" -Wait
-NoNewWindow
176 $shellApplication = new-object
-com shell
.application
177 $zipPackage = $shellApplication.NameSpace
($file)
178 $destinationFolder = $shellApplication.NameSpace
($tempDir)
179 $destinationFolder.CopyHere
($zipPackage.Items
(),0x10)
181 throw "Unable to unzip package using built-in compression. Set `$env:chocolateyUseWindowsCompression = 'false' and call install again to use 7zip to unzip. Error: `n $_"
185 # Call chocolatey install
186 Write-Output "Installing chocolatey on this machine"
187 $toolsFolder = Join-Path $tempDir "tools"
188 $chocInstallPS1 = Join-Path $toolsFolder "chocolateyInstall.ps1"
192 Write-Output 'Ensuring chocolatey commands are on the path'
193 $chocInstallVariableName = "ChocolateyInstall"
194 $chocoPath = [Environment
]::GetEnvironmentVariable
($chocInstallVariableName)
195 if ($chocoPath -eq
$null -or
$chocoPath -eq
'') {
196 $chocoPath = 'C:\ProgramData\Chocolatey'
199 $chocoExePath = Join-Path $chocoPath 'bin'
201 if ($($env:Path)
.ToLower
().Contains
($($chocoExePath)
.ToLower
()) -eq
$false) {
202 $env:Path
= [Environment
]::GetEnvironmentVariable
('Path',[System
.EnvironmentVariableTarget
]::Machine
);
205 Write-Output 'Ensuring chocolatey.nupkg is in the lib folder'
206 $chocoPkgDir = Join-Path $chocoPath 'lib\chocolatey'
207 $nupkg = Join-Path $chocoPkgDir 'chocolatey.nupkg'
208 if (![System
.IO
.Directory
]::Exists
($chocoPkgDir)) { [System
.IO
.Directory
]::CreateDirectory
($chocoPkgDir); }
209 Copy-Item "$file" "$nupkg" -Force
-ErrorAction SilentlyContinue
211 # SIG # Begin signature block
212 # MIINWwYJKoZIhvcNAQcCoIINTDCCDUgCAQExDzANBglghkgBZQMEAgEFADB5Bgor
213 # BgEEAYI3AgEEoGswaTA0BgorBgEEAYI3AgEeMCYCAwEAAAQQH8w7YFlLCE63JNLG
214 # KX7zUQIBAAIBAAIBAAIBAAIBADAxMA0GCWCGSAFlAwQCAQUABCBTImtQthSv4EIL
215 # 5pnaQl3hubK8yJm5W8uSyH+Rm3qPoKCCCngwggUwMIIEGKADAgECAhAECRgbX9W7
216 # ZnVTQ7VvlVAIMA0GCSqGSIb3DQEBCwUAMGUxCzAJBgNVBAYTAlVTMRUwEwYDVQQK
217 # EwxEaWdpQ2VydCBJbmMxGTAXBgNVBAsTEHd3dy5kaWdpY2VydC5jb20xJDAiBgNV
218 # BAMTG0RpZ2lDZXJ0IEFzc3VyZWQgSUQgUm9vdCBDQTAeFw0xMzEwMjIxMjAwMDBa
219 # Fw0yODEwMjIxMjAwMDBaMHIxCzAJBgNVBAYTAlVTMRUwEwYDVQQKEwxEaWdpQ2Vy
220 # dCBJbmMxGTAXBgNVBAsTEHd3dy5kaWdpY2VydC5jb20xMTAvBgNVBAMTKERpZ2lD
221 # ZXJ0IFNIQTIgQXNzdXJlZCBJRCBDb2RlIFNpZ25pbmcgQ0EwggEiMA0GCSqGSIb3
222 # DQEBAQUAA4IBDwAwggEKAoIBAQD407Mcfw4Rr2d3B9MLMUkZz9D7RZmxOttE9X/l
223 # qJ3bMtdx6nadBS63j/qSQ8Cl+YnUNxnXtqrwnIal2CWsDnkoOn7p0WfTxvspJ8fT
224 # eyOU5JEjlpB3gvmhhCNmElQzUHSxKCa7JGnCwlLyFGeKiUXULaGj6YgsIJWuHEqH
225 # CN8M9eJNYBi+qsSyrnAxZjNxPqxwoqvOf+l8y5Kh5TsxHM/q8grkV7tKtel05iv+
226 # bMt+dDk2DZDv5LVOpKnqagqrhPOsZ061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