helpers/Connect-O365Sharepoint.ps1

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
function Connect-O365Sharepoint
{
    PARAM
    (
        [System.Management.Automation.PSCredential]
        $Credential,
        [string]
        $TenantName
    )
    
    try
    {
        Import-Module -Name Microsoft.Online.Sharepoint.Powershell -DisableNameChecking -Force -ErrorAction Stop
    }
    catch
    {
        Write-Error -Message "The module 'Microsoft.Online.Sharepoint.Powershell' was not found. A connection to Sharepoint Online will not be established"
    }

    $Params = @{
        Url = "https://$($PSBoundParameters.TenantName)-admin.sharepoint.com"
        Credential = $Credential
        WarningAction = 'SilentlyContinue'
    }
    Connect-SPOService @Params
}