SharePoint Online PowerShell Tip: Customize Your Script to Meet Client Requirements

sharepoint

Introduction

Recently I was building a PowerShell Module for one of our customers SharePoint farm. Of course, it’s completely customized. The SharePoint Online farm has multiple site collections with different languages. Any organization located in across geographical area needs this set up. So, in one our cmdlet we retrieve site information including language (In My Lab all the site are in en-US but the code works for other LocaleID ).

Get Started

Though, I was in middle of building binary module, I just paused it for a while and used SharePoint Online Management module for testing.
1Good let’s import the module to meet our need

Import-Module -Name Microsoft.Online.SharePoint.PowerShell -Verbose

2 Well, module is imported with warning due to unapproved verbs and that’s not going to impact. Now, let’s connect to SharePoint Tenant by using the below cmdlet

Connect-SPOService https://contoso-admin.SharePoint.com -Credential "Chendrayan@Contoso.onmicrosoft.com"

and by using the Cmdlet Get-SPOSite we can get the information we required like shown below

Get-SPOSite | Select StorageQuota , LocaleID

3 I just selected two properties to avoid exposing domain information. 1033 is LCID of en-US (English United States). Let’s play with our native Windows PowerShell

[System.Globalization.CultureInfo]::GetCultureInfo

4
which accepts three overloads let’s grab the first one to show the display name of the LocaleID (A property (output) of Get-SPOSite)

[System.Globalization.CultureInfo]::GetCultureInfo(1033)

5 Fine, we know the way to do! Let’s include in our Get-SPOSite cmdlet to get desired output.

Get-SPOSite | Select StorageQuota , @{Name="Language" ; Expression = {([System.Globalization.CultureInfo]::GetCultureInfo([int]$_.LocaleId))}}

6 or we can get display name by executing the below code

Get-SPOSite | Select StorageQuota , @{Name="Language" ; Expression = {([System.Globalization.CultureInfo]::GetCultureInfo([int]$_.LocaleId)).DisplayName}}

7 Clear, in the binary module I added the below piece of code to get desired output for our content managers

obj.Properties.Add(new PSNoteProperty("Language", System.Globalization.CultureInfo.GetCultureInfo((int)oWeb.Language).DisplayName));

and the sample code is below

using System;
using System.Collections.Generic;
using System.Management.Automation;
using Microsoft.SharePoint.Client;
namespace xSharePointOnline
{
    [Cmdlet(VerbsCommon.Get, "xSPOSite")]
    public class GetxSPOSite : PSCmdlet
    {
        [Parameter(Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
        public Uri Url
        {
            get;
            set;
        }

        [Parameter(Mandatory = false)]
        [Credential]
        public PSCredential SPOCredential
        {
            get;
            set;
        }
        protected override void ProcessRecord()
        {
            using (ClientContext SPOClientContext = new ClientContext(Url))
            {
                SPOClientContext.Credentials = new SharePointOnlineCredentials(SPOCredential.UserName, SPOCredential.Password);
                WebCollection oWebCollection = SPOClientContext.Web.Webs;
                IEnumerable<Web> oWebs = SPOClientContext.LoadQuery(oWebCollection);
                SPOClientContext.ExecuteQuery();
                PSObject obj = new PSObject();
                foreach (var oWeb in oWebs)
                {
                    obj.Properties.Add(new PSNoteProperty("Title", oWeb.Title));
                    obj.Properties.Add(new PSNoteProperty("Created", oWeb.Created));
                    obj.Properties.Add(new PSNoteProperty("NoCrawl", oWeb.NoCrawl));
                    obj.Properties.Add(new PSNoteProperty("Language", System.Globalization.CultureInfo.GetCultureInfo((int)oWeb.Language).DisplayName));
                    WriteObject(obj, true);
                }
            }
        }
    }
}

Note: I have included help and format files in this code!

Leave a Reply

Your email address will not be published. Required fields are marked *