Last active
August 22, 2022 07:05
-
-
Save mcwindy/869ed075a29790215c0df1a6e2e706c0 to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Import-Module oh-my-posh | |
| Import-Module posh-git | |
| Set-PoshPrompt mine | |
| chcp 936 | |
| Set-Alias -Name code -Value code-insiders.cmd | |
| C:\Users\mcwindy\login.ps1 | |
| # Chocolatey profile | |
| $ChocolateyProfile = "$env:ChocolateyInstall\helpers\chocolateyProfile.psm1" | |
| if (Test-Path($ChocolateyProfile)) { | |
| Import-Module "$ChocolateyProfile" | |
| } | |
| # Set-Proxy command | |
| Function SetProxy() { | |
| Param( | |
| $Addr = "http://127.0.0.1:7890", | |
| [switch]$ApplyToSystem | |
| ) | |
| $env:HTTP_PROXY = $Addr; | |
| $env:HTTPS_PROXY = $Addr; | |
| $env:http_proxy = $Addr; | |
| $env:https_proxy = $Addr; | |
| if ($addr -eq $null) { | |
| [Net.WebRequest]::DefaultWebProxy = New-Object Net.WebProxy; | |
| if ($ApplyToSystem) { SetSystemProxy $null; } | |
| Write-Output "Successful unset all proxy variable"; | |
| } | |
| else { | |
| [Net.WebRequest]::DefaultWebProxy = New-Object Net.WebProxy $Addr; | |
| if ($ApplyToSystem) { | |
| $matchedResult = ValidHttpProxyFormat $Addr; | |
| # Matched result: [URL Without Protocol][Input String] | |
| if (-not ($matchedResult -eq $null)) { | |
| SetSystemProxy $matchedResult.1; | |
| } | |
| } | |
| Write-Output "Successful set proxy as $Addr"; | |
| } | |
| } | |
| Function SetSystemProxy($Addr = $null) { | |
| Write-Output $Addr | |
| $proxyReg = "HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings"; | |
| if ($Addr -eq $null) { | |
| Set-ItemProperty -Path $proxyReg -Name ProxyEnable -Value 0; | |
| return; | |
| } | |
| Set-ItemProperty -Path $proxyReg -Name ProxyServer -Value $Addr; | |
| Set-ItemProperty -Path $proxyReg -Name ProxyEnable -Value 1; | |
| } | |
| Function ValidHttpProxyFormat ($Addr) { | |
| $regex = "(?:https?:\/\/)(\w+(?:.\w+)*(?::\d+)?)"; | |
| $result = $Addr -match $regex; | |
| if ($result -eq $false) { | |
| throw [System.ArgumentException]"The input $Addr is not a valid HTTP proxy URI."; | |
| } | |
| return $Matches; | |
| } | |
| Function UnsetProxy() { | |
| $env:HTTP_PROXY = ""; | |
| $env:HTTPS_PROXY = ""; | |
| $env:http_proxy = ""; | |
| $env:https_proxy = ""; | |
| } | |
| # Set-Alias setproxy SetProxy | |
| # Set-Alias unsetproxy UnsetProxy # powershell not case sensitive |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment