Connect-SPCSOM.ps1

##############################
#.SYNOPSIS
#Creates a context object for connection to SharePoint Online.
#
#.DESCRIPTION
#Creates a context object to the supplied site using the credential object or username/password combination supplied. This context is stored as a global
#variable. It can also be assigned to a differenent variable to support mulitple connections to multiple sites, simultaneously.
#
#.PARAMETER credential
#Credential Object to use for authentication
#
#.PARAMETER userName
#String object for the usersname.
#
#.PARAMETER password
#Secure.String object for the password. If it isn't supplied, will initiate a read-host to retrieve it.
#
#.PARAMETER SiteURL
#The site for the context.
#
#.EXAMPLE
#$credentail = Get-PSCredential
#Connect-SPCSOM -credential $credential -SiteURL "Https://tenantname.sharepoint.com/sites/test"
#
#$ctx = Connect-SPCSOM -username "<username>" -SiteURL "Https://tenantname.sharepoint.com/sites/test"
#
#.NOTES
#Need to work on the global variable usage in other functions. This doesn't seem to be working 100%.
##############################
Function Connect-SPCSOM {
    [CmdletBinding()]
    Param(
        [Parameter(Mandatory=$true,ParameterSetName='Credential')]
        #[Parameter(ParameterSetName="NoCred")]
        [PSCredential]$credential,

        [Parameter(Mandatory=$true,ParameterSetName='UserPass')]
        #[Parameter(ParameterSetName="NoCred")]
        [String]$userName,
        [Parameter(ParameterSetName='UserPass')]
        [string]$Domain = "GHS_NTDOMAIN",
        [Parameter(ParameterSetName='UserPass')]
        [securestring]$Password = $null,

        [Parameter(Mandatory=$true, ParameterSetName="NoCred")]
        [Parameter(Mandatory=$true, ParameterSetName="Credential")]
        [Parameter(Mandatory=$true, ParameterSetName="UserPass")]
        [String]$SiteURL
    )

    if($Credential){
        Write-Verbose "Credential detected. Assign username and password."
        $Username = $Credential.UserName
        $Password = $Credential.Password
    }

    #Make sure the password is of type [SecureString]
    if($Password -eq $null -AND $userName -ne ""){
        Write-Verbose -Message "No Password supplied. Reading from host."
        $Password = Read-Host -Prompt "Enter Password" -AsSecureString
    }


    #Make sure the correct assemblies exist and are loaded
    if(([appdomain]::currentdomain.getassemblies() | Where-Object {$_.ManifestModule -match "Microsoft.SharePoint.Client.dll" -or $_.ManifestModule -match "Microsoft.SharePoint.Client.Runtime.dll"}).Count -ne 2){
        try{
            Write-Verbose "Loading Microsoft.SharePoint.Client Assembly"
            [void] [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SharePoint.Client')
            Write-Verbose "Loading Microsoft.SharePoint.Client.RunTime Assembly"
            [void] [System.Reflection.Assembly]::LoadWithPartialName('Microsoft.SharePoint.Client.RunTime')
        }
        Catch{
            Throw 'Missing Microsoft.SharePoint.Client or Microsoft.SharePoint.Client.RunTime Assembly: These assemblies are required for CSOM access to SharePoint.'
            exit 1
        }
    }


    #Create the CSOM Context
    Write-Verbose -Message "Creating connection to $SiteURL"
    $Context = New-Object Microsoft.SharePoint.Client.ClientContext $SiteURL
    #$CTXCredentials = New-Object System.Net.NetworkCredential($userName,$password,$domain)
    #$Context.Credentials = $CTXCredentials
    if($credential){
        $Context.Credentials = $CTXCredentials
    }
    elseif($userName){
        $CTXCredentials = New-Object System.Net.NetworkCredential($userName,$password,$domain)
        $Context.Credentials = $CTXCredentials
    }


    Write-Verbose -Message "Assign context to globabl variable."
    $Global:SPCSOMContext= $Context
    return $Context

}