简体   繁体   中英

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

I m trying to download a file from Linux server with Apache webserver to Windows 2012 R2 using Windows powershell

Note :: 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

ERROR

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 im able to download but through powershell it show bad credentials

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

尝试使用创建密码字符串

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM