email output of powershell script

10,088

Solution 1

This is but one possible solution; there are many.

[string]$emailBody = ""
$emailBody += "This is line 1<br />"
$emailBody += "This is line 2<br />"
$emailBody += "This is line 3<br />"
Send-MailMessage -From "$senderName <$senderAddr>" -To "$recptName <$recptAddr>" -Subject "$emailSubject" -Body $emailBody -SMTPServer $smtpServer -BodyAsHTML

Does that make sense?

Solution 2

If you wanted to branch out of powershell, you could redirect the output of the script to a text file and use a 3rd party command line mailer like blat to send the text file as the body (or attachment) of an email and specify the smtp server to bounce off of.

Solution 3

My solution isn't pretty, and doesn't use powershell, but I've been using it for years to do exactly what you are trying to achieve.

@echo off
set s1=dfsrsrvr1
set s2=dfsrsrv2

set output=%TEMP%\dfsr.txt
echo DFS Replication Backlog Report>%OUTPUT%
echo.>>%OUTPUT%
echo For each DFS replicated share, any backlog is displayed below.>>%OUTPUT%
echo The first value is the backlog from %S2% to %S1%, the second value is the reverse>>%OUTPUT%
echo.>>%OUTPUT%

echo Accounts>>%OUTPUT%
echo ========>>%OUTPUT%
dfsrdiag backlog /rgname:Accounts /rfname:Accounts /sendingmember:%S2% /receivingmember:%S1% | head -n 2 | tail -n 1 | cut -d: -f2>>%OUTPUT%
dfsrdiag backlog /rgname:Accounts /rfname:Accounts /sendingmember:%S1% /receivingmember:%S2% | head -n 2 | tail -n 1 | cut -d: -f2>>%OUTPUT%

echo.>>%OUTPUT%

blat "%OUTPUT%" -to [email protected] -server mta.example.com -f [email protected] -subject "DFS Replication Report %DATE% %TIME:~0,5%"

It relies upon head, cut and tail from GNU Unix Utils, and blat command line mailer.

It also uses the dfsrdiag utility (which should be on your server) to get the required stats from the DFSR service. In the example, the replication group name is Accounts, adjust to taste/add more replication groups as required.

Share:
10,088

Related videos on Youtube

Gordon Carlisle
Author by

Gordon Carlisle

Updated on September 18, 2022

Comments

  • Gordon Carlisle
    Gordon Carlisle over 1 year

    I found this wonderful script that outputs the status of the current DFS backlog to the powershell console. This works great, but I need the script to email me so I can schedule it to run nightly. I have tried using the Send-MailMessage command, but can't get it to work. Mainly because my powershell skills are very weak. I believe most of the issue revolve around the script using the Write-Host command. While the coloring is nice I would much rather have it email me the results. I also need the solution to be able to specify a mail server since the dfs servers don't have email capability.

    Any help or tips are welcome and appreciated.

    Here is the code.

    $RGroups = Get-WmiObject -Namespace "root\MicrosoftDFS" -Query "SELECT * FROM DfsrReplicationGroupConfig"
    $ComputerName=$env:ComputerName
    $Succ=0
    $Warn=0
    $Err=0
    
    foreach ($Group in $RGroups)
    {
    $RGFoldersWMIQ = "SELECT * FROM DfsrReplicatedFolderConfig WHERE     ReplicationGroupGUID='" + $Group.ReplicationGroupGUID + "'"
    $RGFolders = Get-WmiObject -Namespace "root\MicrosoftDFS" -Query  $RGFoldersWMIQ
    $RGConnectionsWMIQ = "SELECT * FROM DfsrConnectionConfig WHERE ReplicationGroupGUID='"+     $Group.ReplicationGroupGUID + "'"
    $RGConnections = Get-WmiObject -Namespace "root\MicrosoftDFS" -Query  $RGConnectionsWMIQ
    foreach ($Connection in $RGConnections)
    {
    $ConnectionName = $Connection.PartnerName.Trim()
    if ($Connection.Enabled -eq $True)
    {
    if (((New-Object System.Net.NetworkInformation.ping).send("$ConnectionName")).Status -eq "Success")
    {
    foreach ($Folder in $RGFolders)
    {
    $RGName = $Group.ReplicationGroupName
    $RFName = $Folder.ReplicatedFolderName
    
    if ($Connection.Inbound -eq $True)
    {
    $SendingMember = $ConnectionName
    $ReceivingMember = $ComputerName
    $Direction="inbound"
    }
    else
    {
    $SendingMember = $ComputerName
    $ReceivingMember = $ConnectionName
    $Direction="outbound"
    }
    
    $BLCommand = "dfsrdiag Backlog /RGName:'" + $RGName + "' /RFName:'" + $RFName + "' /SendingMember:" + $SendingMember + " /ReceivingMember:" + $ReceivingMember
    $Backlog = Invoke-Expression -Command $BLCommand
    
    $BackLogFilecount = 0
    foreach ($item in $Backlog)
    {
    if ($item -ilike "*Backlog File count*")
    {
    $BacklogFileCount = [int]$Item.Split(":")[1].Trim()
    }
    }
    
    $Emailbody += "$BacklogFileCount files in backlog $SendingMember->$ReceivingMember for $RGName
    "
    
    } # Closing iterate through all folders
    } # Closing  If replies to ping
    } # Closing  If Connection enabled
    } # Closing iteration through all connections
    } # Closing iteration through all groups
    
    $emailFrom = "[email protected]"
    $emailTo = "[email protected]"
    $subject = "DFS Backlog Report"
    $smtpServer = "MailServer"
    $smtp = new-object Net.Mail.SmtpClient($smtpServer)
    $smtp.Send($emailFrom, $emailTo, $subject, $Emailbody)
    
    • Admin
      Admin about 12 years
      What version of PowerShell are you running? if (test-path variable:psversiontable) {$psversiontable.psversion} else {[version]"1.0.0.0"}
  • jscott
    jscott about 12 years
    The Send-MailMessage cmdlet can do all of this without relying on other binaries.
  • Rex
    Rex about 12 years
    I know.. but some people aren't fully comfortable in powershell and can copy/paste a script but don't want to edit it more than necessary. I was just providing another option for those people. :)
  • jscott
    jscott about 12 years
    Certainly, and +1 for the suggestion. But it seems, to me, it would be "cleanest" to keep all operations within the scope of the same script.
  • Rex
    Rex about 12 years
    Yup - it would be better to keep it all in one script. I just know I've worked with people that have an unnatural fear of even breathing on the computer running the script let alone modify a script they know works no matter how minor the change might be. :)
  • Gordon Carlisle
    Gordon Carlisle about 12 years
    Thanks the $emailBody += was helpful in finding a solution to the problem. I have updated the code to reflect how I got the report to email.
  • jscott
    jscott about 12 years
    @GordonCarlisle If you were able to resolve this, you should post the details as an answer and mark it as "accepted".