Setting Required Proxy Variables Using Command Prompt (URL Encoding Included)

Today I come to you with a script I’ve created that helps to set the HTTP_PROXY/HTTPS_PROXY variables when using certain command prompt based programs.

I created this out of curiosity and necessity as I had a co-worker who preferred to work strictly in Cmd vs using something like Git-Bash or Cygwin.

This script uses a combination of PowerShell and Batch functionality to create a completely native Windows approach towards setting required configurations when working behind a Corporate Proxy.

The process would be as follows:

  1. Open Command Prompt
  2. Execute this Batch Script
  3. Perform your Required Commands (pip/curl/etc)

Here is the script:

ECHO OFF
CLS

:MENU
	ECHO.
	ECHO ...............................................
	ECHO PRESS 1, 2 to select your task, or 3 to EXIT.
	ECHO ...............................................
	ECHO.
	ECHO 1 - Set Proxy Variables
	ECHO 2 - Clear Proxy Variables
	ECHO 3 - Exit
	ECHO.
	SET /P M=Type 1, 2, or 3 then press ENTER:
	IF %M%==1 GOTO SET_PROXY
	IF %M%==2 GOTO CLEAR_PROXY
	IF %M%==3 GOTO END

:SET_PROXY
	SET PROXY_ADDRESS=yourCorporateProxy.com
	SET PROXY_PORT=80
	set /p USERNAME="Enter Username: "
	set /p PASSWORD="Enter Password: "

	SET COMMAND=powershell.exe -command "Add-Type -AssemblyName System.Web;[System.Web.HttpUtility]::UrlEncode(\"%PASSWORD%\")"


	FOR /F "tokens=* USEBACKQ" %%F IN (`%COMMAND%`) DO (
	SET PASSWORD=%%F
	)

	SET FULL_PROXY_URL=http://%USERNAME%:%PASSWORD%@%PROXY_ADDRESS%:%PROXY_PORT%/

	SET HTTP_PROXY=%FULL_PROXY_URL%
	SET HTTPS_PROXY=%FULL_PROXY_URL%

	GOTO END
:CLEAR_PROXY
	SET HTTP_PROXY=""
	SET HTTPS_PROXY=""

	GOTO END
:END
	CLS

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s