从Powershell加载带有InvokeWebrequest的SharePoint Online页面



我希望能够通过Powershell的Invoke-Webrequest加载SharePoint Online页面。

有人能告诉我如何成功浏览登录屏幕吗?

以下是我如何从SPO获取列表项。您需要有"Microsoft.SharePoint.Client.SharePointOnlineCredentials"dll。

然后使用我为SPO修改的以下函数。

优点:这甚至适用于较旧的PowerShell版本。如果您只想针对更高级别的PowerShell,那么此代码也可以工作。您可以选择使用Invoke-Webrequest而不是System.Net.HttpWebRequestSystem.Net.HttpWebResponse

function Get-ListItems {
    [CmdletBinding()]
  PARAM (
        [Parameter(Mandatory=$true)]
        [String] $URL
        )
  #$URL = Fix-Url $URL
  $xml = Request-Rest -URL $URL 
  return $xml
}   
function Request-Rest{    
    [CmdletBinding()]
  PARAM (
        [Parameter(Mandatory=$true)]
        [String] $URL,
        [Parameter(Mandatory=$false)]
        [Microsoft.SharePoint.Client.SharePointOnlineCredentials] $credentials,
        [Parameter(Mandatory=$false)]
        [String] $UserAgent = "PowerShell API Client",
        [Parameter(Mandatory=$false)]
        [Switch] $JSON,
        [Parameter(Mandatory=$false)]
        [Switch] $Raw
  )
    #Create a URI instance since the HttpWebRequest.Create Method will escape the URL by default.   
    $URI = New-Object System.Uri($URL,$true)   
    #Create a request object using the URI   
    $request = [System.Net.HttpWebRequest]::Create($URI)   
    #Build up a nice User Agent   
    $request.UserAgent = $(   
        "{0} (PowerShell {1}; .NET CLR {2}; {3})" -f $UserAgent, $(if($Host.Version){$Host.Version}else{"1.0"}),  
        [Environment]::Version,  
        [Environment]::OSVersion.ToString().Replace("Microsoft Windows ", "Win")  
        )
    if ($credentials -eq $null)
    {
       $request.UseDefaultCredentials = $true
    }
    else
    {
       $request.Credentials = $credentials
    }
    if ($PSBoundParameters.ContainsKey('JSON'))
    {
        $request.Accept = "application/json"
    }
    $request.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f")
    #$request.Accept = "application/json;odata=verbose"
    try
    {
        [System.Net.HttpWebResponse] $response = [System.Net.HttpWebResponse] $request.GetResponse()
    }
    catch
    {
         Throw "Exception occurred in $($MyInvocation.MyCommand): `n$($_.Exception.Message)"
    }
    $reader = [IO.StreamReader] $response.GetResponseStream()  
    if (($PSBoundParameters.ContainsKey('JSON')) -or ($PSBoundParameters.ContainsKey('Raw')))
    {
        $output = $reader.ReadToEnd()  
    }
    else
    {
        [xml]$output = $reader.ReadToEnd()  
    }
    $reader.Close()  
    Write-Output $output  
    $response.Close()
}

虽然我不知道将凭据与InvokeWebRequest本身一起传递的直接方法,但我找到的一个解决方法是,通过手动验证SharePoint页面来捕获cookie值,并将其用于后续请求。你可以用fiddler或其他类似的工具来抓取饼干。两个cookie名称分别为"FedAuth"one_answers"rtFa"

$session = New-Object Microsoft.PowerShell.Commands.WebRequestSession 
$cookieCollection = New-Object System.Net.CookieCollection 
$cookie1 = New-Object System.Net.Cookie 
$cookie1.Domain = "<your domain>.sharepoint.com" 
$cookie1.Name = "FedAuth" 
$cookie1.Value = "<cookie value here>" 
$cookieCollection.Add($cookie1) 
$cookie2 = New-Object System.Net.Cookie 
$cookie2.Domain = "<your domain>.sharepoint.com" 
$cookie2.Name = "rtFa" 
$cookie2.Value = "<cookie value here>"  
$cookieCollection.Add($cookie2) 
$session.Cookies.Add($cookieCollection) 
 
$uri = "https:<your site collection here>/_layouts/15/SharePointDesignerSettings.aspx" 
$response = Invoke-WebRequest -Uri $uri -WebSession $session -Method Default 
$form = $response.Forms[0]

您可以使用$form来检查Html元素。如果您想提交对表单所做的更改,请使用下面的行

$response = Invoke-WebRequest -Uri $uri -WebSession $session -Method POST -Body $form

注意:InvokeWebRequest在字段提交方面存在问题。。基本上,它使用输入元素的"id",而不是表单字段集合中的"name"。。下面的url具有将字段Id转换为Name 的代码

https://d-fens.ch/2013/05/11/invoke-webrequest-uses-id-attribute-of-input-element-as-field-name-in-form-fields-collection/

如果您正在寻找页面的最终内容,InvokeWebRequest将无法满足您的需要。SharePoint页面的大部分内容都是使用JavaScript异步加载的。InvokeWebRequest将只返回页面中的初始HTML内容。

你想从页面中查找什么样的内容?可以使用RESTful查询(Invoke-RESTMethod和SharePoint REST API)或从PowerShell SharePoint PNP和SharePoint Online cmdlet库访问有关SharePoint的大部分内容。

最新更新