Get-MessageTracking log issue.

Hi ,
I have an issue with a really critical script that basically runs the "Get-MessageTrackingLog' command on an Exchange 2010 cluster.
The way I'm Setup is according to the following scenario:
I have 2 load balanced  CAS servers that relay mail to two MailBox servers.
The problem is that when mail arrives to the cluster it is load balanced ether to CAS nod A or to CAS node B.
The problem is that when I run the "Get-MessageTrackingLog'" CmdLet against Node A, i wont see the tracking log info that came via Node B . So now I have to run the same script twice...Once with Nod A and once with Node B...
Is their a way to overcome this issue?
Thanks for your help.

Jason.
Elad-ItAsked:
Who is Participating?
 
Chris DentConnect With a Mentor PowerShell DeveloperCommented:

Oops... I meant to add a line break (<br />).

You prefer to exclude those with 0 mails? This version excludes them.

Chris
#=======================================Variables=====================================================

$StartDate = (Get-Date).AddDays(-2)
$EndDate = (Get-Date).AddDays(-1)

$DomainName = "@Domain.com"

$HtmlHead = "
  <title>Daily report E-Mail Item count</title>
  <style type='text/css'>
    body   { background-color: peachpuff }
    table  { border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse }
    th, td { border-width: 1px; border-style: solid; border-color: black; background-color: palegoldenrod }
  </style>"

$Servers = "CAS01", "CAS02"

#
#=======================================Path==========================================================
#

cd C:\scripts\dfs

#
#======================================Get Tracking Logs=============================================
#

$HtmlBody = @()
$HtmlBody += "<h2>Daily report E-Mail Item count</h2>"

