1

I'm trying to access a site that has Windows Security on it:enter image description here

So I referenced this post and wrote the following code:

$web = New-Object Net.WebClient
$web.Credentials = new-object System.Net.NetworkCredential($username, $password, $domain)
$content = $web.DownloadString("https://my.url")

But I still get the same error I got before adding credentials: "The remote server returned an error: (401) Unauthorized." I then referenced this post, but I don't think basic authentication applies in this case. I have verified the username, password, and domain several times. Are there other commonly used solutions for making web requests with authentication?

Community
  • 1
  • 1
aquemini
  • 950
  • 2
  • 13
  • 32
  • what are you passing for $domain in the second line above? I've gotten this to work, without passing anything for domain. – mti2935 Jul 21 '14 at 20:41
  • the domain that i blocked out in the image above is not the domain that the account i'm using is under, therefore i need to specify it each time. – aquemini Jul 21 '14 at 20:50
  • Which version of Powershell are you using? Also, are you using different credential than the script invoking one? – Rahul Jul 21 '14 at 20:54
  • 1
    If it is taking a Windows credential it needs to be secure. `$password = ConvertTo-SecureString "mypassword" -AsPlainText -Force` – Hashman Jul 21 '14 at 20:54
  • PowerShell 2.0 is on the server i'm using, and @Hashman i tried that and it still says Unauthorized – aquemini Jul 21 '14 at 21:31
  • this code worked fine for me until all of the sudden. thats why im here now reading this post. must have been some update operations applied to the server... still looking for a solution – Northstrider Sep 21 '15 at 21:00

1 Answers1

6

Not sure which PowerShell version you are using but if you are using PowerShell3.0 or greater you can use Invoke-WebRequest CommandLet like below

$username = "domain_name\user_name"
$password = "mypassword" | ConvertTo-SecureString -asPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($username,$password)

$res = Invoke-WebRequest https://my.url -Credential $cred
Rahul
  • 76,197
  • 13
  • 71
  • 125
  • unfortunately PowerShell 2.0 is on the server i'm using – aquemini Jul 21 '14 at 21:31
  • That's unfortunate. Is it possible to upgrade to 3.0? I don't think you/your Organization needs to pay extra money to upgrade to higher version. Also, are you using different credential than the script invoking one? – Rahul Jul 21 '14 at 21:33