Skip to content

Instantly share code, notes, and snippets.

@OlafD
Last active January 21, 2019 14:22
Show Gist options
  • Save OlafD/82629878fbb8c822fcdf43a5eb5a34fc to your computer and use it in GitHub Desktop.
Save OlafD/82629878fbb8c822fcdf43a5eb5a34fc to your computer and use it in GitHub Desktop.
From the PowerShell PnP extensions installed on any machine, copy the files Microsoft.SharePoint.Client.dll and Microsoft.SharePoint.Client.Runtime.dll to the same folder, as this script. Then, you can execute a Rest call like .\RestAgainstSharePointOnline.ps1 -Url https://{yourtenant}.sharepoint.com/_api/web -UserName {youruser} -Password {your…
param (
[string]$Url,
[string]$UserName,
[string]$Password,
[Microsoft.PowerShell.Commands.WebRequestMethod]$Method = [Microsoft.PowerShell.Commands.WebRequestMethod]::Get
)
$currentPath = (Get-Item -Path ".\").FullName
Add-Type -Path "$currentPath\Microsoft.SharePoint.Client.dll"
Add-Type -Path "$currentPath\Microsoft.SharePoint.Client.Runtime.dll"
<# When PowerShell PnP Extensions are installed, we can use this to load the assemblies
[Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client")
[Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint.Client.Runtime")
#>
if([string]::IsNullOrEmpty($Password)) {
$SecurePassword = Read-Host -Prompt "Enter the password" -AsSecureString
}
else {
$SecurePassword = $Password | ConvertTo-SecureString -AsPlainText -Force
}
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($UserName, $SecurePassword)
$request = [System.Net.WebRequest]::Create($Url)
$request.Credentials = $credentials
$request.Headers.Add("X-FORMS_BASED_AUTH_ACCEPTED", "f")
$request.Accept = "application/json;odata=verbose"
$request.Method=$Method
$response = $request.GetResponse()
$requestStream = $response.GetResponseStream()
$readStream = New-Object System.IO.StreamReader $requestStream
$data=$readStream.ReadToEnd()
$results = $data | ConvertFrom-Json
$results.d
@OlafD
Copy link
Author

OlafD commented Dec 12, 2018

This sample will not work, when WebLogin is necessary, because of the current ADFS configuration of the tenant.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment