powershell-3.0 - 为什么 Invoke-RestMethod 不使用 UseDefaultCredential 开关发送凭据?

标签 powershell-3.0

我正在使用 PowerShell Invoke-RestMethod 并希望通过当前登录用户的凭据发送。所以我正在使用 -UseDefaultCredential 开关:

Invoke-RestMethod -Uri $serverUrl -UseDefaultCredential -ContentType "application/xml" -Body '<tags count="1"><tag name="testTag" /></tags>' -Method Put -Verbose

但我不断收到 401 Unauthorized 响应。

当我创建一个 PSCredentials 对象并传递它时,它起作用了。

$creds = Get-Credential -Credential 'someusername'
Invoke-RestMethod -Uri $serverUrl -Credential $creds -ContentType "application/xml" -Body '<tags count="1"><tag name="testTag" /></tags>' -Method Put -Verbose

查看 Fiddler,我可以看到这两种用法都得到初始 401,但带有 -Credential 的选项会发送带有 Authorization header 的后续请求。

为什么 -UseDefaultCredential 不起作用?我还可以查看哪些内容来调查此问题?

作为引用,这是向 TeamCity REST API 发送请求。

最佳答案

一些网络服务器要求在 POST 之前进行 GET。 我认为它可能与 PUT 相同,您可以尝试:

Invoke-RestMethod -Uri $serverUrl -UseDefaultCredential -SessionVariable s -Method Get
Invoke-RestMethod -Uri $serverUrl -WebSession $s -ContentType "application/xml" -Body '<tags count="1"><tag name="testTag" /></tags>' -Method Put -Verbose

关于powershell-3.0 - 为什么 Invoke-RestMethod 不使用 UseDefaultCredential 开关发送凭据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34652621/

相关文章:

exception - 简单的 Try/catch 不起作用

powershell - 使用 powershell 对象执行脚本

Powershell Write-EventLog/Get-WinEvent 消息问题

windows - Powershell get-childitem 输出格式

powershell - 我们是否需要 Write-Output?

powershell - 使用 PowerShell 格式化磁盘而不提示确认

powershell - 代码似乎需要Powershell 4,但是看不到为什么

powershell - 使用 PowerShell 连接文件

json - 在 powershell 3 中美化 json

powershell - XML文件中的条件替换