Public/Connect-MDSMsolService.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
Function Connect-MDSMsolService {
    <#
    .SYNOPSIS
    Connect to MS Online (Office 365)

    .DESCRIPTION
    Connect to MS Online (Office 365) using an MDSCredential entry, prompt for credentials, or use the currently logged on user credentials.

    .EXAMPLE
    Connect-MDSMsolService -MDSCredential MyCred1

    Connect to MS Online with the MyCred1 credentials created with Add-MDSCredential.

    .EXAMPLE
    Connect-MDSMsolService -Credential MyUserName@domain.com

    Connect to MS Online by prompted for a password for the user MyUserName@domain.com.

    .EXAMPLE
    Connect-MDSMsolService -CurrentCredential

    Connect to MS Online using the currently logged on credentials. Simply does Connect-MsolService -CurrentCredential.

    .NOTES

    #>

    [System.Diagnostics.CodeAnalysis.SuppressMessage('PSAvoidUsingPlainTextForPassword','')]
    [System.Diagnostics.CodeAnalysis.SuppressMessage('PSUsePSCredentialType','')]

    [CmdletBinding(DefaultParameterSetName='MDSCredential')]
    Param (
        [parameter(Position=0,ParameterSetName='MDSCredential')]
        [ValidateNotNullOrEmpty()]
        [String]$MDSCredential,

        [parameter(Position=0,ParameterSetName='Credential')]
        [ValidateNotNullOrEmpty()]
        [System.Management.Automation.CredentialAttribute()]
        $Credential,

        [parameter(Position=0,ParameterSetName='CurrentCredential')]
        [switch]$CurrentCredential
    )

    Begin {}
    Process {
        Try {
            $Parameters = @{
                ErrorAction = 'Stop'
            }

            Switch ($PSCmdlet.ParameterSetName) {
                'Credential' {
                    $Parameters.Add('Credential',$Credential)
                    Write-Verbose "Connect-MDSMsolService using credential $($Credential.Username)"
                    Continue
                }
                'CurrentCredential' {
                    $Parameters.Add('CurrentCredential',$true)
                    Write-Verbose "Connect-MDSMsolService using current credential"
                    Continue
                }
                'MDSCredential' {
                    If ($PSBoundParameters.MDSCredential) {
                        $Credential = Get-MDSCredential -Name $MDSCredential -ErrorAction Stop
                        $Parameters.Add('Credential',$Credential)
                        Write-Verbose "Connect-MDSMsolService using credential $($Credential.Username)"
                    }
                    Continue
                }
                Default {}
            }

            Connect-MsolService @Parameters
        }
        Catch {
            Write-Error $PSItem
        }
    }
    End {}
}