Created
March 31, 2015 20:51
-
-
Save glapointe/98ca20586fc172f70f4b to your computer and use it in GitHub Desktop.
Utility function for connecting to a SharePoint Online Site Collection using the SharePoint CSOM API and PowerShell.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
function global:Connect-SPOSite { | |
[CmdletBinding()] | |
param ( | |
[Parameter(Mandatory=$true, ValueFromPipeline=$true, Position=0)] | |
$Url | |
) | |
begin { | |
[System.Reflection.Assembly]::LoadFile("C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.dll") | Out-Null | |
[System.Reflection.Assembly]::LoadFile("C:\Program Files\SharePoint Online Management Shell\Microsoft.Online.SharePoint.PowerShell\Microsoft.SharePoint.Client.Runtime.dll") | Out-Null | |
} | |
process { | |
if ($global:spoCred -eq $null) { | |
$cred = Get-Credential -Message "Enter your credentials for SharePoint Online:" | |
$global:spoCred = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($cred.UserName, $cred.Password) | |
} | |
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext $Url | |
$ctx.Credentials = $spoCred | |
if (!$ctx.ServerObjectIsNull.Value) { | |
Write-Host "Connected to site: '$Url'" -ForegroundColor Green | |
} | |
return $ctx | |
} | |
end { | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment