用于计算多个用户的电子邮件数量的脚本

时间:2015-03-31 21:20:45

标签: powershell exchange-server

我的脚本需要一些帮助。如果我只使用一个电子邮件地址,我就可以使用它。我需要添加一个包含8个电子邮件地址的列表来进行扫描。如何修改此项以便为所有8位用户发送1封电子邮件?

我已经看过制作一个html文件的脚本,它在一个漂亮的表中显示所有内容,但这些脚本是针对所有用户进行交换的,而我只需要一组8个用户。

  Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010

#Powershell blah blah blah
$nl = [Environment]::NewLine

#Mailbox to gather stats on
$mailboxs=$mailbox= 'user1@domain.com','user2@domain.com'


#Get todays
$startDate=Get-Date
$endDate=Get-Date

#Subtract 1 day from todays date (report ending day) and 1 day from todays date (report starting day)
$startDateFormatted=$startDate.AddDays(-1).ToShortDateString()
$endDateFormatted=$endDate.AddDays(-1).ToShortDateString()

foreach ($mailbox in $mailboxs)
{
# Sent e-mails
$sendCount = Get-TransportService | Get-MessageTrackingLog -Start "$startDateFormatted 00:00:00" -End "$endDateFormatted 23:59:59" -Sender $mailbox -resultsize unlimited | select-object -unique MessageId

# Received e-mails - This works but not on generic accounts
$receiveCount = Get-TransportService | Get-MessageTrackingLog -Start "$startDateFormatted 00:00:00" -End "$endDateFormatted 23:59:59" -Recipients $mailbox -resultsize unlimited | select-object -unique MessageId

$sendCountString = $sendCount.count
$receiveCountString = $receiveCount.count
}

$Output = 
$Mailbox | 
foreach {
$ResultHash = 
 @{
    Address = $_
    Sent    = $Sendcountstring
    Received = $Receivecountstring
   }

 New-Object -TypeName PSObject -Property $ResultHash |
  Select Address,Sent,Received
  }

#Who to send the e-mail report to.
#Multiple e-mail addresses should be in this format "<email1@domain.com>, <email2@domain.com>"

$MailParams = @{
From = "ISSReports@domain.com"
To = "user3@domain.com"
subject = "Daily e-mail report for ISS for $startDateFormatted"
BodyAsHTML = $true
smtpServer = "mail.domain.com"
}

$header = 
@"
"Mailbox Stats
Report date range: $startDateFormatted 00:00:00 - $endDateFormatted 23:59:59 
"@

$body = $Output | ConvertTo-Html -As Table -Head $header | out-string

Send-MailMessage @MailParams -Body $body

2 个答案:

答案 0 :(得分:0)

我从你的剧本中把它放在一起,并从我自己的几个借来的一些作品。对于这种报告,您只需要阅读一次日志,并且只需返回Deliver事件即可消除messageid重复数据删除的需要。每封电子邮件将发送一个传递事件,包含发件人和收件人。

#Mailboxs to gather stats on
$mailbox= 'user1@domain.com','user2@domain.com'

#Get todays date twice
$startDate=Get-Date
$endDate=Get-Date

#Hash tables for send and receive counts
$Sent = @{}
$Received = @{}

#Subtract 1 day from todays date (report ending day) and 1 days from todays date (report starting day)
$startDateFormatted=$startDate.AddDays(-1).ToShortDateString()
$endDateFormatted=$endDate.AddDays(-1).ToShortDateString()

$TransportServers = Get-ExchangeServer  |
 Where {$_.serverrole -match "hubtransport"} |
 Select -ExpandProperty Name

foreach ($TransportServer in $TransportServers)
{
  Get-MessageTrackingLog -Start "$startDateFormatted 00:00:00" -End "$endDateFormatted 23:59:59" -EventID Deliver -Server $TransportServer -ResultSize Unlimited |
   foreach {
    if ($mailbox -contains $_.sender )
      { $Sent[$_.Sender]++ }

    foreach ($Recipient in $_.Recipients)
     {
      if ($mailbox -contains $Recipient )
       { $Received[$Recipient]++ }
     }
   }
 }

$Output = 
$Mailbox | 
foreach {
 $ResultHash = 
 @{
    Address = $_
    Sent    = $Sent[$_]
    Received = $Received[$_]
   }

 New-Object -TypeName PSObject -Property $ResultHash |
  Select Address,Sent,Received
}

#Who to send the e-mail report to.
#Multiple e-mail addresses should be in this format "<email1@domain.com>, <email2@domain.com>"

$MailParams = @{
From = "ISSReports@domain.com"
To = "user3@domain.com"
subject = "Weekly e-mail report for $mailbox for $startDateFormatted - $endDateFormatted"
BodyAsHTML = $true
smtpServer = "mail.domain.com"
}

$header = 
@"
"Mailbox Stats
Report date range: $startDateFormatted 00:00:00 - $endDateFormatted 23:59:59 
"@

$body = $Output | ConvertTo-Html -As Table -Head $header | out-string

Send-MailMessage @MailParams -Body $body

答案 1 :(得分:0)

对于这8个邮箱,您需要计算该邮箱发送的邮件数吗?这需要3个高级功能:

  1. 统计邮件
  2. 创建报告
  3. 发送报告
  4. 所以,我认为这样(主要是伪代码):

    Function Count-SentFromMbx { 
    
    #paramter $mailbox
        #parameter $startdate
        #parameter $enddate
        # function counts sent mail per your working single user script
        #    or better yet, per mjolinor's script leveraging `-contains`
        # build custom object with mailbox and count as properties
        #    or better yet, enough information to build a digest.
        # return object/collection
    } 
    
    # Create a function to format the results email
    
    # Create a function to send the results email
    
    # Define collection of mailboxes
    $mailboxes = "blah@blah.com","blah2@blah.com",etc.
    
    # define or get the dates
    $startdate = whatever
    $enddate = whatever
    
    # initialize array for results
    $results = @{}
    
    # set other variables for storage of the report
    # and for recipient list
    # and anything else needed 'globally'
    
    # Pull data from the logs: 
        $results += Count-SentFromMbx -mailbox $mailbox -startdate $startdate -enddate $enddate
    
    # if you end up with a collection of collections, you may need to change to 
    #    $results = ... and add a line like `$results | $reportData += $_`
    
    # build report using report function
    
    # send report using send function
    

    我建议的主要思想是对流程中涉及的每个模块化任务使用高级函数(Get-Help about_advanced_functions)。然后,您可以利用对象的强大功能来轻松收集数据。您可以通过调整报告功能为您的受众定制报告。

    让你的函数返回一个对象,试试$results | ConvertTo-Html。最低限度,如果您必须构建自己的报告,对象集合$ results会为您提供一个有组织的数据源,可以轻松地通过foreach循环。使用对象甚至可以扩展集合以返回消息摘要,而不仅仅是发送的消息数。而不是计算消息,创建一个主题,收件人和发件人的自定义对象。 (创建对象的简便方法是使用自定义属性列表来管道对象以进行选择)。返回该集合并将其添加到结果集合中。然后报告可以包括消息摘要和总计。物体携带信息的能力为您提供了极大的灵活性。

    mjolinor所做的一件重要事情就是将你的8个地址放在一个集合中。循环时,您可以比较if ($collection -contains $suspect) { do stuff}。如果$ suspect在$ collection中,则执行该块。这允许您循环遍历日志。