That's tricky... How frequently are you planning to run this scheduled job? If once a day then probably you can pick the HTML file using below line... That will find the html file created in last one hour in that directory, you can adjust this to number
of hours in past to search...
$attachment = "C:\Program Files\Microsoft\Exchange Server\V15\Scripts\" + (Get-ChildItem "C:\Program Files\Microsoft\Exchange Server\V15\Scripts\*.htm" | ?{$_.lastwritetime -gt (get-date).addhours(-1)}).name
And then add "-Attachments $attachment" to Send-MailMessage cmdlet to attach that file while sending message...
So something like this...
CollectOverMetrics.ps1 -DatabaseAvailabilityGroup DAG1 -StartTime "06/15/2012" -EndTime "06/16/2012" -GenerateHTMLReport
$attachment = "C:\Program Files\Microsoft\Exchange Server\V15\Scripts\" + (Get-ChildItem "C:\Program Files\Microsoft\Exchange Server\V15\Scripts\*.htm" | ?{$_.lastwritetime -gt (get-date).addhours(-1)}).name
send-mailmessage -to user@domain.tld -from user2@domain.tld -subject "this is a test" -smtpserver "FQDN of mail server" -Attachments $attachment
Make sure that you load Exchange snap-in while executing these via scheduled task, follow this for that...
http://www.msexchange.org/kbase/ExchangeServerTips/ExchangeServer2013/Powershell/scheduling-exchange-powershell-task.html
I plan on running it once a day. That works from powershell. However, when I put that into task scheduler, I am unsure what the -starttime and -endtime should be or if that should even be there? Also, I assume those different parts of command should be piped
together? So something like this:
CollectOverMetrics.ps1 -DatabaseAvailabilityGroup DAG1 -StartTime "01/04/2015" -EndTime "01/05/2015" -GenerateHTMLReport | $attachment = "C:\Program Files\Microsoft\Exchange Server\V15\Scripts\" + (Get-ChildItem "C:\Program Files\Microsoft\Exchange Server\V15\Scripts\*.htm" | ?{$_.lastwritetime -gt (get-date).addhours(-1)}).name |
send-mailmessage -to user@domain.tld -from user2@domain.tld -subject "DAG Failover Report" -smtpserver "FQDN of exchange server" -Attachments $attachment
The reason i assume this is because when i run it in powershell, the complete command is broken into parts. I'll try to explain:
if I copy that complete command and paste it into powershell, the first part of the command is what is run:
PS C:\Program Files\Microsoft\Exchange Server\V15\Scripts> ./CollectOverMetrics.ps1 -DatabaseAvailabilityGroup DAG1 -S
tartTime "01/04/2015" -EndTime "01/05/2015" -GenerateHTMLReport
Get statistics from exchangehost
Get statistics from exchangehost1
Found total of 2 entries
Searching for ACLL loss reports on exchangehost.
Searching for ACLL loss reports on exchangehost1.
Generated the following per-DAG reports:
C:\Program Files\Microsoft\Exchange Server\V15\Scripts\FailoverReport.DAG1.2015_01_05_20_03_55.csv
Importing data from C:\Program Files\Microsoft\Exchange Server\V15\Scripts\FailoverReport.DAG1.2015_01_05_20_03_55.csv
Generating summary report
Building summary table
Processing totals
Processing 'Mount, Automatic, Startup'
Processing 'Move, Admin, Cmdlet'
Summarising failure data
No failed operations in the data
Summary report is C:\Program Files\Microsoft\Exchange Server\V15\Scripts\FailoverSummary.2015_01_05_20_03_55.htm
PS C:\Program Files\Microsoft\Exchange Server\V15\Scripts> $attachment = "C:\Program Files\Microsoft\Exchange Server\V15
\Scripts\" + (Get-ChildItem "C:\Program Files\Microsoft\Exchange Server\V15\Scripts\*.htm" | ?{$_.lastwritetime -gt (ge
t-date).addhours(-1)}).name
PS C:\Program Files\Microsoft\Exchange Server\V15\Scripts> send-mailmessage -to user@domain.tld -from user2@domain.tld -subject "DAG Failover Report" -smtpserver "FQDN of exchange server" -Attachments $attachment
Well, it does not work when i put it into task scheduler. When running in task scheduler, the report doesnt even get generated. I tried with my added "|" and without my added "|". I clearly am not doing something right in task scheduler
because it works fine when in just powershell
-
Edited by
forgiven
Tuesday, January 06, 2015 3:14 AM