What's In Your Profile - #2 - Exchange Region

Hello again everyone! For installment number two of What's In Your Profile, I'll share my Exchange Region. This only allows me to connect/disconnect from Exchange On Prem/365, but is still something I use daily.

I know it's nothing too particularly exciting, but it is still part of my $Profile for easy access!

There are two functions 'find-emails' and 'find-emailsmanual' that are quick and easy ways to access the Exchange Tracking Logs for the typical "Did person X send me an email this morning?" questions.

Hope this helps!

#region Exchange 
Function Connect-ExchangeOnline
           [Parameter(Mandatory = $False)]
           [System.Management.Automation.PsCredential]$Credential = $Host.UI.PromptForCredential("Enter Global Admin Credential",
           "Enter the username and password of an Exchange Online Global Administrator account.",

           [Parameter(Mandatory = $False)]
           [System.Uri]$Uri = "https://ps.outlook.com/powershell/"

     $session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri $Uri -Credential $Credential -Authentication Basic -AllowRedirection
     Import-PSSession $session -AllowClobber
     Return $session

Function Disconnect-ExchangeOnline
     Get-PSSession | ?{$_.ComputerName -like "*outlook.com"} | Remove-PSSession

function find-emailsmanual {
    $history = read-host How many hours previously do you want to search?; 
    $sender_id = Read-host From who?; 
    $rec_id = read-host To who?; 
    $now = get-date; 
    Get-MessageTrackingLog `
        -Sender $sender_id `
        -Recipients $rec_id `
        -Start $now.AddHours(-$history)
function find-emails([string]$email_backtime, [string]$email_from, [string]$email_to) {
 $now = get-date; 
 Get-MessageTrackingLog -Sender $email_from -Recipients $email_to -Start $now.AddHours(-$email_backtime)

#connect to exchange On Prem
if (Test-Path 'C:\Program Files\Microsoft\Exchange Server\V14\Bin\RemoteExchange.ps1') { 
     Import-Module 'C:\Program Files\Microsoft\Exchange Server\V14\bin\RemoteExchange.ps1';
     Write-Host "Type in the command Connect-ExchangeServer -auto; to connect to Exchange Online" -BackgroundColor Black -ForegroundColor Green
     } else {
          Write-Warning "Exchange Management Shell Not Installed";

#endregion Exchange 


Popular Posts