【问题标题】:filter a result with a csv file使用 csv 文件过滤结果
【发布时间】:2013-07-19 07:06:41
【问题描述】:

基本上,我有一个扫描邮件跟踪日志的脚本,它每 15 分钟检查一次日志的最后一小时,在短时间内查找大量电子邮件。我知道如何使用 -gt 和 -notlike 进行过滤,但我希望脚本检查 csv 中我想要过滤的值。例如,有些人我们希望在短时间内发送大量电子邮件,我需要一个 csv 例外列表。

$ErrorActionPreference = "silentlycontinue"
add-pssnapin Microsoft.Exchange.Management.PowerShell.E2007
add-pssnapin Microsoft.Exchange.Management.PowerShell.Admin

$Now= Get-Date
$start= $Now.AddHours(-1)
#$end= $Now.AddHours(-1)

$Senders = ("bumailhub1","bumailhub2" | Get-MessageTrackingLog -resultsize unlimited -      Start "$start" -End "$Now" -EventId SEND | Select-Object ServerHostname,EventId,RecipientCount,Sender,MessageSubject,ConnectorId | where {$_.Connectorid -eq "Outbound new (Stucheck out)"})


********Right here below is where i need it to check for a csv exceptions list, which will contain a list of email addresses that are under the column "Name", and again when creating $csvmaker


$report = $senders | Group-Object Sender | Sort-Object Count -Descending | Where-Object {$_.Count -gt 50 -and $_.Name -NOTLIKE "MicrosoftExchange*"} | Select-object Count, Name
$mailing1 = $report | ConvertTo-HTML | Set-Content c:\TEMP\Date_$((get-date).tostring("MMddyyyy"))_Time_$((get-date).tostring("HHmmss"))_Users_Over_100PERLast2Hour.htm

$exceptions = Import-Csv C:\TEMP\CSVS\Exceptions\Exceptions.csv

$csvmaker = $senders | Group-Object Sender | Sort-Object Count -Descending | Where-Object {$_.Count -gt 100 -and $_.Name -NOTLIKE "MicrosoftExchange*"} | Select-object Count, Name
$csvmade =  $csvmaker | ConvertTo-CSV | Set-Content c:\TEMP\CSVS\Users_Over_100PERLast2Hour.csv

$mailing1
$mailing2 = $report | ConvertTo-Html
$subjectreport = $senders | Group-Object MessageSubject | Sort-Object Count -Descending | Where-Object {$_.Count -gt 50 -and $_.Name -NOTLIKE "MicrosoftExchange*"} | Select-object Count, Name
$subjectreportemail = $subjectreport | ConvertTo-Html

$head = Get-Content C:\TEMP\files\head.htm
$report1 = Get-Content C:\TEMP\files\report1.htm
$report2 = Get-Content C:\TEMP\files\report2.htm

If ($report | Where-Object {$_.Count -gt 100})
{
$Sender = "mailhub_issues@mydomain.com"
$SMTPClient = new-object System.Net.Mail.smtpClient
$SMTPClient.host = "mysmtp.mydomain.com"
$MailMessage = new-object System.Net.Mail.MailMessage
$Address = new-object System.Net.Mail.MailAddress("1@mydomain")
$Address2 = new-object System.Net.Mail.MailAddress("2@mydomain")
$Address3 = new-object System.Net.Mail.MailAddress("3@mydomain")
$Address4 = new-object System.Net.Mail.MailAddress("4@mydomain")
$Address5 = new-object System.Net.Mail.MailAddress("5@mydomain")
$Address6 = new-object System.Net.Mail.MailAddress("6@mydomain")
$Address7 = new-object System.Net.Mail.MailAddress("7@mydomain")
$MailMessage.Subject = "SPAM EMAIL ALERT"
$MailMessage.Body = $head + $report1 + $subjectreportemail + $report2 + $mailing2
$MailMessage.Sender = $Sender
$MailMessage.From = $Sender
$MailMessage.To.add($Address)
#$MailMessage.To.add($Address2)
#$MailMessage.To.add($Address3)
#$MailMessage.To.add($Address4)
#$MailMessage.To.add($Address5)
#$MailMessage.To.add($Address6)
$MailMessage.To.add($Address7)
$MailMessage.IsBodyHtml = $true
$SMTPClient.Send($MailMessage)



}
$ChkFile = "c:\TEMP\CSVS\Users_Over_100PERLast2Hour.csv"
$FileExists = (Test-Path $ChkFile -PathType Leaf)

