我正在处理一个相当特殊的问题.我们需要点击SharePoint场上的列表服务.Web身份验证通过Oracle SSO联合,但我们确实为可以执行Web请求的自动化配置了帐户.使用AAM,我们为服务器端自动化配置了一个"内部"URL,它直接绕过AD,其他所有内容都被推送到SSO.
这是我用来尝试获取列表集合的代码(已消毒).
$username = "DOMAIN\username"
$password = "somepassword"
$site = "https://sp.biz.com/sites/SiteCollection"
$credentials = New-Object -TypeName System.Management.Automation.PSCredential -ArgumentList $username, (ConvertTo-SecureString $password -AsPlainText -Force)
$proxy = New-WebServiceProxy -Uri "$site/_vti_bin/Lists.asmx" -Credentials $credentials
$proxy.GetListCollection()
Run Code Online (Sandbox Code Playgroud)
当我使用该代码时,我遇到了403.
调用带有"0"参数的"GetListCollection"的异常:"服务器无法处理请求.--->访问被拒绝.(HRESULT异常:0x80070005(E_ACCESSDENIED))"
如果我更改$ site以使用内部URL(通过AAM设置)并在其中一个前端运行,我会成功收到列表集合.现在,起初我认为帐户和权限存在问题,但在运行Fiddler捕获后,我发现它根本没有进行身份验证.
当我运行以下cURL命令时,它会验证并返回列表集合.Soap.xml只是直接从WDSL复制的基本GetListCollection数据包.
curl -v -u 'username':'pass' --ntlm -X POST -H "Content-Type: text/xml" --data-binary @soap.xml https://sp.biz.com/sites/SiteCollection/_vti_bin/Lists.asmx
Run Code Online (Sandbox Code Playgroud)
这是来自cURL的已清理的详细输出.
* STATE: INIT => CONNECT handle 0x600056190; line 1029 (connection #-5000)
* Hostname was NOT found in DNS cache
* Trying <IPv6>...
* STATE: CONNECT => WAITCONNECT handle 0x600056190; line …
Run Code Online (Sandbox Code Playgroud) 当 Invoke-WebRequest 达到 500、401、403 等时,如何捕获输出?当调用成功时,它将结果存储在$Response
变量中,但是如果我遇到 401,它会抛出错误但不存储响应。
$Response = Invoke-WebRequest -Method HEAD -Uri 'https://site.contoso.com'
Invoke-WebRequest : The remote server returned an error: (401) Unauthorized.
At line:1 char:13
+ $Response = Invoke-WebRequest -Method HEAD -Uri 'https://site.contoso.com' -Erro ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : InvalidOperation: (System.Net.HttpWebRequest:HttpWebRequest) [Invoke-WebRequest], WebException
+ FullyQualifiedErrorId : WebCmdletWebResponseException,Microsoft.PowerShell.Commands.InvokeWebRequestCommand
Run Code Online (Sandbox Code Playgroud)
该$Response
变量仍然为空,但是端点返回 401 Unauthorized和标头,它只是没有被 PowerShell 捕获,因为它将其视为异常。通过 Fiddler 进行管道传输证实了这一点。
创建使用请求[System.Net.WebRequest]
具有当我执行相同的结果$Response = $WebRequest.GetResponse()
。
这是我的$PSVersionTable
。
Name Value
---- -----
PSVersion 4.0
WSManStackVersion 3.0 …
Run Code Online (Sandbox Code Playgroud)