Share via


Validate SharePoint Online Tenant Admin URL Using PowerShell - CSOM

Introduction

Recently one of my colleagues asked me for code which validates SharePoint Online Tenant admin URL using PowerShell which made me share this as TechNet Wiki Article. During the discussion, few suggested string manipulation tricks in PowerShell because the SP Online tenant admin URL is a well-known format like https://contoso-admin.sharepoint.com. (Regex to find URL with admin) Well, that was not a good move? Let us do it in PowerShell by using the correct class of SharePoint Online.

Reference

We posted an article a while before in which we used C# to build PowerShell binary cmdlet. This link may give an insight http://social.technet.microsoft.com/wiki/contents/articles/34315.sharepoint-online-powershell-tip-conditional-scope-binary-module.aspx

Using PowerShell

The base need is to throw an exception if the URL argument is not a tenant admin URL and reuse the credential. The logic we applied is very simple PowerShell trick!

  • Create a function.
  • Name your function as desired.
  • Parameterize your function and name it as desired.
  • Declare a script scope variable and get a credential. This allows us to reuse the secure credential for other functions.
  • Invoke execute query method inside the try block and catch the exception.

Incorrect Credential Exception

Incorrect Tenant Admin URL Exception

Note: We can still enhance the code with proper exception handling! Since, this is a demo and to avoid string manipulation we picked up a method which validates the credential and tenant admin URL!

Code

Import-Module 'C:\Program Files\NuGet\Packages\Microsoft.SharePointOnline.CSOM.16.1.5312.1200\lib\net45\Microsoft.SharePoint.Client.dll'            Import-Module 'C:\Program Files\NuGet\Packages\Microsoft.SharePointOnline.CSOM.16.1.5312.1200\lib\net45\Microsoft.SharePoint.Client.Runtime.dll'            Import-Module 'C:\Program Files\NuGet\Packages\Microsoft.SharePointOnline.CSOM.16.1.5312.1200\lib\net45\Microsoft.Online.SharePoint.Client.Tenant.dll'            function Connect-xSPOTenant {                [CmdletBinding()]                param(                    [Parameter(Mandatory)]                    [uri]                    $Url,                                         [Parameter(Mandatory)]                    [System.Management.Automation.CredentialAttribute()]                    [pscredential]                    $Credential                )                                                      $Script:SPOCredential = [Microsoft.SharePoint.Client.SharePointOnlineCredentials]::new($Credential.UserName , $Credential.Password)                    $SPOClientContext = [Microsoft.SharePoint.Client.ClientContext]::new($Url)                    $SPOClientContext.Credentials = $SPOCredential                    $oTenant = [Microsoft.Online.SharePoint.TenantAdministration.Tenant]::new($SPOClientContext)                    $oTenant.ServerObjectIsNull.Value -ne $true | Out-Null                    try {                        $SPOClientContext.ExecuteQuery()                    }                    Catch{                        $_.Exception.Message                     }                                           }            Connect-xSPOTenant -Url https://contoso.sharepoint.com -Credential "chendrayan@contoso.onmicrosoft.com"          

Summary

There are many articles over the internet which demo validating the connection with SharePoint Online using ClientContext object. Unfortunately, it will not work due to the absence of ServerObjectIsNull property. Kick on your PowerShell and test it $SPOClientContext | GM -MemberType Properties