2

I'm trying install chocolatey using packer.io and powershell script.

I have a two scripts, one for proxy configuration and one for chocolatey install. first script for proxy:

$ErrorActionPreference = "Stop"
# set global proxy
$reg = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
Set-ItemProperty -Path $reg -Name ProxyServer -Value "http://mycompoany.proxy:1234"
Set-ItemProperty -Path $reg -Name ProxyEnable -Value 1

second script for installing chocolatey:

$ErrorActionPreference = "Stop"
$reg = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"

$settings = Get-ItemProperty -Path $reg
$settings.ProxyServer
$settings.ProxyEnable

iex ((new-object net.webclient).DownloadString('http://chocolatey.org/install.ps1'))

I run this scripts using powershell in packer and first script pass, second script doesn't. I get error message:

^[[0;32m    windows-2012-R2-standard: Exception calling "DownloadString" with "1" argument(s): "Unable to connect to^[[0m
^[[0;32m    windows-2012-R2-standard: the remote server"^[[0m
^[[0;32m    windows-2012-R2-standard: At C:\Windows\Temp\script.ps1:19 char:1^[[0m
^[[0;32m    windows-2012-R2-standard: + iex ($wc.DownloadString('https://chocolatey.org/install.ps1'))^[[0m
^[[0;32m    windows-2012-R2-standard: + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~^[[0m
^[[0;32m    windows-2012-R2-standard: + CategoryInfo          : NotSpecified: (:) [], ParentContainsErrorRecordE^[[0m
^[[0;32m    windows-2012-R2-standard: xception^[[0m
^[[0;32m    windows-2012-R2-standard: + FullyQualifiedErrorId : WebException^[[0m
^[[0;32m    windows-2012-R2-standard:^[[0m
^[[1;32m==> windows-2012-R2-standard: Deleting output directory...^[[0m

Of course $settings.ProxyServer returns correct proxy address and proxy is enabled. When I run only first script and boot machine created by packer I can manually install chocolatey without any modification and browse internet using IE. If I don't run first script (to set procxy) I won't install anything because of proxy. Also I can't open any internet page. As result I assume that my script for proxy works.

I don't use user and pass for my proxy.

My windows system is Windows 2012 server R2. Packer version 0.10.1

I tried set proxy directly in webclient powershell object but this doesn't work too.

Any ideas?

esio
  • 1,592
  • 3
  • 17
  • 30
  • Have you tried `-ExecutionPolicy unrestricted`? e.g. like this: `@powershell -NoProfile -ExecutionPolicy unrestricted -Command "iex ((new-object net.webclient).DownloadString('https://chocolatey.org/install.ps1'))"` – Wlad Aug 17 '16 at 12:52

3 Answers3

0

You can use this. Worked for me. https://github.com/chocolatey/choco/wiki/Proxy-Settings-for-Chocolatey

Explicit Proxy Settings

Chocolatey has explicit proxy support starting with 0.9.9.9.

You can simply configure 1 or 3 settings and Chocolatey will use a proxy server. proxy is required and is the location and port of the proxy server. proxyUser and proxyPassword are optional. The values for user/password are only used for credentials when both are present.

choco config set proxy <locationandport>
choco config set proxyUser <username>
choco config set proxyPassword <passwordThatGetsEncryptedInFile>

Example

Running the following commands in 0.9.9.9:

choco config set proxy http://localhost:8888
choco config set proxyUser bob
choco config set proxyPassword 123Sup#rSecur3
Felix Aballi
  • 899
  • 1
  • 13
  • 31
0

Unfortunatelly I can't set proxy in Power Shell. This doesn't work without restart anr manually run IE once [sic!].

I set proxy in Autounattend.xml file and this solution works.

esio
  • 1,592
  • 3
  • 17
  • 30
  • Can you please elaborate? Some information about how to configure the Autounattend.xml file and how to use it from Packer – mnieto Feb 09 '23 at 16:39
-1

Two commands to install Chocolatey on Windows

Prerequisites: Launch cmd in administrator mode

1) Extend the cmd: @"%SystemRoot%\System32\WindowsPowerShell\v1.0\powershell.exe" -NoProfile -InputFormat None -ExecutionPolicy Bypass -Command "iex ((New-Object System.Net.WebClient).DownloadString('https://chocolatey.org/install.ps1'))" && SET "PATH=%PATH%;%ALLUSERSPROFILE%\chocolatey\bin"

2) Run the cmd: choco install chocolateygui