Get-Content "list.txt" | %{
  $Alias = $_

  $MailReport = $Servers | %{
    Get-MessageTrackinglog -ResultSize Unlimited -EventID "RECEIVE" `
      -Recipient "$Alias$DomainName" -Server $_ -Start $StartDate -End $EndDate
  }

  $Count = ($MailReport | Measure-Object).Count

  # If the user has no mails a file will not be linked
  If ($MailReport -ne $Null) {
    $MailReport | Export-CSV "C:\Scripts\dfs\csvs\$Alias.csv" -NoTypeInformation
    $FilePath = "to <a href='file://///ShareServer/MailReport/$Alias.csv'>$Alias.csv</a>"

    $HtmlBody += "From: $StartDate To: $EndDate there were $Count E-Mail's $FilePath<br />"
  }
}

$MessageBody = ConvertTo-Html -Head $HtmlHead -Body $HtmlBody

#
#======================================Generate Mail report===========================================
#                      PowerShell 2 will replace this section with Send-MailMessage

$SMTPServer = "127.0.0.1"
$From = "reporter@domain.com"
$To = "me@domain.com"

$MailMessage = New-Object Net.Mail.MailMessage($From, $To)
$MailMessage.Subject = "Daily report E-Mail Item count."
$MailMessage.IsBodyHtml = $True
$MailMessage.Body = $HTMLBody
(New-Object Net.Mail.SmtpClient($SMTPServer)).Send($MailMessage)

start-sleep -s 300
#==================================Generate CSV Script Sender==========================================
$e = get-content "C:\Scripts\dfs\part1.txt"
$f = get-content "C:\Scripts\dfs\part2.txt"
Get-ChildItem C:\Scripts\dfs\csvs | foreach-object {echo $e$_" "$f >>sendcsvs.ps1}
start-sleep -s 10
#===================================Send me the CSV====================================================
./"C:\Scripts\dfs\sendcsvs.ps1"
cd E:\count\MBX
./s1.ps1
#===================================End========================================================================

Open in new window

0
 
Hilal1924Commented:
Put your computers (both of them in a text file called cmp.txt in C:\cmp.txt) and then run the following command
Get-Content C:\cmp.txt | Get-MessageTrackingLog
 
:)
Hilal
0
 
Elad-ItAuthor Commented:
Thanks for your reply.
But will that not just also create two similar reports ( one for each server)?
My question is how would I query both as a fedaration, thus geting an agregated report..
IE, insted of getting a report from server A and finding out that Jimmy got 3 emails on that server and on the other server he got 1 email,
I'd an aggregated report saying Jimmy had got 4 mails...
Thanks!
Jason
0
Creating Active Directory Users from a Text File

If your organization has a need to mass-create AD user accounts, watch this video to see how its done without the need for scripting or other unnecessary complexities.

 
Hilal1924Commented:
would it help if you did like this
Get-Content c:\cmp.txt |Get-mrssagetrackinglog |export-csv c:\track.csv

then all you have to do is.just sort them. I will write a script for you, but I already in bed. so I will write it first thing tomorrow.
Hilal
0
 
Elad-ItAuthor Commented:
Thanks!!
Good night.
Sleep well.
0
 
Hilal1924Commented:
OK This is sort of workaround with minimum effort required. This Command will export the Message Tracking Log to a CSV file or an HTML file (based on the format you like). Both Logs will be merged in the exported file and will be sorted according to ServerHostName. You can Substitute ServerHostName with Sender, Subject, ClientIp, Recipients etc, whatever you like. Say for Example In your reference you want to find all mail sent to Jimmy, Then all you have to do is replace "ServerHostName" with "Recipients" and it will sort the tracking log according to recipients alphabetically.
Hilal

Get-Content c:\cmp.txt |Get-MessageTrackingLog -Server EX2007 |sort ServerHostName|ConvertTo-Html |Out-File C:\Track.html 

Get-Content c:\cmp.txt |Get-MessageTrackingLog -Server EX2007 |sort ServerHostName|ConvertTo-csv |Out-File C:\Track.csv 

Get-Content c:\cmp.txt |Get-MessageTrackingLog -Server EX2007 |sort Recipients|ConvertTo-Html |Out-File C:\Track.html 

Get-Content c:\cmp.txt |Get-MessageTrackingLog -Server EX2007 |sort Recipients|ConvertTo-csv |Out-File C:\Track.csv

Open in new window

0
 
Elad-ItAuthor Commented:
You see, my script is very intricate and your approch will cause too much manual aggregation..
See my snippet - so youll see what i mean.

# Date Variables
$StartDate = (get-date).AddDays(-2) 
$EndDate = (get-date).AddDays(-1) 
$b = "@domain2.com"
$a = "<style>"
$a = $a + "BODY{background-color:peachpuff;}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:palegoldenrod}"
$a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:palegoldenrod}"
$a = $a + "</style>"

cd /
e:
cd count
cd scripts
cd test

Get-Content "feedlist.txt" | %{
  Get-MessageTrackinglog -ResultSize Unlimited -EventID "RECEIVE" `
    -Recipient "$_$b" -Server afcc-ex-il1 -Start $StartDate -End $EndDate `
    | Export-CSV E:\count\Scripts\Test\csvs\"$_.csv"

$count = Get-Content e:\count\scripts\test\csvs\"$_.csv"
$namecount = $count.Length-2
#Echo "Start Date,End Date,There were,amount,E-Mails To:, Alias Name" >> e:\count\scripts\test\sunday1.csv
Echo " From: $StartDate , To: $EndDate ,there were , $namecount , E-Mail's To , $_ , \\ShareServer\DepartmentName\share\FeedReport\$_.csv" >> e:\count\scripts\test\sunday1.csv
}
Import-Csv sunday1.csv | ConvertTo-Html -head $a –body "<H2>Daily Feed E-Mail Item count</H2>" | Out-File e:\count\scripts\test\workalready.htm

$HTMLBody = Get-Content "E:\count\Scripts\Test\workalready.htm"
$SMTPServer = "10.0.0.6"
$From = "reporter@domain2.com"
$To = "getreport@domain1.com"
#$Subject = "Daily  E-Mail Item count per mailbox."
$MailMessage = New-Object System.Net.Mail.MailMessage($From, $To)
#$MailMessage = New-Object System.Net.Mail.MailMessage($From, $To, $Subject)
$MailMessage.Subject = "Daily Feed E-Mail Item count."
$MailMessage.IsBodyHtml = $True
$MailMessage.Body = $HTMLBody
 
(New-Object System.Net.Mail.SmtpClient($SMTPServer)).Send($MailMessage)
start-sleep -s 300
$e = get-content part1.txt
$f = get-content part2.txt
Get-ChildItem E:\count\Scripts\Test\csvs | foreach-object {echo $e$_" "$f >>sendcsvs.ps1}
start-sleep -s 10
./sendcsvs.ps1
cd \
cd E:\count
cd MBX
./s1.ps1

Open in new window

0
 
Chris DentPowerShell DeveloperCommented:

Hey Jason,

You want a loop for the servers. Something vaguely like this.

Chris
Get-Content "feedlist.txt" | %{
  # We're about to lose the pipeline variable to the server name so store this one in
  # a fixed variable
  $Feed = $_

  # Server names, and ForEach-Object to keep output in the pipeline.
  "Server1" , "Server" | %{

    # Get the logs from each server
    Get-MessageTrackinglog -ResultSize Unlimited -EventID "RECEIVE" `
      -Recipient "$Feed$b" -Server $_ -Start $StartDate -End $EndDate

  # Save the combined output to $Feed.csv.
  } | Export-CSV E:\count\Scripts\Test\csvs\"$Feed.csv"

  $count = Get-Content e:\count\scripts\test\csvs\"$_.csv"
  $namecount = $count.Length-2
  #Echo "Start Date,End Date,There were,amount,E-Mails To:, Alias Name" >> e:\count\scripts\test\sunday1.csv
  Echo " From: $StartDate , To: $EndDate ,there were , $namecount , E-Mail's To , $_ , \\ShareServer\DepartmentName\share\FeedReport\$_.csv" >> e:\count\scripts\test\sunday1.csv
}

