PowerShell WebClient Example

UPDATE [August 2014] : I made an update to this post here.

 

I’m still amazed that PowerShell has no commandlet equivalent of WGET or CURL. Even in SQL Server Integration Services (SSIS) this is missing.

In the age of RESTful webservices, the official guidance (at the time of writing) is to write your own with custom .Net code. Seriously?!?

It seems they are indeed serious…

So here for your amusement is a simple  function you can use in your PS1 scripts. It calls the .Net WebClient class.

## Declare a function that takes source and destination arguments
Function Get-Webclient ($url, $out) {

$proxy = [System.Net.WebRequest]::GetSystemWebProxy()

$proxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials

$request = New-Object System.Net.WebCLient

$request.UseDefaultCredentials = $true ## Proxy credentials only

$request.Proxy.Credentials = $request.Credentials

$request.DownloadFile($url, $out)

}

## Call the function with our source and destination.
Get-Webclient "http://www.google.com" "C:\tmp\Foo3.txt"

But what if the destination is password protected and you need authentication credentials?

## Declare a function that takes source and destination arguments
Function Get-Webclient ($url, $out) {

$proxy = [System.Net.WebRequest]::GetSystemWebProxy()

$proxy.Credentials = [System.Net.CredentialCache]::DefaultCredentials

$request = New-Object System.Net.WebCLient

$request.UseDefaultCredentials = $true

$request.Proxy.Credentials = $request.Credentials

$passwd	= ConvertTo-SecureString "mypassword" -AsPlainText -Force; ## Website credentials

$request.Credentials = New-Object System.Management.Automation.PSCredential ("username", $passwd);

$request.DownloadFile($url, $out)

}

## Call the function with our source and destination.
Get-Webclient "http://www.securedwebsite.com" "C:\tmp\Foo3.txt"
Advertisements

6 thoughts on “PowerShell WebClient Example

  1. Hi. Found your blog somewhat randomly.

    Is there something wrong with Invoke-WebMethod and Invoke-RestMethod that make them unusable for you? They were introduced in PS3, and “wget” is aliased to Invoke-WebMethod:

    wget http://www.stackoverflow.com/ -OutFile homepage.html

    On a system with IE installed (ie. not Server Core) you can navigate the DOM and everything:

    (wget http://www.yahoo.com).ParsedHtml.title

    Invoke-RestMethod assumes JSON by default, so you can do one-liners like this:

    (Invoke-RestMethod https://api.stackexchange.com/2.1/info?site=stackoverflow).items.total_users

    Hope this helps.

  2. great, I removed the proxy part and it worked like magic…

    yes, Microsoft is simply pathetic, and the attempts to defend their OS border with grotesque.

  3. Wow! I have been looking for this solution for over a week, and yours worked perfectly. Thank you. Now I just need to figure out how the output file will recognize the CR/LF. Is there a switch i can use with downloadfile()? The output file opens nicely formatted in word, but not in notepad.

  4. $webClient.DownloadFile($url, $dest)
    Receiving following error:
    Exception calling \”DownloadFile\” with \”2\” argument(s): \”The remote server returned an error: (404) Not Found.\”
    Has anyone got around it?

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 )

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