1

I'm trying to download a file from Linux server with Apache web server to Windows Server 2012 R2 using Windows PowerShell

Note: the URL is HTTPS

$source = "https://uri"
$destination = "C:\path\file.txt"
$username = "admin"
$password = "@dfkl!f"  | ConvertTo-SecureString -asPlainText -Force
$cred = New-Object System.Management.Automation.PSCredential($username,$password)
Invoke-WebRequest $source -OutFile $destination -Credential $cred
Invoke-WebRequest : Authorization Required
This server could not verify that you are authorized to access the document     requested. Either you supplied the wrong
credentials (e.g., bad password), or your browser doesn't understand how to supply the credentials required.
Apache/2.2.15 (CentOS) Server at url Port 443

When I type the credentials through browser i m able to download but through powershell it show bad credentials

6
  • 1
    Try use System.Net.WebClient + System.Net.CredentialCache as shown here - stackoverflow.com/questions/12282842/… Commented Feb 13, 2015 at 10:29
  • I tried with above solution but dint work... it shows 401 unauthorized error Commented Feb 13, 2015 at 12:01
  • It's seems that you need to inspect network traffic with tools like Ethereal Commented Feb 13, 2015 at 12:09
  • It might expect basic authentication Commented Feb 13, 2015 at 12:46
  • @Mathias R. Jessen I m trying in powershell so i created a same function thats shown in above answer but still same error appears Commented Feb 13, 2015 at 13:06

2 Answers 2

1

I just tried this against one of my Apache/Linux boxes on a SSL page that uses Basic auth, and it seemed to work... Your mileage might vary...

$source = "https://Uri"
$destination = "C:\Foo\Bar"
$username = 'mylogin'
$password = 'reallgoodpassword'

$auth = 'Basic ' + [System.Convert]::ToBase64String([System.Text.Encoding]::UTF8.GetBytes($username+':'+$password ))

Invoke-WebRequest -Headers @{ "Accept" = "*/*"; "Authorization" = $auth } -Uri $source -Method Get
Sign up to request clarification or add additional context in comments.

Comments

0

try to create the passwordstring with

$password = ConvertTo-SecureString "@dfkl!f" -AsPlainText -Force

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.