Open in new window

0
 
Elad-ItAuthor Commented:
Chris, It seems to be the right thing to do... I just wish there was a way to savy this out... you know, make it much simpler.
re-phrasing the above script is difficult :-)
Trying to implement your advice..
J
0
 
Chris DentPowerShell DeveloperCommented:

Some bits can be simplified. If you're running PowerShell 2 you have Send-MailMessage, that'll drop off some of end of your script.

I'm sure it will be possible to simplify it further. What do you have in that feedlist file? Just the recipient you're wanting to track?

Chris
0
 
Elad-ItAuthor Commented:
yes it's a list of aliases  and the $b value pasts in the @domain.com suffix.
J
0
 
Chris DentPowerShell DeveloperCommented:

Okay, makes sense.

I would add the mail part into the same section. For example...

How do you determine who gets the mail? And it definitely needs to be a CSV file? What does sendcsv do?

Chris
Get-Content "feedlist.txt" | %{
  # We're about to lose the pipeline variable to the server name so store this one in
  # a fixed variable
  $Feed = $_

  # Server names, and ForEach-Object to keep output in the pipeline.
  "Server1" , "Server" | %{

    # Get the logs from each server
    Get-MessageTrackinglog -ResultSize Unlimited -EventID "RECEIVE" `
      -Recipient "$Feed$b" -Server $_ -Start $StartDate -End $EndDate

  # Save the combined output to $Feed.csv.
  }

  # Send the mail message here
  Send-MailMessage <Parameters>
}

Open in new window

0
 
Hilal1924Commented:
I would go with Chris's Comment.
 
Hilal
0
 
Elad-ItAuthor Commented:
Chris,

Didn't get too far with your suggestion, and not because the suggestion is wrong :)
Id Split the script into to
Part one retrieves tracking log, writes them to two locations and combines the output.
Part two creates the report CSV comprised of each alias and a count of the Emails it receives.

The problem is with part 2 - where am i going wrong?
Jason.





=========First Part===============
# Date Variables
$StartDate = (get-date).AddDays(-2) 
$EndDate = (get-date).AddDays(-1)
$b = "@domainsuffix.com"
cd /
C:
cd Scripts
cd DailyFeedScript
Get-Content "feedlist.txt" | %{
  Get-MessageTrackinglog -ResultSize Unlimited -EventID "RECEIVE" `
    -Recipient "$_$b" -Server CAS01 -Start $StartDate -End $EndDate `
    | Export-CSV C:\Scripts\DFS\csvs\01\"$_.csv" -NoTypeInformation
}
Get-Content "feedlist.txt" | %{
  Get-MessageTrackinglog -ResultSize Unlimited -EventID "RECEIVE" `
    -Recipient "$_$b" -Server CAS02 -Start $StartDate -End $EndDate `
    | Export-CSV C:\Scripts\SFS\csvs\02\"$_.csv" -NoTypeInformation
}
get-childitem "C:\Scripts\DFS\csvs\02\" | foreach-object {echo "gc C:\Scripts\DFS\csvs\02\$_ >> C:\Scripts\DFS\csvs\01\$_"} |Out-File "C:\Scripts\SFS\overwrite.ps1"
./C:\Scripts\DFS\overwrite.ps1

===========Part2==========
get-childitem "C:\Scripts\DFS\csvs\01\" |foreach-object {$count = Get-Content "C:\Scripts\DFSt\csvs\01\$_",$namecount = $count.Length-1}
$namecount = $count.Length-1
Echo " From: $StartDate , To: $EndDate ,there were , $namecount , E-Mail's To , $_ , \\ShareServer\MailReport\$_" >> C:\Scripts\DFS\FULL.csv

Open in new window

0
 
Chris DentPowerShell DeveloperCommented:

I'm trying to remember what was in the first file ;)

For the count you can just do:

Get-ChildItem "C:\Scripts\DFS\csvs\01\" | %{
  Echo " From: $StartDate , To: $EndDate ,there were , $((Import-CSV $_.FullName).Count), E-Mail's To , $_ , \\ShareServer\MailReport\$_" >> C:\Scripts\DFS\FULL.csv
 }


We'd need to fix the use of $_ there, not sure what you have in that at the moment.

Is that what you really want though? If it's mail sent to particular recipients then Group-Object may be a better way to go.

Chris
0
 
Elad-ItAuthor Commented:
If you don't remember, who am I to prevent you  :-) (pasted below).
Its a bit awkward ..and it half works right now...
#=======================================Variables=====================================================
$StartDate = (get-date).AddDays(-2) 
$EndDate = (get-date).AddDays(-1) 
$b = "@Domain.com"
$a = "<style>"
$a = $a + "BODY{background-color:peachpuff;}"
$a = $a + "TABLE{border-width: 1px;border-style: solid;border-color: black;border-collapse: collapse;}"
$a = $a + "TH{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:palegoldenrod}"
$a = $a + "TD{border-width: 1px;padding: 0px;border-style: solid;border-color: black;background-color:palegoldenrod}"
$a = $a + "</style>"
#=======================================Path==========================================================
cd /
C:
cd Scripts
cd dfs
#
#
#======================================Get Tracking Logs=============================================
Get-Content "list.txt" | %{
  Get-MessageTrackinglog -ResultSize Unlimited -EventID "RECEIVE" `
    -Recipient "$_$b" -Server CAS01 -Start $StartDate -End $EndDate `
    | Export-CSV C:\Scripts\dfs\csvs\01\"$_.csv" -NoTypeInformation
}
Get-Content "list.txt" | %{
  Get-MessageTrackinglog -ResultSize Unlimited -EventID "RECEIVE" `
    -Recipient "$_$b" -Server CAS02 -Start $StartDate -End $EndDate `
    | Export-CSV C:\Scripts\dfs\csvs\02\"$_.csv" -NoTypeInformation
}
#======================================File Consolidation===&Report Body=============================
#
#===================overwrite 01 with 02==================================================
#
get-childitem "C:\Scripts\dfs\csvs\02\" | foreach-object {echo "gc C:\Scripts\dfs\csvs\02\$_ >> C:\Scripts\dfs\csvs\01\$_"} |Out-File "C:\Scripts\dfs\overwrite.ps1"
./overwrite.ps1
#
#===================create Mail Body data to FUll.csv=================================================
#
Get-ChildItem "C:\Scripts\dfs\csvs\01\" | %{
  Echo " From: $StartDate , To: $EndDate ,there were , $((Import-CSV $_.FullName).Count), E-Mail's To , $_ , \\ShareServer\MailReport\$_ " >> C:\Scripts\dfs\FULL.csv
 }
start-sleep -s 300
#
#======================================Create HTML Mail Body==========================================
#
#
Import-Csv C:\Scripts\dfs\FULL.csv | ConvertTo-Html -head $a –body "<H2>Daily report E-Mail Item count</H2>" | Out-File C:\Scripts\dfs\MailBody.htm
#
#======================================Generate Mail report===========================================
$HTMLBody = Get-Content "C:\Scripts\dfs\MailBody.htm"
$SMTPServer = "127.0.0.1"
$From = "reporter@domain.com"
$To = "me@domain.com"
$MailMessage = New-Object System.Net.Mail.MailMessage($From, $To)
$MailMessage.Subject = "Daily report E-Mail Item count."
$MailMessage.IsBodyHtml = $True
$MailMessage.Body = $HTMLBody
(New-Object System.Net.Mail.SmtpClient($SMTPServer)).Send($MailMessage)
start-sleep -s 300
#==================================Generate CSV Script Sender==========================================
$e = get-content "C:\Scripts\dfs\part1.txt"
$f = get-content "C:\Scripts\dfs\part2.txt"
Get-ChildItem C:\Scripts\dfs\csvs | foreach-object {echo $e$_" "$f >>sendcsvs.ps1}
start-sleep -s 10
#===================================Send me the CSV====================================================
./"C:\Scripts\dfs\sendcsvs.ps1"
cd \
cd E:\count
cd MBX
./s1.ps1
#===================================End========================================================================

Open in new window

0
 
Chris DentPowerShell DeveloperCommented:

Your Overwrite bit, that just takes the content from each CSV and concatenates right?

Can't we do it this way?

I took a few other liberties as well ;)

Chris
#=======================================Variables=====================================================

$StartDate = (Get-Date).AddDays(-2) 
$EndDate = (Get-Date).AddDays(-1) 
$b = "@Domain.com"
$a = "
  <title>Daily report E-Mail Item count</title>
  <style type='text/css'>
    body   { background-color: peachpuff }
    table  { border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse }
    th, td { border-width: 1px; border-style: solid; border-color: black; background-color: palegoldenrod }
  </style>"

$Servers = "CAS01", "CAS02"

#=======================================Path==========================================================
cd C:\scripts\dfs
#
#
#======================================Get Tracking Logs=============================================

Get-Content "list.txt" | %{
  $Alias = $_
  $Servers | %{
    Get-MessageTrackinglog -ResultSize Unlimited -EventID "RECEIVE" `
      -Recipient "$Alias$b" -Server $_ -Start $StartDate -End $EndDate `
  } | Export-CSV "C:\Scripts\dfs\csvs\$Alias.csv" -NoTypeInformation
}

#
#===================create Mail Body data to FUll.csv=================================================
#

Get-ChildItem "C:\Scripts\dfs\csvs" | %{
  Echo " From: $StartDate , To: $EndDate ,there were , $((Import-CSV $_.FullName).Count), E-Mail's To , $_ , \\ShareServer\MailReport\$_ " >> C:\Scripts\dfs\FULL.csv
 }
start-sleep -s 300

#
#======================================Create HTML Mail Body==========================================
#
#

Import-Csv C:\Scripts\dfs\FULL.csv | ConvertTo-Html -head $a –body "<H2>Daily report E-Mail Item count</H2>" | Out-File C:\Scripts\dfs\MailBody.htm

#
#======================================Generate Mail report===========================================
$HTMLBody = Get-Content "C:\Scripts\dfs\MailBody.htm"
$SMTPServer = "127.0.0.1"
$From = "reporter@domain.com"
$To = "me@domain.com"
$MailMessage = New-Object System.Net.Mail.MailMessage($From, $To)
$MailMessage.Subject = "Daily report E-Mail Item count."
$MailMessage.IsBodyHtml = $True
$MailMessage.Body = $HTMLBody
(New-Object System.Net.Mail.SmtpClient($SMTPServer)).Send($MailMessage)
start-sleep -s 300
#==================================Generate CSV Script Sender==========================================
$e = get-content "C:\Scripts\dfs\part1.txt"
$f = get-content "C:\Scripts\dfs\part2.txt"
Get-ChildItem C:\Scripts\dfs\csvs | foreach-object {echo $e$_" "$f >>sendcsvs.ps1}
start-sleep -s 10
#===================================Send me the CSV====================================================
./"C:\Scripts\dfs\sendcsvs.ps1"
cd E:\count\MBX
./s1.ps1
#===================================End========================================================================

Open in new window

0
 
Elad-ItAuthor Commented:
Thanks Chris, It looks really really better than my attempt :)
just tow things,  with this line; ""  Echo  " From: $StartDate , To: $EndDate ,there were , $((Import-CSV $_.FullName).Count), E-Mail's To , $_ , \\ShareServer\MailReport\$_ " >> C:\Scripts\dfs\FULL.csv  "" .
Is there a way to add in a 0 if  ".Count"  equals 0 ?
and, "\\ShareServer\MailReport\$_" should display on the HTML report as a hyper link... I cant see why that had changed...
Thanks for all your help!!
As usual, your the best!

Jason.
0
 
Chris DentPowerShell DeveloperCommented:

Morning :)

What does the very last section do?

I've made a few more changes, but I haven't tested these.

Chris
#=======================================Variables=====================================================

$StartDate = (Get-Date).AddDays(-2)
$EndDate = (Get-Date).AddDays(-1)

$DomainName = "@Domain.com"

$HtmlHead = "
  <title>Daily report E-Mail Item count</title>
  <style type='text/css'>
    body   { background-color: peachpuff }
    table  { border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse }
    th, td { border-width: 1px; border-style: solid; border-color: black; background-color: palegoldenrod }
  </style>"

$Servers = "CAS01", "CAS02"

#
#=======================================Path==========================================================
#

cd C:\scripts\dfs

#
#======================================Get Tracking Logs=============================================
#

$HtmlBody = @()
$HtmlBody += "<h2>Daily report E-Mail Item count</h2>"

Get-Content "list.txt" | %{
  $Alias = $_

  $MailReport = $Servers | %{
    Get-MessageTrackinglog -ResultSize Unlimited -EventID "RECEIVE" `
      -Recipient "$Alias$DomainName" -Server $_ -Start $StartDate -End $EndDate
  }

  $Count = ($MailReport | Measure-Object).Count

  $HtmlBody += "From: $StartDate To: $EndDate there were $Count E-Mail's to <a href='file://///ShareServer/MailReport/$Alias.csv'>$Alias.csv</a>"

  $MailReport | Export-CSV "C:\Scripts\dfs\csvs\$Alias.csv" -NoTypeInformation
}

