PowerShell and WebServices from behind a Proxy

Today i puppetted with accessing webservices from behind a proxy in PowerShell and i thought of sharing with internet community if one may come across.

Following are the work conditions that i have.
1) A corporate proxy through which all my web traffic route through
2) PowerShell RC1 version
3) Window XP with SP2

My Objective was to download a file from internet from behing a Proxy via PowerShell..(Coz i may Schedule a download one day through PowerShell Script)

I kickstarted my R&D and searched internet to gain some foo for thought. Later I came through Abisheks blog in msdn and lighted my bulb.

So, here the way we go

Here the verbose of my script

PS C:\> $a = New-Object system.net.webclient # This will create a webclient

PS C:\> $b = New-Object system.net.webproxy (“********************”) # This will create webproxy component

PS C:\> $b.UseDefaultCredentials = $true  # instructs to use my default credentials

PS C:\> $a.Proxy = $b  # wraps the webproxy settings to a Webclient

#Now that we a have webclient which can communicate to the internet if permitted by the proxy

#Lets download a string.

PS C:\> $a.DownloadString(“http://www.google.com“) # dont forget to place http in front as it is required.

# to download a file from an URL and place it into C: drive? Dont worry here u go!

PS C:\> $a.DownloadFile(“http://www.google.com” , “c:\sample.htm”)  #This will downlaod a file from the internet and places it in C: drive with file name Sample.htm


One thought on “PowerShell and WebServices from behind a Proxy

Leave a Reply

Please log in using one of these methods to post your comment:

WordPress.com Logo

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

Twitter picture

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

Facebook photo

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

Google+ photo

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

Connecting to %s