ExchangeOnlineShell.psm1

<#
.Synopsis
   Connect to Exchange Online Powershell using Proxy settings or directly.
.DESCRIPTION
   Used to Connect to ExchangeOnlinePowershell.
   It checks if you computer is using any proxy settings and import them from IE if needed.
.EXAMPLE
   Connect-ExchangeOnlineShell
   Connecting to Exchange Online Shell and let the cmdlet to figure out if any Proxy Settings are in place.
.EXAMPLE
   $UserCreds=Get-Credential
   PS C:\>Connect-ExchangeOnlineShell -Credential $UserCreds
   Connecting to Exchange Online Shell using credentials stored in $UserCreds variable and let the cmdlet to figure out if any Proxy Settings are in place.
.EXAMPLE
   Connect-ExchangeOnlineShell -SkipProxyCheck
   Connecting to Exchange Online Shell directly
#>

function Connect-ExchangeOnlineShell
{
    [CmdletBinding(SupportsShouldProcess=$true)]
    [Alias('ceos','Connect-EOShell')]
    Param
    (
        # Credentials Used to Connect to Exchange Online Shell
        [Parameter(Mandatory=$true,
                   Position=0)]
        [System.Management.Automation.PSCredential]$Credential,
        
        #Used to Skip Proxy Settings Check
        [Alias('NoProxy')]
        [Switch]$SkipProxyCheck
    )

    Begin
    {
        $ProxyUsed=$false
        if(!$SkipProxyCheck){
            
            Write-Verbose "Checking Proxy Settings on computer it might take some time. Please be patient"
            Write-Verbose "Proxy Setings Check 1 of 2: Registry Check"
            Write-Verbose "Reading Proxy Settings from Registry"
            $Proxy = Get-ItemProperty -Path 'HKCU:\Software\Microsoft\Windows\CurrentVersion\Internet Settings'
            if($Proxy.ProxyEnable){
                Write-Verbose "Proxy Settings have been detected"
                $ProxyUsed=$true
                break
            }else{
                Write-Verbose "No Proxy Settings detected"
                $ProxyUsed=$false
            }

            Write-Verbose "Proxy Setings Check 2 of 2: Transparent Proxy Check"
            Write-Verbose "If you have a transparent proxy, your computer will not provide any info about it"
            Write-Verbose "Trying to connect to outlook.office365.com via port 443 directly"
            $TCPobj=New-Object System.Net.Sockets.TCPClient
            $Connect=$TCPobj.BeginConnect("outlook.office365.com",443,$null,$null)
            $wait=$Connect.AsyncWaitHandle.WaitOne(2000,$false)
            if(!$wait){
                Write-Verbose "Connection could not be established : TimeOut has been reached"
                Write-Verbose "Most likely your computer is using Transparent Proxy"
                $ProxyUsed=$true
            }else{
                $TCPobj.EndConnect($Connect) | out-Null 
                Write-Verbose "Connection has been established successfully"
                Write-Verbose "No Transparent Proxy settings detected"
                $ProxyUsed=$false
            }
            
        }else{
            Write-Verbose "Skipping Proxy Settings Check and Connecting to outlook.office365.com directly"
        }
    
    }
    Process
    {
        if ($pscmdlet.ShouldProcess( "Exchange Online Management Shell")){
            if($ProxyUsed){
                Write-Verbose "Proxy Server is used: Importing Proxy Settings from IE"
                $proxySettings = New-PSSessionOption -ProxyAccessType IEConfig -ProxyAuthentication basic
                Write-Verbose "Creating Session Object using $($Credential.UserName) credentials"
                $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Credential -Authentication Basic -AllowRedirection -SessionOption $proxySettings -ErrorVariable ExchangeOnlineSessionObjectError
            }else{
                Write-Verbose "No Proxy Detected: Connecting to Exchange Online Shell Directly"
                Write-Verbose "Creating Session Object using $($Credential.UserName) credentials"
                $Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $Credential -Authentication Basic -AllowRedirection -ErrorVariable ExchangeOnlineSessionObjectError
            }

            if($ExchangeOnlineSessionObjectError){
                Write-Verbose "Failed to create a Session Object"
                Write-Verbose "Please double check your credentials and try again"
            }else{
                Write-Verbose "Session Object has been created successfully"
                Write-Verbose "Importing Created Session"
                Import-Module (Import-PSSession $Session -AllowClobber) -Global  
                Write-Verbose "Session has been imported successfully"
                Write-Verbose "Now you are connected to ExchangeOnlieShell"
            }
       }
    }
    End
    {

    }
}

<#
.Synopsis
   Disconnect the Exchange Online Session.
.DESCRIPTION
   Gets all the Sessions with Microsoft.Exchange Configuration established to outlook.office365.com and removes them.
.EXAMPLE
    Disconnect-ExchangeOnlineShell
#>

function Disconnect-ExchangeOnlineShell
{
    [CmdletBinding(SupportsShouldProcess=$true)]
    [Alias('deos','Kill-ExchangeOnlineShellSession','Kill-EOShellSession','Disconnect-EOShell')]
    Param
    (
    )

    Begin
    {
    }
    Process
    {
        if ($pscmdlet.ShouldProcess( "Exchange Online Powershell Sessions")){
            Get-PSSession | Where {$_.ComputerName -eq "outlook.office365.com" -and $_.ConfigurationName -eq "Microsoft.Exchange"} | Remove-PSSession
        }
        
    }
    End
    {
    }
}