If ($FileExists)
{

$Sender = "mailhub_issues@mydomain"
$SMTPClient = new-object System.Net.Mail.smtpClient
$SMTPClient.host = "mysmtp.mydomain.com"
$MailMessage = new-object System.Net.Mail.MailMessage
$Address = new-object System.Net.Mail.MailAddress("1@mydomain")
$Address2 = new-object System.Net.Mail.MailAddress("2@mydomain")
$Address3 = new-object System.Net.Mail.MailAddress("3@mydomain")
$Address4 = new-object System.Net.Mail.MailAddress("4@mydomain")
$Address5 = new-object System.Net.Mail.MailAddress("5@mydomain")
$Address6 = new-object System.Net.Mail.MailAddress("6@mydomain")
$Address7 = new-object System.Net.Mail.MailAddress("7@mydomain")
$att = new-object Net.Mail.Attachment($ChkFile)
$MailMessage.Subject = "Disabled Mailboxes"
$MailMessage.Body = "Enclosed is a CSV file containing a list of the users who have had there mailboxes disabled as a result of too many email in a short time"
$MailMessage.Attachments.Add($att)
#$MailMessage.Body = $head + $report1 + $subjectreportemail + $report2 + $mailing2
$MailMessage.Sender = $Sender
$MailMessage.From = $Sender
$MailMessage.To.add($Address)
#$MailMessage.To.add($Address2)
#$MailMessage.To.add($Address3)
#$MailMessage.To.add($Address4)
#$MailMessage.To.add($Address5)
#$MailMessage.To.add($Address6)
$MailMessage.To.add($Address7)
$MailMessage.IsBodyHtml = $true
#$Attachment = $mailing
#$MailMessage.Attachements.Add($Attachment)
$SMTPClient.Send($MailMessage)
$att.Dispose()
import-csv c:\TEMP\CSVS\Users_Over_100PERLast2Hour.csv | foreach {disable-mailbox $_.Name -confirm:$false}
Move-Item c:\TEMP\CSVS\Users_Over_100PERLast2Hour.csv c:\TEMP\CSVS\OLD\Date_$((get-date).tostring("MMddyyyy"))_Time_$((get-date).tostring("HHmmss"))_Users_Over_100PERLast2Hour.csv

  }

【问题讨论】:

    标签: powershell csv import filter


    【解决方案1】:

    只需从一个简单的文本文件中读取这些发件人:

    $whitelistedSenders = Get-Content 'C:\path\to\sender_whitelist.txt'
    

    如果您必须使用 CSV,请仅提取发件人姓名列的值:

    $whitelistedSenders = Import-Csv 'C:\path\to\sender_whitelist.csv' `
      | % { $_.'Sender Name' }
    

    然后只处理那些不在白名单中的发件人:

    $subjectreport = $senders `
      | ? { $whitlistedSenders -notcontains $_.Name } `
      | Group-Object MessageSubject `
      | Sort-Object Count -Descending `
      | ? { $_.Count -gt 50 -and $_.Name -notlike "MicrosoftExchange*" } `
      | select Count, Name
    

    【讨论】:

    • 如果将| 符号放在行尾,则不必使用行继续符。虽然您使用的方法看起来不错,但我已经被咬过很多次了,因为在行继续字符之后会出现一些额外的空格,这会导致 PowerShell 失效。
    • @KeithHill 我的编辑器配置为在保存时自动删除尾随空格。 ;) 我更喜欢以这种方式换行,因为我可以立即看到命令/管道从上一行继续的位置。
    • 我尝试了上述想法,但它们似乎都过滤掉了所有结果。我脚本中的所有变量都具有正确的值,直到它达到? { $whitlistedSenders -notcontains $_.Name } ` 然后结果变为无。这似乎过滤掉了所有结果。还有其他想法吗?不过,从 csv 或 txt 文件中提取的变量确实会从文件中获取正确的值。
    • $whitelistedSenders.GetType()($senders|%{$_.Name}).GetType() 的输出是什么?运行Compare-Object $($senders|%{$_.Name}) $whitelistedSenders -Include 会得到什么?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-03-18
    • 1970-01-01
    • 1970-01-01
    • 2022-08-12
    • 2014-10-23
    • 2011-09-20
    相关资源
    最近更新 更多