You should be fine with setting the correct environment variables at the current user scope only (not at "machine" level). Add HTTP_PROXY
, HTTPS_PROXY
and NO_PROXY
.
Since I have to switch those variables in my laptop everytime I'm outside my job, I've made these script for Powershell.
Add Variables
File name: proxy-variables-add.ps1
Write-Host "ADDING VARIABLES"
# Define your environment variables here
$EnvVariable1 = "http://10.1.33.254:80"
$EnvVariable2 = "http://10.1.33.254:80"
$EnvVariable3 = "localhost,127.0.0.1,::1,LOCALHOST"
# Set environment variables for the current user
[Environment]::SetEnvironmentVariable("HTTP_PROXY", $EnvVariable1, [System.EnvironmentVariableTarget]::User)
[Environment]::SetEnvironmentVariable("HTTPS_PROXY", $EnvVariable2, [System.EnvironmentVariableTarget]::User)
[Environment]::SetEnvironmentVariable("NO_PROXY", $EnvVariable3, [System.EnvironmentVariableTarget]::User)
# Display a message to confirm that the variables have been set
Write-Host "Environment USER variables have been set:"
Write-Host "HTTP_PROXY = $EnvVariable1"
Write-Host "HTTPS_PROXY = $EnvVariable2"
Write-Host "NO_PROXY = $EnvVariable3"
# Notify the user to restart applications to apply the changes
Write-Host "Please restart any applications that need to use the new environment variables."
Remove Variables
File name: proxy-variables-remove.ps1
Write-Host "REMOVING VARIABLES"
# Define your environment variables here
$EnvVariable1 = $null
$EnvVariable2 = $null
$EnvVariable3 = $null
# Set environment variables for the current user
[Environment]::SetEnvironmentVariable("HTTP_PROXY", $EnvVariable1, [System.EnvironmentVariableTarget]::User)
[Environment]::SetEnvironmentVariable("HTTPS_PROXY", $EnvVariable2, [System.EnvironmentVariableTarget]::User)
[Environment]::SetEnvironmentVariable("NO_PROXY", $EnvVariable3, [System.EnvironmentVariableTarget]::User)
# Display a message to confirm that the variables have been set
Write-Host "Environment USER variables have been set:"
Write-Host "HTTP_PROXY = null (and variable deleted)"
Write-Host "HTTPS_PROXY = null (and variable deleted)"
Write-Host "NO_PROXY = null (and variable deleted)"
# Notify the user to restart applications to apply the changes
Write-Host "Please restart any applications that need to use the new environment variables."
From bash, you can run those scripts with the following code, at .bashrc
:
# Colors
NC='\033[0m' # No Color
YL='\033[0;33m' # Yellow
function proxyshell {
currentFolder=$(pwd)
cd "$HOME/my-scripts/powershellFolder" || exit
if [ "$1" == "on" ]; then
printf "${YL}>> Running Script\n${NC}"
echo ""
powershell -File proxy-variables-add.ps1
cd "$currentFolder" || exit
return 1
fi
if [ "$1" == "off" ]; then
printf "${YL}>> Running Script\n${NC}"
echo ""
powershell -File proxy-variables-remove.ps1
cd "$currentFolder" || exit
return 1
fi
}
In the command line, for setting up the environment variables:
$ proxyshell on
$ proxyshell off
Result