Quantcast
Channel: TechNet Blogs
Viewing all articles
Browse latest Browse all 36188

How to test mail flow from SharePoint with PowerShell

$
0
0

Summary:

The following PowerShell script was taken from my post on configuring TLS between SharePoint and Exchange. However, since it was buried at step 22, I wanted to create a separate post just sharing the script.

Why?

I find this script very useful when testing mail flow from Sharepoint since it uses the "SPUtility::SendEmail" API , sends mail, capture logs and present them by launching notepad from a single server.

The Script:

# check to ensure Microsoft.SharePoint.PowerShell is loaded 
 $snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
 if ($snapin -eq $null) {
 Write-Host "Loading SharePoint Powershell Snapin"
 Add-PSSnapin Microsoft.SharePoint.Powershell
 }

#E-Mail Parameters:
 $email = "Administrator@contoso.com"
 $subject = "Test from SharePoint"
 $body = "This is the body"

#SPWeb URL
 $web = Get-SPWeb "http://SPWEBURL"

#specify start time of action
 $StartTime = (Get-Date).AddMinutes(-1).ToString()

# Try sending e-mail via SharePoint.
 $send = [Microsoft.SharePoint.Utilities.SPUtility]::SendEmail($web,0,0,$email,$subject,$body)
 
#what to do if it fails
 if ($send -eq $false -and $web -ne $null){
 write-host "It didn't work, checking ULS for errors. Please stand by..." -foregroundcolor Red -backgroundcolor Yellow

#specify end time of action
 $EndTime = (Get-Date).AddMinutes(+1).ToString()

#make dir if it does not exist
 $TARGETDIR = "c:logs"
 if(!(Test-Path -Path c:logs)){
 New-Item -ItemType directory -Path $TARGETDIR
 }

#finding error and creating log
 start-sleep 5
 Get-SPLogEvent -StartTime $StartTime -EndTime $EndTime | Where-Object {$_.Category -eq "E-Mail"} | Export-Csv -LiteralPath "$TARGETDIRlog.csv"

#starting notepad to open log
 start notepad.exe "$TARGETDIRlog.csv"
}

#what to do if it works
 else{

if ($send -eq $true -and $web -ne $null){
 write-host "It Worked..Congrats!" -foregroundcolor DarkGreen -backgroundcolor White
 }

$send = "false"
 if($send -eq $false -and $web -eq $null){
 write-host "Is the WEB url correct?" -foregroundcolor Red -BackgroundColor White
 }
 }

Example:

 

I hope you find this useful and thanks for reading!

-Mike


Viewing all articles
Browse latest Browse all 36188

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>