【问题标题】:Download a file from Remote Server(https) using powershell with credentials使用带有凭据的 powershell 从远程服务器(https)下载文件
【发布时间】:2015-02-21 00:10:27
【问题描述】:

我正在尝试使用 Windows powershell 从带有 Apache 网络服务器的 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 显示凭据错误

【问题讨论】:

  • 尝试使用 System.Net.WebClient + System.Net.CredentialCache 如下所示 - stackoverflow.com/questions/12282842/…
  • 我尝试了上述解决方案,但效果很好......它显示 401 未经授权的错误
  • 看来您需要使用 Ethereal 之类的工具检查网络流量
  • 它可能期望basic authentication
  • @Mathias R. Jessen 我正在尝试使用 powershell,所以我创建了一个与上面答案中显示的相同的函数,但仍然出现相同的错误

标签: windows powershell powershell-4.0 windows2012


【解决方案1】:

我刚刚在一个使用基本身份验证的 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

【讨论】:

    【解决方案2】:

    尝试创建密码字符串

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-24
      • 1970-01-01
      • 1970-01-01
      • 2018-03-21
      • 2016-05-20
      相关资源
      最近更新 更多