new-MSGraphPowershellSession.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
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
<#
    .SYNOPSIS
 
    This function creates the powershell session to msGraph AD.
 
    .DESCRIPTION
 
    This function creates the powershell session to msGraph AD.
 
    .PARAMETER msGraphADCredential
 
    The credential utilized to connect to msGraph ad.
 
    .PARAMETER msGraphCertificateThumbprint
 
    The certificate thumbprint for the associated msGraph application.
 
    .PARAMETER msGraphTenantID
 
    The tenant ID associated with the msGraph application.
 
    .PARAMETER msGraphApplicationID
 
    The application ID for msGraph management.
 
    .PARAMETER msGraphEnvironmentName
 
    The msGraph environment for the connection to msGraph ad.
 
    .OUTPUTS
 
    Powershell session to use for exchange online commands.
 
    .EXAMPLE
 
    new-msGraphADPowershellSession -msGraphADCredential $CRED -msGraphEnvironmentName NAME
 
    #>

    Function New-MSGraphPowershellSession
     {
        [cmdletbinding()]

        Param
        (
            [Parameter(ParameterSetName = "UserCredentials",Mandatory = $false)]
            [pscredential]$msGraphADCredential=$NULL,
            [Parameter(ParameterSetName = "CertificateCredentials",Mandatory = $true)]
            [string]$msGraphCertificateThumbPrint,
            [Parameter(ParameterSetName = "CertificateCredentials",Mandatory = $true)]
            [string]$msGraphTenantID,
            [Parameter(ParameterSetName = "CertificateCredentials",Mandatory = $true)]
            [string]$msGraphApplicationID,
            [Parameter(ParameterSetName = "UserCredentials",Mandatory = $true)]
            [Parameter(ParameterSetName = "CertificateCredentials",Mandatory = $true)]
            [string]$msGraphEnvironmentName,
            [Parameter(ParameterSetName = "UserCredentials",Mandatory = $false)]
            [Parameter(ParameterSetName = "CertificateCredentials",Mandatory = $false)]
            [boolean]$isAudit=$FALSE,
            [Parameter(ParameterSetName = "CertificateCredentials",Mandatory = $true)]
            [array]$msGraphScopesRequired=@()
        )

        #Output all parameters bound or unbound and their associated values.

        write-functionParameters -keyArray $MyInvocation.MyCommand.Parameters.Keys -parameterArray $PSBoundParameters -variableArray (Get-Variable -Scope Local -ErrorAction Ignore)

        #Define variables that will be utilzed in the function.

        [boolean]$isCertAuth=$false
        #$exchangeOnlineCommands=@('get-ExoRecipient','new-distributionGroup','get-recipient','set-distributionGroup','get-distributionGroupMember','get-mailbox','get-unifiedGroup','set-UnifiedGroup')
        #Initiate the session.
        
        Out-LogFile -string "********************************************************************************"
        Out-LogFile -string "BEGIN NEW-msGraphADPowershellSession"
        Out-LogFile -string "********************************************************************************"

        if ($msGraphCertificateThumbPrint -ne "")
        {
            $isCertAuth=$true
            out-logfile -string ("Is certificate auth = "+$isCertAuth)
        }

        if ($isCertAuth -eq $False)
        {
            if ($msGraphADCredential -ne $NULL)
            {
                try 
                {
                    Out-LogFile -string "Creating the msGraph active directory powershell session."

                    Connect-msGraphAD -Credential $msGraphADCredential -msGraphEnvironmentName $msGraphEnvironmentName
                }
                catch 
                {
                    Out-LogFile -string $_ -isError:$TRUE -isAudit $isAudit
                }
            }
            else
            {
                try 
                {
                    Out-LogFile -string "Creating the msGraph active directory powershell session."

                    Connect-msGraphAD -msGraphEnvironmentName $msGraphEnvironmentName
                }
                catch 
                {
                    Out-LogFile -string $_ -isError:$TRUE -isAudit $isAudit
                }
            }
        }
        elseif ($isCertAuth -eq $TRUE) 
        {
            try 
            {
                out-logfile -string "Creating the connection to exchange online powershell using certificate authentication."

                connect-mgGraph -certificateThumbprint $msGraphCertificateThumbPrint -ClientId $msGraphApplicationID -tenantID $msGraphTenantID -environment $msGraphEnvironmentName
            } 
            catch 
            {
                out-logfile -string $_ -isError:$TRUE -isAudit $isAudit
            }
        }
               
        Out-LogFile -string "The exchange online powershell session was created successfully."

        Out-LogFile -string "END NEW-msGraphADPOWERSHELL SESSION"
        Out-LogFile -string "********************************************************************************"
    }