使用Powershell中的Invoke-Webrequest加载SharePoint Online页面

问题描述 投票:0回答:3

我希望能够从Powershell使用Invoke-Webrequest加载SharePoint Online页面。

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

powershell sharepoint-online
3个回答
0
投票

以下是我从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()
}

0
投票

虽然我不知道将凭证与Invoke-WebRequest本身一起传递的直接方法,但我发现一种解决方法是通过手动验证SharePoint页面来捕获cookie值,并将其用于后续请求。您可以使用fiddler或其他类似工具来抓取cookie。这两个cookie的名字分别是'FedAuth'和'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 
$cookie1.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

注意:关于字段提交,Invoke-WebRequest存在问题。基本上它在表单字段集合中使用输入元素的“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/


0
投票

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

你在页面上寻找什么样的内容?可以使用RESTful查询(Invoke-RESTMethod和SharePoint REST API)或PowerShell SharePoint PNP和SharePoint Online cmdlet库访问有关SharePoint的大多数内容。

© www.soinside.com 2019 - 2024. All rights reserved.