Friday, 3 August 2018

Microsoft Exchange Online Powershell



Connect to Exchange online PowerShell for Windows:
$UserCredential = Get-Credential "hostadmin@mycompany.com.my"

$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri https://outlook.office365.com/powershell-liveid/ -Credential $UserCredential -Authentication Basic -AllowRedirection

Import-PSSession $Session

To remove sessions:
Get-PSSession | Remove-PSSession

#########################################################
Microsoft Exchange Online Powershell Module:
You will need a Windows machine with the latest version of Powershell. You will need to specifically install Exchange Online Powershell with MFA Support. This can be downloaded from
https://docs.microsoft.com/en-us/powershell/exchange/exchange-online/connect-to-exchange-online-powershell/mfa-connect-to-exchange-online-powershell?view=exchange-ps
OR

When running these Powershell scripts you will see a message along the lines of “WARNING: The names of…” – this is an issue with Microsoft not documenting their Official Powershell scripts well enough! But it is not an issue to worry about.












#Import the right Exchange Online module to talk with Azure AD
Import-Module $((Get-ChildItem -Path $($env:LOCALAPPDATA+"\Apps\2.0\") -Filter Microsoft.Exchange.Management.ExoPowershellModule.dll -Recurse ).FullName|?{$_ -notmatch "_none_"}|select -First 1)
#Auth to the tenant and setup powershell
$ExoSession = New-ExoPSSession
Import-PSSession $ExoSession -AllowClobber


#########################################################



Content Search in Office 365 Security & Compliance:





Deletes the search results returned by a content search named Remove Phishing Message:
















PS C:\>Import-Module $((Get-ChildItem -Path $($env:LOCALAPPDATA+"\Apps\2.0\") -Filter Microsoft.Exchange.Management.ExoPowershellModule.dll -Recurse ).FullName|?{$_ -notmatch "_none_"}|select -First 1)
PS C:\>$ExoSession = New-ExoPSSession -ConnectionUri https://ps.compliance.protection.outlook.com/powershell-liveid
PS C:\>Import-PSSession $ExoSession -AllowClobber
PS C:\>New-ComplianceSearchAction -SearchName "Remove Phishing Message" -Purge -PurgeType SoftDelete



Phishingcleanup.ps1 Script
param (

    [Parameter(Mandatory=$true)][string]$ComplianceName
)

Import-Module $((Get-ChildItem -Path $($env:LOCALAPPDATA+"\Apps\2.0\") -Filter Microsoft.Exchange.Management.ExoPowershellModule.dll -Recurse ).FullName|?{$_ -notmatch "_none_"}|select -First 1)
$ExoSession = New-ExoPSSession -ConnectionUri https://ps.compliance.protection.outlook.com/powershell-liveid
Import-PSSession $ExoSession -AllowClobber

New-ComplianceSearchAction -SearchName $ComplianceName -Purge -PurgeType SoftDelete

Get-PSSession | Remove-PSSession