$MessageBody = ConvertTo-Html -Head $HtmlHead -Body $HtmlBody

#
#======================================Generate Mail report===========================================
#                      PowerShell 2 will replace this section with Send-MailMessage

$SMTPServer = "127.0.0.1"
$From = "reporter@domain.com"
$To = "me@domain.com"

$MailMessage = New-Object Net.Mail.MailMessage($From, $To)
$MailMessage.Subject = "Daily report E-Mail Item count."
$MailMessage.IsBodyHtml = $True
$MailMessage.Body = $HTMLBody
(New-Object Net.Mail.SmtpClient($SMTPServer)).Send($MailMessage)

start-sleep -s 300
#==================================Generate CSV Script Sender==========================================
$e = get-content "C:\Scripts\dfs\part1.txt"
$f = get-content "C:\Scripts\dfs\part2.txt"
Get-ChildItem C:\Scripts\dfs\csvs | foreach-object {echo $e$_" "$f >>sendcsvs.ps1}
start-sleep -s 10
#===================================Send me the CSV====================================================
./"C:\Scripts\dfs\sendcsvs.ps1"
cd E:\count\MBX
./s1.ps1
#===================================End========================================================================

Open in new window

0
 
Elad-ItAuthor Commented:
I think that the issue is that by the time you get to "$MailReport | Export-CSV "C:\Scripts\dfs\csvs\$Alias.csv" -NoTypeInformation"  the value "$MailReport" is only equals "$MailReport = $Servers"..

