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

时间:2016-05-06 20:03:00

标签: powershell sharepoint-online

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

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

3 个答案:

答案 0 :(得分: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()
}

答案 1 :(得分:0)

尽管我不知道将凭据与Invoke-WebRequest本身一起传递的直接方法,但我发现一种解决方法是,通过手动对SharePoint页面进行身份验证来捕获cookie值,并将其用于后续请求。您可以使用提琴手或其他类似工具来获取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的代码命名

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

答案 2 :(得分:0)

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

您要从页面中查找哪种内容?可以使用RESTful查询(Invoke-RESTMethod和SharePoint REST API)或从PowerShell SharePoint PNP和SharePoint Online cmdlet库访问有关SharePoint的大多数内容。