ProxyServer.psm1

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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
#region Functions
#region Get-ProxyServer
function Get-ProxyServer
{
    # The registry key with the settings
    $regKey="HKCU:\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"

    # Get current proxy server setting
    [string]$proxyServerSetting = (Get-ItemProperty -path $regKey ProxyServer -ErrorAction SilentlyContinue).proxyserver

    # Seperater proxy server address and port
    $items = $proxyServerSetting.Split(':')

    # Create object and add server name and port
    $obj = New-Object PSObject
    $obj | Add-Member -MemberType NoteProperty -Name "Server" -Value $items[0]
    $obj | Add-Member -MemberType NoteProperty -Name "Port" -Value $items[1]

    # Check if proxy is enabled and update object
    [string]$proxyServerSetting = (Get-ItemProperty -path $regKey ProxyEnable -ErrorAction SilentlyContinue).ProxyEnable

    if( $proxyServerSetting -eq 1 )
    {
        $obj | Add-Member -MemberType NoteProperty -Name Status -Value "Enabled"
    }
    else
    {
        $obj | Add-Member -MemberType NoteProperty -Name Status -Value "Disabled"
    }

    # Get overrides
    [string]$proxyServerSetting = (Get-ItemProperty -path $regKey ProxyOverride -ErrorAction SilentlyContinue).ProxyOverride

    $items = $proxyServerSetting.Split(";")

    $obj | Add-Member -MemberType NoteProperty -Name Overrides -Value $items

    # Return object
    Write-Output $obj
}
#endregion

#region Disable-ProxyServer
function Disable-ProxyServer
{    
    # The registry item to update
    $regKey="HKCU:\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"
    
    # Update registry
    Set-ItemProperty -Path $regKey ProxyEnable 0 -ErrorAction SilentlyContinue
}
#endregion

#region Enable-ProxyServer
function Enable-ProxyServer
{
    # The registry item to update
    $regKey="HKCU:\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"
    
    Set-ItemProperty -Path $regKey ProxyEnable 1 -ErrorAction SilentlyContinue
}
#endregion

#region Set-ProxyServer
function Set-ProxyServer
{
    Param(
        [Parameter(Mandatory=$True,Position=0)]
        [string]$ComputerName,
        [Parameter(Position=1)]
        [string]$Port = "8080"
        )

    $regKey="HKCU:\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"

    $proxyServerSetting = $ComputerName + ":" + $Port

    Set-ItemProperty -Path $regKey ProxyServer $proxyServerSetting -ErrorAction SilentlyContinue
}
#endregion

#region Get-ProxyServerOverride
function Get-ProxyServerOverride
{
    # The registry key with the settings
    $regKey="HKCU:\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"

    # Create array to hold results
    $overrides = @()

    # Get overrides
    [string]$proxyServerSetting = (Get-ItemProperty -path $regKey ProxyOverride -ErrorAction SilentlyContinue).ProxyOverride

    $items = $proxyServerSetting.Split(";")

    foreach($o in $items)
    {
        if($o.ToLower().Equals("<local"))
        {
            continue
        }

        $obj = New-Object PSObject
        $obj | Add-Member -MemberType NoteProperty -Name URL -Value $o
    }

    # Return object
    Write-Output $items
}
#endregion

#region New-ProxyServerOverride
function New-ProxyServerOverride
{
    Param(
        [Parameter(Mandatory=$True,Position=0)]
        [string]$URL
    )
    
    # The registry key with the settings
    $regKey="HKCU:\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"

    # Create array to hold results
    $overrides = @()

    # Get overrides
    [string]$proxyServerSetting = (Get-ItemProperty -path $regKey ProxyOverride -ErrorAction SilentlyContinue).ProxyOverride

    $proxyServerSetting += ";" + $url

    # Check for leading ";"
    if($proxyServerSetting.StartsWith(";"))
    {
        $proxyServerSetting = $proxyServerSetting.Substring(1)
    }

    # Update registry
    Set-ItemProperty -Path $regKey ProxyOverride $proxyServerSetting -ErrorAction SilentlyContinue
}
#endregion

#region Remove-ProxyServerOverride
function Remove-ProxyServerOverride
{
    Param(
        [Parameter(Mandatory=$True,Position=0)]
        [string]$URL
    )

    # The registry key with the settings
    $regKey="HKCU:\\Software\\Microsoft\\Windows\\CurrentVersion\\Internet Settings"

    # Create array to hold resultsS
    [string]$overrides = ""

    # Get overrides
    [string]$proxyServerSetting = (Get-ItemProperty -path $regKey ProxyOverride -ErrorAction SilentlyContinue).ProxyOverride

    $items = $proxyServerSetting.Split(";")

    # Create the new setting
    [string]$proxyOverrides = ""

    foreach($o in $items)
    {
        if($o.Length -le 0)
        {
            continue
        }

        if( -Not ($o.Equals($URL)))
        {
            $overrides += ";" + $o
        }
    }

    # Check for leading ";"
    if($overrides.StartsWith(";"))
    {
        $overrides = $overrides.Substring(1)
    }

    # Update registry
    Set-ItemProperty -Path $regKey ProxyOverride $overrides -ErrorAction SilentlyContinue
}
#endregion

#region Enable-ProxyServerBypassForLocal
function Enable-ProxyServerBypassForLocal
{
    New-ProxyServerOverride -URL "<local>"
}
#endregion

#region Disable-ProxyServerBypassForLocal
function Disable-ProxyServerBypassForLocal
{
    Remove-ProxyServerOverride -URL "<local>"
}
#endregion
#endregion

#region Exports
Export-ModuleMember -Function Get-ProxyServer
Export-ModuleMember -Function Set-ProxyServer
Export-ModuleMember -Function Disable-ProxyServer
Export-ModuleMember -Function Enable-ProxyServer
Export-ModuleMember -Function Get-ProxyServerOverride
Export-ModuleMember -Function New-ProxyServerOverride
Export-ModuleMember -Function Remove-ProxyServerOverride
Export-ModuleMember -Function Enable-ProxyServerBypassForLocal
Export-ModuleMember -Function Disable-ProxyServerBypassForLocal
#endregion