【发布时间】:2019-09-30 19:18:38
【问题描述】:
- PowerShell 5.1.17763.592 / Windows Server 2019 版本 1809(内部版本 17763.737)
我正在尝试使用 Invoke-WebRequest cmdlet 下载文件。该文件受 HTTP 基本身份验证保护。像这样进行两步操作:
PS E:\> $cred = Get-Credential
cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:
Credential
PS E:\> Invoke-WebRequest -Uri http://example.com/foo.zip -Credential $cred -OutFile $env:TEMP\foo.zip
尝试通过管道输入凭证以便我可以在单行中执行此操作失败:
PS E:\> Get-Credential | Invoke-WebRequest -Uri http://example.com/foo.zip -Credential $_ -OutFile $env:TEMP\foo.zip
cmdlet Get-Credential at command pipeline position 1
Supply values for the following parameters:
Credential
Invoke-WebRequest : Cannot send a content-body with this verb-type.
At line:1 char:18
+ ... redential | Invoke-WebRequest -Uri http://example.com/foo.z ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : NotSpecified: (:) [Invoke-WebRequest], ProtocolViolationException
+ FullyQualifiedErrorId : System.Net.ProtocolViolationException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
我已经用谷歌搜索了这个错误,但我唯一能找到的是Invoke-WebRequest using Get method doesn't allow content-body,但如果这是根本原因,我不明白为什么两个班轮会起作用。我怀疑我误解了$_ 的评估方式。
【问题讨论】:
标签: powershell