Solved

exchange 2010 powershell script

Posted on 2011-09-07
1
324 Views
Last Modified: 2012-05-12
I am trying to build a small reporting script for emails. i have the attached code, and it runs but nothing is being dropped into the html file.

If the file isnt created, it creates it. and if i just type somewhere in the code $subject or another field i have defined, they are displayed in the output, but still nothing is put into the html file.

What do i have wrong?
$Session = New-PSSession -ConfigurationName Microsoft.Exchange -ConnectionUri http://server.domain.local/PowerShell/ -Authentication Kerberos
Remove-Item C:\exchangescripts\email_logs.html
New-Item -ItemType file -Path C:\exchangescripts -Name email_logs.html
$mailboxCountThreshold =  30
$mailboxSizeThreshold = 2000
$mailboxSizeThreshold1 = 1000 
$backupThreshold = 1 
$mbxSizeThreshold = 30MB
$fileName = "email_logs.html"
Function writeHtmlHeader
{
    param($fileName)
    $date = ( Get-Date ).ToString('dd/mm/yyyy') 
    Add-Content $fileName "<html>"
    Add-Content $fileName "<head>"
    Add-Content $fileName "<meta http-equiv='Content-Type' content='text/html; charset=iso-8859-1'>"
    Add-Content $fileName '<title>domain.local Email Tracker $date</title>'
    Add-Content $fileName '<STYLE TYPE="text/css">'
    Add-Content $fileName  "<!--"
    Add-Content $fileName  "td {"
    Add-Content $fileName  "font-family: Tahoma;"
    Add-Content $fileName  "font-size: 11px;"
    Add-Content $fileName  "border-top: 1px solid #999999;"
    Add-Content $fileName  "border-right: 1px solid #999999;"
    Add-Content $fileName  "border-bottom: 1px solid #999999;"
    Add-Content $fileName  "border-left: 1px solid #999999;"
    Add-Content $fileName  "padding-top: 0px;"
    Add-Content $fileName  "padding-right: 0px;"
    Add-Content $fileName  "padding-bottom: 0px;"
    Add-Content $fileName  "padding-left: 0px;"
    Add-Content $fileName  "}"
    Add-Content $fileName  "body {"
    Add-Content $fileName  "margin-left: 5px;"
    Add-Content $fileName  "margin-top: 5px;"
    Add-Content $fileName  "margin-right: 0px;"
    Add-Content $fileName  "margin-bottom: 10px;"
    Add-Content $fileName  "" 
    Add-Content $fileName  "table {"
    Add-Content $fileName  "border: thin solid #000000;"
    Add-Content $fileName  "}"
    Add-Content $fileName  "-->"
    Add-Content $fileName  "</style>"
    Add-Content $fileName "</head>"
    Add-Content $fileName "<body>"
    Add-Content $fileName  "<table width='100%'>"
    Add-Content $fileName  "<tr bgcolor='#CCCCCC'>"
    Add-Content $fileName  "<td colspan='7' height='25' align='center'>"
    Add-Content $fileName  "<font face='tahoma' color='#003399' size='4'><strong>domain.local Email Tracker - $date</strong></font>"
    Add-Content $fileName  "</td>" 
    Add-Content $fileName  "</tr>" 
    Add-Content $fileName  "</table>" 
}

Function writeTableHeader 
{ 
    param($fileName) 
    Add-Content $fileName "<table width='100%'><tbody>"  
    Add-Content $fileName "<tr bgcolor=#CCCCCC>" 
    Add-Content $fileName "<td width='10%' align='center'><B>Time Stamp</B></td>" 
    Add-Content $fileName "<td width='10%' align='center'><b>Event ID</b></td>" 
    Add-Content $fileName "<td width='10%' align='center'><b>Recipients</b></td>" 
    Add-Content $fileName "<td width='10%' align='center'><b>Message Subject</b></td>"
    Add-Content $fileName "<td width='10%' align='center'><b>Sender</b></td>"
    Add-Content $fileName "</tr>"
}
Function WriteInfo
{
    $logs = Invoke-Command -Session $session -ScriptBlock {get-messagetrackinglog -server 'server name' -sender: 'dan.james@domain.co.uk' -Start '09/06/2011 00:00:00' -end '09/07/2011 00:00:00'}
    foreach($email in $logs) 
    { 
        $Timestamp = $email.timestamp
        $eventid = $email.EventID
        $Recipients = $email.recipients
        $subject = $email.messagesubject
        $Sender = $email.Sender
        Add-Content $fileName "<table width='100%'><tbody>"  
        Add-Content $fileName "<tr bgcolor=#CCCCCC>"  
        Add-Content $fileName ("<td width='10%' align='center'>" + $Timestamp + "</td>")
        Add-Content $fileName ("<td width='10%' align='center'>" + $eventid + "</td>") 
        Add-Content $fileName ("<td width='10%' align='center'>" + $Recipients + "</td>")
        Add-Content $fileName ("<td width='10%' align='center'>" + $subject + "</td>")
        Add-Content $fileName ("<td width='10%' align='center'>" + $Sender + "</td>")
        Add-Content $fileName "</tr>" 
    }
} 
writeHtmlHeader $fileName
writeTableHeader $fileName
WriteInfo $fileName
Remove-PSSession $Session

Open in new window

0
Comment
Question by:CaptainGiblets
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
1 Comment
 
LVL 49

Accepted Solution

by:
Akhater earned 500 total points
ID: 36494512
Change the $filename from "email_logs.html" to "c:\exchangescripts\email_logs.html"

In the currentconfig another html file is being generated in the directory where you are running the script
0

Featured Post

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

In-place Upgrading Dirsync to Azure AD Connect
Previously, on our Nano Server Deployment series, we've created a new nano server image and deployed it on a physical server in part 2. Now we will go through configuration.
To show how to create a transport rule in Exchange 2013. We show this process by using the Exchange Admin Center. Log into Exchange Admin Center.: First we need to log into the Exchange Admin Center. Navigate to the Mail Flow >> Rules tab.:  To cr…
how to add IIS SMTP to handle application/Scanner relays into office 365.

695 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question