2015-03-31 29 views
0

我需要一点帮助,我的脚本。如果我只使用一个电子邮件地址,我有这个工作。我需要添加一个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= '[email protected]','[email protected]' 


#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 "<[email protected]>, <[email protected]>" 

$MailParams = @{ 
From = "[email protected]" 
To = "[email protected]" 
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 

回答

0

我把这个从你的脚本和一些从我自己的几个借来的作品放在一起。对于这种报告,您只需要通读一次日志,并且可以通过仅返回Deliver事件来消除通过messageid进行重复数据删除的需要。每发送一封邮件都会有一封邮件,包含发件人和收件人。

#Mailboxs to gather stats on 
$mailbox= '[email protected]','[email protected]' 

#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 "<[email protected]>, <[email protected]>" 

$MailParams = @{ 
From = "[email protected]" 
To = "[email protected]" 
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 
+0

这是我现在运行它的错误消息。 Send-MailMessage:无法将'System.Object []'转换为参数'Body'所需的类型'System.String'。不支持指定的 方法。 在C:\ Users \ dbeavin \ Desktop \ ISSTest.ps1:70 char:36 + Send-MailMessage @MailParams -Body $ body + ~~~~~ + CategoryInfo:InvalidArgument:(:) [Send-MailMessage ],ParameterBindingException + FullyQualifiedErrorId:CannotConvertArgument,Microsoft.PowerShell.Commands.SendMailMessage – 2015-04-01 01:05:49

+0

因为需要加载Exchange cmdlet,所以我还必须在顶部添加Add-PSSnapin Microsoft.Exchange.Management.PowerShell.E2010。我将此作为计划任务使用,因此我需要将其添加进来。 – 2015-04-01 01:06:52

+0

通过$ body的out-string添加了一个管道。这应该可以解决这个错误。您不一定必须加载捕获才能拥有Exchange cmdlet。我通常只连接到Exchange服务器并使用隐式远程处理,因此我不必在服务器上安装和更新Exchange管理工具。 – mjolinor 2015-04-01 01:29:42

0

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

  1. 算邮件
  2. 创建报告
  3. 发送报告

所以,我认为是这样的(主要是伪代码):

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 = "[email protected]","[email protected]",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。最低限度,如果您必须构建自己的报表,$ objects的集合将为您提供一个有组织的数据源,并轻松通过foreach循环。使用对象,您甚至可以扩展集合以返回消息摘要,而不仅仅是发送消息的数量。与计算消息不同,可以创建包含主题,收件人和发件人的自定义对象。 (创建对象的简单方法是使用自定义属性列表选择管道对象)。返回该集合并将其添加到结果集合中。然后报告可以包括消息摘要和总计。对象携带信息的能力给了你极大的灵活性。

mjolinor做的关键事情之一是把你的8个地址放在一个集合中。循环播放时,您可以比较if ($collection -contains $suspect) { do stuff}。如果$ suspect在$ collection中,则执行该块。这允许您循环一次日志。

相关问题