Public/Connect-ExchangeSession.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
##################################################################################################
# Connect-ExchangeSession
# Creates a session that connects to Exchange server
##################################################################################################
Function Connect-ExchangeSession(){
    #Connecting to Exchange Online
    Set-ExecutionPolicy -ExecutionPolicy RemoteSigned

    #Capture administrative credential for future connections.
    $credential = get-credential

    #Creates an Exchange Online session
    $ExchangeSession = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $credential -Authentication  Basic -AllowRedirection

    #Import session commands
    Import-PSSession $ExchangeSession

    #Return these values so I can use the same credentials for other connections and turn off the PSSession when I am done with it
    $result = @{}
    $result.Add("Credential", $Credential)
    $result.Add("Session", $ExchangeSession)

    return $result
}