对于这件事,我会收到大量自动警报(每天500-600个),我正在尝试编写一个powershell脚本来为我分析它们。
它现在可以做的是
在我的前景中解析一个子文件夹
使用正则表达式,可自动获取电子邮件警报中的特定变量
每个循环都将每个匹配项转储到csv的新行中
我正在计算每个用户的出现次数。因此,如果出现“ jdoe”,它将添加到数组中。如果“ Jdoe”在2000左右的电子邮件中显示了4次以上,那么最后它会说类似
Jdoe 5
Jbond 2
客人100
我在想可能是二维数组?但是我不能把头缠住它。
我是一个非常虚弱的编码器,下面我不得不评论一下,以便我可以更好地了解每一部分在做什么
Add-Type -Assembly "Microsoft.Office.Interop.Outlook"
$Outlook = New-Object -ComObject Outlook.Application
$namespace = $Outlook.GetNameSpace("MAPI")
$DebugPreference = 'Continue'
#------------
# Selects my Inbox, and then selects sub-folder under inbox. For my outlook, I have a subfolder called
# "Account Locked Alerts" that I have an outlook rule put all of the account locked out alerts in
#------------
$inbox = $namespace.GetDefaultFolder([Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox)
$subfolder = $inbox.Folders('Account Locked Alerts')
#------------
#I'm using RegEx here because selecting the values is super dooper easy. Also, there's technically two "account name" value in these emails
# and I'm interested in the second.
#------------
$RE = [RegEx]'(?sm)Account Name\s*:\s*(?<AccName>.*?)$.*Account Name\s*:\s*(?<Acc2Name>.*?)$.*'
$users = @()
#------------
# For every email in my sub folder...
# (p.s., to check every email in "inbox", just replace $subfolder with $inbox)
#------------
$Data = ForEach ($item in $subfolder.items){
#------------
# Check to see if there's a match, and if there is....
#------------
if ($item.body -match $RE){
#------------
# Add to array and write it to csv
#------------
Write-Host "ding " $Matches.Acc2Name
$users += $Matches.Acc2name
[PSCustomObject]@{
AccName = $Matches.Acc2Name
}
}
}
$Data
$Data | Export-CSv '.\data.csv' -NoTypeInformation
编辑:该问题已被标记为另一个。我不这么认为,因为链接的可能的dup已经知道要期待什么变量,并且只对它们进行计数。我有9000个未知的变量,每周都有成百上千的用户来往。这是动态添加和THEN计数,尽管“可能重复”回答了我一半的问题。
答案 0 :(得分:1)
您的脚本有一些小问题。不需要$users
变量,您可以使用group-by
获得想要的结果。
仅需稍微更改一下脚本即可。
#------------
#I'm using RegEx here because selecting the values is super dooper easy. Also, there's technically two "account name" value in these emails
# and I'm interested in the second.
#------------
$RE = [RegEx]'(?sm)Account Name\s*:\s*(?<AccName>.*?)$.*Account Name\s*:\s*(?<Acc2Name>.*?)$.*'
#------------
# For every email in my sub folder...
# (p.s., to check every email in "inbox", just replace $subfolder with $inbox)
#------------
$Data = ForEach ($item in $subfolder.items){
#------------
# Check to see if there's a match, and if there is....
#------------
if ($item.body -match $RE){
#------------
# Add to array and write it to csv
#------------
Write-Verbose "ding " $Matches.Acc2Name
[PSCustomObject]@{
Acc2Name = $Matches.Acc2Name
}
}
}
$Data | Group-Object -Property Acc2Name -NoElement
$Data | Export-CSv '.\data.csv' -NoTypeInformation