18

I am trying to use PowerShell to log into a website and download a file.

However I can't get PS to pass the credentials properly.

Here is my PS:

$webclient = new-object System.Net.WebClient
$webclient.Credentials = new-object System.Net.NetworkCredential("username","password","domain")
$webpage = $webclient.DownloadString("url goes here")

Here is the log in box I get when I hit the site in IE: enter image description here

Clarence Klopfstein
  • 4,682
  • 10
  • 33
  • 47

5 Answers5

16

This is what I got to work. I believe the key part is the "Basic" in the CredentialCache

$webclient = new-object System.Net.WebClient
$credCache = new-object System.Net.CredentialCache
$creds = new-object System.Net.NetworkCredential("un","pw")
$credCache.Add("url", "Basic", $creds)
$webclient.Credentials = $credCache
$webpage = $webclient.DownloadString("url")
Clarence Klopfstein
  • 4,682
  • 10
  • 33
  • 47
8

If you want to use Invoke-WebRequest instead of the WebClient:

$securepassword = ConvertTo-SecureString "password" -AsPlainText -Force
$credentials = New-Object System.Management.Automation.PSCredential("username", $securepassword)
Invoke-WebRequest -Uri "url goes here" -Credential $credentials

I based the code on this blog article by Douglas Tarr. Note that in the article the username and password are confused, but I've fixed them in my sample.

twasbrillig
  • 17,084
  • 9
  • 43
  • 67
Aaron D
  • 5,817
  • 1
  • 36
  • 51
5

For some reason I wasn't able to get any of these solutions to work (using PowerShell 5 on Win 10). Could perhaps be an obvious, fool-hardy situation since I don't use PS very often. But FWIW this is how I was able to get it to work, by manually setting the Authorization header.

$url = "{url here}"
$username = "{username here}"
$password = "{password here}"

$b = [System.Text.Encoding]::UTF8.GetBytes($username + ":" + $password)
$p = [System.Convert]::ToBase64String($b)

$creds = "Basic " + $p

Invoke-WebRequest -Uri $url -Headers @{"Authorization"=$creds} 

For whatever reason, the other answers here did make requests but no Authorization header was ever sent.

k3davis
  • 985
  • 12
  • 29
1

This is how I did it,

first created a download.ps1 file that contains the powershell script,

Then running this script through a batch file:

C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe -File  C:\Users\SS\Desktop\download.ps1

this is the PowerShell script:

    $Username = 'Domain\user'
    $Password = 'pass'
    $Url = "http://google.com/target/filename.zip"
    $Path = "C:\path\to\downloaded\file\filename.zip"
    $WebClient = New-Object System.Net.WebClient
    $WebClient.Credentials = New-Object System.Net.Networkcredential($Username, $Password)
    $WebClient.DownloadFile( $url, $path )
Shahar Hamuzim Rajuan
  • 5,610
  • 9
  • 53
  • 91
-1

What you have should work (try the code with some other sites). Normally, an invalid username/password would cause the code to fail with a 401 error (not the windows security login window). The problem may be related to the web site requiring Windows Integrated Authentication

Community
  • 1
  • 1
Jim Ecker
  • 407
  • 6
  • 6