Contents

Script Dumpster: Setting Proxy server settings


Contents

OK, so I’m at my new job and for the first time in years, I have to deal with proxy servers again…

Since I’m used to having full admin rights on my machine and unrestricted internet, this was somewhat of a shock, while perfect for my productivity as well 😛

Unfortunately for them, I have since been informed that a special IT proxy is available, which provides this access, hence me being able to update my site again 🙂

Here’s a quick script which allows me to switch from “normal” proxy (while pre-defined you can set it to whatever you prefer) to IT proxy or disable it all together

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
[CmdletBinding(DefaultParameterSetName='SetProxy')]
Param (
    [Parameter(ParameterSetName='SetProxy')]
    $ProxyServer = '192.168.0.250:8080' ,
    [Parameter(ParameterSetName='SetProxy')]
    [switch]$IT,
    [Parameter(ParameterSetName='DisableProxy')]
    [switch]$Disable
    )

begin {

    Write-Verbose "Running command $($MyInvocation.MyCommand)"
    Write-Verbose "Server proxy set to '$ProxyServer'"
    Write-Verbose "IT is set to '$IT'"
    Write-Verbose "Disable is set to '$Disable'"

    $regKey="HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"
    if ($IT) {
        $ProxyServer = '192.168.0.251:8080'
    }
}
process {

    Write-Verbose 'Checking current Proxy settings'
    $CurrentProxyServer = (Get-ItemProperty -path $regKey ProxyServer -ErrorAction SilentlyContinue).ProxyServer
    Write-Verbose "Current proxy is set to '$CurrentProxyServer'"

    if ($Disable){
        if([string]::IsNullOrEmpty($CurrentProxyServer)){
            Write-Output "Proxy is already disabled"
            return
        } else {
            Set-ItemProperty -path $regKey ProxyEnable -value 0
            Remove-ItemProperty -path $regKey -name ProxyServer
            Write-Output "Proxy is now disabled"
            return
        }
    }



    if([string]::IsNullOrEmpty($CurrentProxyServer)){
        Write-Output "Proxy is currently disabled"
        Set-ItemProperty -path $regKey ProxyEnable -value 1
        Set-ItemProperty -path $regKey ProxyServer -value $ProxyServer
        Write-Output "Proxy is now enabled and set to '$ProxyServer'"
    } else {
        if ($CurrentProxyServer -eq $ProxyServer) {
            Write-Output "Current proxy set to $ProxyServer"
            return
        } else {
            Write-Output "Proxy is currently set to '$CurrentProxyServer'"
            Write-Output "Setting proxy to '$ProxyServer' instead"

            Set-ItemProperty -path $regKey ProxyEnable -value 1
            Set-ItemProperty -path $regKey ProxyServer -value $ProxyServer
            Write-Output "Proxy is enabled and set to '$ProxyServer'"
        }
    }
}

Happy Scripting! 🙂