Solved

exchange 2010 powershell script

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

Why You Should Analyze Threat Actor TTPs

After years of analyzing threat actor behavior, it’s become clear that at any given time there are specific tactics, techniques, and procedures (TTPs) that are particularly prevalent. By analyzing and understanding these TTPs, you can dramatically enhance your security program.

Join & Write a Comment

Disabling the Directory Sync Service Account in Office 365 will stop directory synchronization from working.
A procedure for exporting installed hotfix details of remote computers using powershell
In this video we show how to create a Distribution Group 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 Recipients >>…
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…

705 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

Need Help in Real-Time?

Connect with top rated Experts

21 Experts available now in Live!

Get 1:1 Help Now