几天前,我试图从我的Sharepoint Online检索所有网站的使用情况报告,但由于我们网站太多,在链接下载.xlsx报告后,通过查看链接很难获得所有这些网站。然后我开始使用PowerShell脚本来提取这些内容。
但出于某种原因,当我的代码开始下载文件时,它会下载O365登录页面的HTML代码。这告诉我应用程序正在丢失会话,因为它已经在前一时刻连接到该站点,突然我以登录页面结束。虽然我不知道如何解决这个问题......
这是尝试下载使用情况报告的代码部分:
# Usage Reports for Prod
$Usage = $UsageGUID
# set the file path and name
$filename = $path + (Get-Variable $searchreport).Name + ".xlsx"
$reportid = (Get-Variable $searchreport).Value
$TTNcontent = "javascript:ResetSpFormOnSubmitCalled();__doPostBack('__Page','ReportId=" + $reportid + "')"
# setup the WebRequest
[net.httpWebRequest] $webRequest = [net.webRequest]::create($url)
$webRequest.Credentials = $SPOCredentials
$webRequest.CookieContainer = New-Object System.Net.CookieContainer
$webRequest.Accept = "image/jpeg, application/x-ms-application, image/gif, application/xaml+xml, image/pjpeg, application/x-ms-xbap, */*"
$webRequest.ContentType = "application/x-www-form-urlencoded"
$webRequest.Method = "POST"
$webRequest.UserAgent = "Mozilla/5.0 (Windows NT 10.0; WOW64; Trident/7.0; rv:11.0) like Gecko"
$encodedContent = [System.Text.Encoding]::UTF8.GetBytes($TTNcontent)
$webRequest.ContentLength = $encodedContent.length
$requestStream = $webRequest.GetRequestStream()
$requestStream.Write($encodedContent, 0, $encodedContent.length)
$requestStream.Close()
#The problem occurs on this step: Once GetResponse is fired, instead of getting the .xlsx as a response, it downloads the HTML of the O365 authentication screen
#My guess is that the application is losing the login session before the .GetResponse() but I don't know how/why...
[net.httpWebResponse] $resp = $webRequest.GetResponse();
$rs = $resp.GetResponseStream();
[System.IO.BinaryReader] $sr = New-Object System.IO.BinaryReader -argumentList $rs;
[byte[]]$results = $sr.ReadBytes(10000000);
# write the file
Set-Content $filename $results -enc byte
这是完整的代码文件: GetUsageReports_SPOnline.ps1
任何想法或建议......?
提前致谢:)
马