powershell - invoke-webrequest 使用 Windows 身份验证返回 401

标签 powershell

我正在编写一个脚本以登录到一个 sharepoint 2013 站点并导航到几个页面,以确保该站点在更新和 DR 演习后正常工作。我这样调用 Invoke-WebRequest :

$site = Invoke-WebRequest -uri 'https://spsite' -Credential $(Get-Credential) -SessionVariable s

当我调用电话时,我收到了 401 Unauthorized 错误。我尝试过使用基本身份验证并构建这样的 header :
$u = 'domain\user'
$p = 'password'
$header = @{ Authorization = "Basic {0}" -f [convert]::ToBase64String([Text.Encoding]::ASCII.GetBytes(("{0}:{1}" -f $u,$p))) }

$site = Invoke-WebRequest -uri 'https://spsite' -Headers $header

结果相同。我希望有人可以提供另一种方式来建立这种联系?

最佳答案

所以我找到了一种在我的情况下完成这项工作的方法,并想发布基础知识,以防其他人遇到这种情况。

我发现当抛出异常时,您可以从异常对象中获取 Web 服务器的实际响应,如下所示:

try{
    $site = Invoke-WebRequest -uri 'https://spsite' -Credential $(Get-Credential) -SessionVariable s
}
catch{
    $site = $_.Exception.Response
}

之后,我能够操纵 $site 变量以跟随重定向并根据需要提交凭据。

关于powershell - invoke-webrequest 使用 Windows 身份验证返回 401,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42444160/

相关文章:

PowerShell DSC。下载并安装软件

powershell - 尝试在包含大量文件的目录中获取唯一的扩展名列表非常缓慢

powershell - Add-AzureAccount -credential 没有像我希望的那样工作

PowerShell NuGet - "no match was found for the specified search criteria"、 "unable to find dependent packages"等

powershell - 如何处理文件路径中的空格

xml - 注释 XML 部分(在节点中有前缀)并取消注释其他

java - 批处理文件命令转powershell命令

PowerShell 使用自签名证书连接到 REST API。

powershell - 有没有办法对任何用户使用 ConvertFrom-SecureString 和 ConvertTo-SecureString?

python - 通过命令行获取信息提示