functions/Connect-O365.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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
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
            }
        }
    }
}