Solved

exchange 2010 powershell script

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

Free Tool: IP Lookup

Get more info about an IP address or domain name, such as organization, abuse contacts and geolocation.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
A recent project that involved parsing Tableau Desktop and Server log files to extract reusable user queries for use in other systems. I chose to use PowerShell to gather the data, and SharePoint to present it...
The video tutorial explains the basics of the Exchange server Database Availability groups. The components of this video include: 1. Automatic Failover 2. Failover Clustering 3. Active Manager
The basic steps you have just learned will be implemented in this video. The basic steps are shown to configure an Exchange DAG in a live working Exchange Server Environment and manage the same (Exchange Server 2010 Software is used in a Windows Ser…

733 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