|
| 1 | +# Windows... |
| 2 | +# |
| 3 | +# Here is a note to someone wondering what we're doing _here_ |
| 4 | +# (And why we're doing it here and now) |
| 5 | +# |
| 6 | +# Windows7 supports proxy configuration through Autounattend.xml |
| 7 | +# Unattended install sets everything needed to have a system-wide proxy |
| 8 | +# configuration. |
| 9 | +# |
| 10 | +# The problem seems to be that it fails to notify wininet subsystems of the |
| 11 | +# configuration. Which can prevent us from accessing the Internet. |
| 12 | +# (sometimes or always, it depends, I don't really know) |
| 13 | +# |
| 14 | +# This piece of code can't be run through WinRM for some reason. Running it |
| 15 | +# through WinRM will result in Windows removing `ProxyEnable` and `ProxyServer` |
| 16 | +# keys from the registry <--- O_o |
| 17 | +# |
| 18 | +# Anyway, in order to MakeItWork(tm) we need to run this interactively. |
| 19 | +# |
| 20 | +# These dark spells of black witchcraftery should be enough to allow |
| 21 | +# Windows/IE/Wininet/Whatever to get the right memo and use the configured |
| 22 | +# proxy |
| 23 | +# |
| 24 | + |
| 25 | +echo "Proxy is: $([Net.GlobalProxySelection]::Select.Address.Host)" |
| 26 | + |
| 27 | +$signature = @' |
| 28 | +[DllImport("wininet.dll", SetLastError = true, CharSet=CharSet.Auto)] |
| 29 | +public static extern bool InternetSetOption(IntPtr hInternet, int dwOption, IntPtr lpBuffer, int dwBufferLength); |
| 30 | +'@ |
| 31 | +$wininet = Add-Type -MemberDefinition $signature -Name wininet -Namespace pinvoke -PassThru |
| 32 | + |
| 33 | +function Refresh-System |
| 34 | +{ |
| 35 | + $INTERNET_OPTION_SETTINGS_CHANGED = 39 |
| 36 | + $INTERNET_OPTION_REFRESH = 37 |
| 37 | + $INTERNET_OPTION_PROXY_SETTINGS_CHANGED = 95 |
| 38 | + $a = $wininet::InternetSetOption(0, $INTERNET_OPTION_SETTINGS_CHANGED, 0, 0) |
| 39 | + $b = $wininet::InternetSetOption(0, $INTERNET_OPTION_PROXY_SETTINGS_CHANGED, 0, 0) |
| 40 | + $c = $wininet::InternetSetOption(0, $INTERNET_OPTION_REFRESH, 0, 0) |
| 41 | +} |
| 42 | + |
| 43 | +function Test-Connection |
| 44 | +{ |
| 45 | + $ie = New-Object -comobject InternetExplorer.Application; |
| 46 | + $ie.visible=$False; |
| 47 | + $ie.navigate('http://google.com'); |
| 48 | + start-sleep -s 5; |
| 49 | + $ie.quit(); |
| 50 | +} |
| 51 | + |
| 52 | +Refresh-System |
| 53 | +Test-Connection |
| 54 | + |
| 55 | +echo "Proxy is: $([Net.GlobalProxySelection]::Select.Address.Host)" |
0 commit comments