使用带有凭据的powershell从远程服务器(https)下载文件

时间:2015-02-13 10:24:50

标签: windows powershell powershell-v4.0 windows2012

我试图使用Windows PowerShell将文件从带有Apache webserver的Linux服务器下载到Windows 2012 R2

注意:: URL是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

当我通过浏览器输入凭据我可以下载但通过powershell它显示错误的凭据

2 个答案:

答案 0 :(得分:1)

我刚刚在一个使用Basic auth的SSL页面上对我的一个Apache / Linux盒子进行了尝试,它似乎有用......你的里程可能会有所不同......

$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

答案 1 :(得分:0)

尝试使用

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