naga4ce
naga4ce

Reputation: 1575

Download a file from Remote Server(https) using powershell with credentials

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

Upvotes: 1

Views: 6437

Answers (2)

Hunter Eidson
Hunter Eidson

Reputation: 1909

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

Upvotes: 1

Henning Schwarten
Henning Schwarten

Reputation: 11

try to create the passwordstring with

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

Upvotes: 0

Related Questions