functions/Connect-O365.ps1

function Connect-O365
{
    [CmdletBinding()]
    PARAM
    (
        [parameter(Mandatory = $true)]
        [ValidateSet("AzureActiveDirectory","Exchange","ComplianceCenter","Sharepoint","Skype","*")]
        [string[]]
        $Services,

        [parameter(Mandatory = $true)]
        [System.Management.Automation.Credential()]
        [System.Management.Automation.PSCredential]
        $Credential
    )

    DYNAMICPARAM
    {
        $Dictionary = [System.Management.Automation.RuntimeDefinedParameterDictionary]::new()
        
        if ($PSBoundParameters.Services -contains "Sharepoint" -or $PSBoundParameters.Services -contains "*")
        {
            $Name = "TenantName"
            $ParamAttr = [System.Management.Automation.ParameterAttribute]::new()
            $ParamAttr.Mandatory = $true
            $Attributes = [Collections.ObjectModel.Collection[System.Attribute]]::new()
            $Attributes.Add($ParamAttr)
            $Parameter = [System.Management.Automation.RuntimeDefinedParameter]::new($Name, [string], $Attributes)
            $Dictionary.Add($Name, $Parameter)
        }

        if ($PSBoundParameters.Services -contains "AzureActiveDirectory" -or $PSBoundParameters.Services -contains "*")
        {
            $Name = "UseMSOnlineModule"
            $ParamAttr = [System.Management.Automation.ParameterAttribute]::new()
            $ParamAttr.Mandatory = $false
            $Attributes = [Collections.ObjectModel.Collection[System.Attribute]]::new()
            $Attributes.Add($ParamAttr)
            $Parameter = [System.Management.Automation.RuntimeDefinedParameter]::new($Name, [switch], $Attributes)
            $Dictionary.Add($Name, $Parameter)
        }

        $Dictionary
    }

    BEGIN
    {
        if ($Services -eq "*")
        {
            $Services = $AllServices
        }
    }

    PROCESS
    {
        switch ($Services)
        {
            { $_ -contains "AzureActiveDirectory" }
            {
                $Splat = @{
                    Credential = $Credential
                }
                if ($PSBoundParameters.ContainsKey("UseMSOnlineModule"))
                {
                    $Splat.Add("UseMSOnlineModule", $true)
                }
                Connect-O365AzureAD @Splat
            }

            { $_ -contains "Exchange" }
            {
                Connect-O365Exchange -Credential $Credential
            }

            { $_ -contains "Skype" }     
            {
                Connect-O365Skype -Credential $Credential
            }

            { $_ -contains "Sharepoint" }
            {
                Connect-O365Sharepoint -Credential $Credential -TenantName $PSBoundParameters.TenantName
            }

            { $_ -contains "ComplianceCenter" }
            {
                Connect-O365Compliance -Credential $Credential
            }
        }
    }
}