SharePoint Online PowerShell Tip: Conditional Scope - Binary Module
Introduction
Recently one of our customers requested us to share a code which should throw an exception while connecting to SharePoint Online if in case of credential invalid or not connect to tenant site. Well, they have a binary module delivered by some suppliers which are pretty straightforward and simple. If all set to good code will execute if not an exception thrown in another cmdlet. The module they use has a bunch of cmdlets which are more or less mock-up of SharePoint Online cmdlets. Connect-SPOService throws exception if credential is invalid and they need custom error message for the custom-built cmdlet Connect-SPOTenant
Requirement
- Connect-SPOTenant: The sign-in name or password does not match one in the Microsoft account system.
- Connect-SPOTenant: Current site is not a tenant administration site.
PowerShell Code
Below shown is PowerShell code shared by my customers and unfortunately, C# code was not shown to us.
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 {
}
}
Solution
Indeed, the above code will not throw an exception because the ClientContext Object is instantiated and will not validate the credential but returns the ClientContext Object as output. Moreover Microsoft.SharePoint.Client.ClientContext doesn't have ServerObjectIsNull property. No matter we supply the wrong password the host shows Connected to Site!
Conditional Scope
C# code with conditional scope is below. This is just a demo so we haven't used any scope methods or properties
using System;
using System.Management.Automation;
using Microsoft.SharePoint.Client;
using Microsoft.Online.SharePoint.TenantAdministration;
namespace xSharePointOnline
{
[Cmdlet(VerbsCommunications.Connect, "SPOTenant")]
public class ConnectSPOTenant : PSCmdlet
{
[Parameter()]
public Uri SPOUrl;
[Parameter()]
[Credential]
public PSCredential SPOCredential;
protected override void ProcessRecord()
{
using (ClientContext SPOClientContext = new ClientContext(SPOUrl))
{
SPOClientContext.Credentials = new SharePointOnlineCredentials(SPOCredential.UserName, SPOCredential.Password);
Tenant oTenant = new Tenant(SPOClientContext);
ConditionalScope Scope = new ConditionalScope(SPOClientContext, () => oTenant.ServerObjectIsNull.Value != true);
SPOClientContext.ExecuteQuery();
}
}
}
}