ProxyServer.psm1
#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 |