Solved

exchange 2010 powershell script

Posted on 2011-09-07
1
322 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
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

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

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

A procedure for exporting installed hotfix details of remote computers using powershell
This article lists the top 5 free OST to PST Converter Tools. These tools save a lot of time for users when they want to convert OST to PST after their exchange server is no longer available or some other critical issue with exchange server or impor…
In this video we show how to create an Address List 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 Organization >> Ad…
In this video we show how to create a mailbox database 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 Servers >> Data…

840 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