Export-Csv : Cannot bind argument to parameter 'InputObject' because it is null.
At C:\Scripts\dfs\dent2.ps1:41 char:27
0
 
Chris DentPowerShell DeveloperCommented:

> "$MailReport" is only equals "$MailReport = $Servers"..

It isn't. ForEach-Object (%) outputs to the pipeline so it can be used in an assignment. It only looks a little odd because it doesn't appear on the same line. However, there's no real difference between this:

$Servers | %{
  Get-MessageTrackinglog -ResultSize Unlimited -EventID "RECEIVE" `
    -Recipient "$Alias$DomainName" -Server $_ -Start $StartDate -End $EndDate
} | Export-CSV "$Alias.csv"

And this:

$MailReport = $Servers | %{
  Get-MessageTrackinglog -ResultSize Unlimited -EventID "RECEIVE" `
    -Recipient "$Alias$DomainName" -Server $_ -Start $StartDate -End $EndDate
}

But... we need to tell the script what to do if MailReport is Null.

Chris
#=======================================Variables=====================================================

$StartDate = (Get-Date).AddDays(-2)
$EndDate = (Get-Date).AddDays(-1)

$DomainName = "@Domain.com"

$HtmlHead = "
  <title>Daily report E-Mail Item count</title>
  <style type='text/css'>
    body   { background-color: peachpuff }
    table  { border-width: 1px; border-style: solid; border-color: black; border-collapse: collapse }
    th, td { border-width: 1px; border-style: solid; border-color: black; background-color: palegoldenrod }
  </style>"

