?
Solved

exchange 2010 powershell script

Posted on 2011-09-07
1
Medium Priority
?
327 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 2000 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

Office 365 Training for IT Pros

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
There are times when we need to generate a report on the inbox rules, where users have set up forwarding externally in their mailbox. In this article, I will be sharing a script I wrote to generate the report in CSV format.
A short tutorial showing how to set up an email signature in Outlook on the Web (previously known as OWA). For free email signatures designs, visit https://www.mail-signatures.com/articles/signature-templates/?sts=6651 If you want to manage em…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Suggested Courses

765 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