1

How can I download the contents of a page on my SharePoint Online (Office 365) environment using PowerShell? This is an .aspx page - I am looking for the rendered/html version, same as if I went there on my browser, not looking for the source code.

I would prefer a simple WebRequest call, but haven't been able to get the credentialing to work. I have tried many variations of the following, all of which return a 403:

$webclient = new-object System.Net.WebClient
$webclient.Credentials = new-object System.Net.NetworkCredential($username, $password)
$webclient.AuthenticationMode = "FormsAuthentication"
$webclient.FormsAuthenticationLoginInfo = New-Object Microsoft.SharePoint.Client.FormsAuthenticationLoginInfo($username, $password)

I have also tried the SharePoint Online client libraries. I can successfully connect to my environment using the code below, but don't know how to request a particular page from there:

$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($url) 
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($username, $password) 
4

Try this

$userName = "UserName"
$password = "Password"
$pageUrl = "PageUrl"
$localPath = "C:\Path\FileName.aspx"

#create secure password
$sPassword = $password | ConvertTo-SecureString -AsPlainText -Force

$webClient = New-Object System.Net.WebClient 
$webClient.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($userName, $sPassword)
$webClient.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f")
$webClient.DownloadFile($pageUrl, $localPath)
$webClient.Dispose()
  • Here's a follow-up question: Is there a way to parse the DOM of this page without saving it to disk? – George Winters May 11 '16 at 19:19

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.