$Servers = "CAS01", "CAS02"

#
#=======================================Path==========================================================
#

cd C:\scripts\dfs

#
#======================================Get Tracking Logs=============================================
#

$HtmlBody = @()
$HtmlBody += "<h2>Daily report E-Mail Item count</h2>"

Get-Content "list.txt" | %{
  $Alias = $_

  $MailReport = $Servers | %{
    Get-MessageTrackinglog -ResultSize Unlimited -EventID "RECEIVE" `
      -Recipient "$Alias$DomainName" -Server $_ -Start $StartDate -End $EndDate
  }

  $Count = ($MailReport | Measure-Object).Count

  # If the user has no mails a file will not be linked
  $FilePath = ""
  If ($MailReport -ne $Null) {
    $MailReport | Export-CSV "C:\Scripts\dfs\csvs\$Alias.csv" -NoTypeInformation
    $FilePath = "to <a href='file://///ShareServer/MailReport/$Alias.csv'>$Alias.csv</a>"
  }

  $HtmlBody += "From: $StartDate To: $EndDate there were $Count E-Mail's $FilePath"
}

$MessageBody = ConvertTo-Html -Head $HtmlHead -Body $HtmlBody

#
#======================================Generate Mail report===========================================
#                      PowerShell 2 will replace this section with Send-MailMessage

$SMTPServer = "127.0.0.1"
$From = "reporter@domain.com"
$To = "me@domain.com"

$MailMessage = New-Object Net.Mail.MailMessage($From, $To)
$MailMessage.Subject = "Daily report E-Mail Item count."
$MailMessage.IsBodyHtml = $True
$MailMessage.Body = $HTMLBody
(New-Object Net.Mail.SmtpClient($SMTPServer)).Send($MailMessage)

start-sleep -s 300
#==================================Generate CSV Script Sender==========================================
$e = get-content "C:\Scripts\dfs\part1.txt"
$f = get-content "C:\Scripts\dfs\part2.txt"
Get-ChildItem C:\Scripts\dfs\csvs | foreach-object {echo $e$_" "$f >>sendcsvs.ps1}
start-sleep -s 10
#===================================Send me the CSV====================================================
./"C:\Scripts\dfs\sendcsvs.ps1"
cd E:\count\MBX
./s1.ps1
#===================================End========================================================================

Open in new window

0
 
Elad-ItAuthor Commented:
Chris, it looks real good , great in fact... the script works like a charm, however { :-) } the mail output does not come in columns but just wrapped text on the HTML  page... and it includes the with the aliases that received 0 mails...
Should I add cmmas to : "From: $StartDate To: $EndDate there were $Count E-Mail's $FilePath"
 
For example..

From: 05/31/2010 10:25:18 To: 06/01/2010 10:25:18 there were 0 E-Mail's From: 05/31/2010 10:25:18 To: 06/01/2010 10:25:18 there were 0 E-Mail's From: 05/31/2010 10:25:18 To: 06/01/2010 10:25:18 there were 0 E-Mail's From: 05/31/2010 10:25:18 To: 06/01/2010 10:25:18 there were 1 E-Mail's to aliasa.csv From: 05/31/2010 10:25:18 To: 06/01/2010 10:25:18 there were 1 E-Mail's to Administrator.csv From: 05/31/2010 10:25:18 To: 06/01/2010 10:25:18 there were 13 E-Mail's to 2alias.csv From: 05/31/2010 10:25:18 To: 06/01/2010 10:25:18 there were 0 E-Mail's From: 05/31/2010 10:25:18 To: 06/01/2010 10:25:18 there were 0 E-Mail's From: 05/31/2010 10:25:18
0
 
Elad-ItAuthor Commented:
last thing,Chris, Its not like you work for me  :-) , I really do appreciate your extensive help with this !!! I really do.
I cant understand why the html color scheme isn't working the peachpuff is now white and the text ins't in Columns ..
0
 
Chris DentPowerShell DeveloperCommented:

Ahh that would be my fault, I took it out of the table because I couldn't much see a point for a sentence to be split across a table (it's not tabular data you see) :)

With the previous version didn't you find the first entry in the table was written as the table header?

Chris
0
 
Elad-ItAuthor Commented:
Yes it was the header - Its much more legible that way though...
0
 
Elad-ItAuthor Commented:
Sorry for not getting arround to closing this question... But guys, get your hats off for Chris Dent (!) hes a true GURU!
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.