2015-03-31 36 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中,則執行該塊。這允許您循環一次日誌。

相關問題