这是我尝试使用 PowerShell 实现的功能 - 使用所需数据(Subject,FromDisplayName,SubmittedDateTime,MessageTrackingReportId)将电子邮件从特定邮箱导出到CSV文件。所以我使用以下四个字符串制作它:
$msg = Search-MessageTrackingReport -Identity MyMailBox -BypassDelegateChecking
#get relevant info from that mailbox and save it to CSV file
$msg | %{ Get-MessageTrackingReport -Identity $_.MessageTrackingReportId -BypassDelegateChecking } | Select Subject, FromDisplayName, SubmittedDateTime, MessageTrackingReportId | Export-CSV C:\MyFile.csv -NoTypeInformation
#import CSV file and get rid of dublicates and select only needed columns
$temp = Import-CSV C:\MyFile.csv | sort MessageTrackingReportId -Unique | Select Subject, FromDisplayName, SubmittedDateTime
#export CSV file
$file = $temp | Export-CSV C:\MyFile.csv -NoTypeInformation
但我面临的问题是,电子邮件主题可能如下所示:
** FAN-CRITICAL Service Alert: LTC-SW-NEX01/fan-status is OK ** (24x7 OOH Alerts)
或
** SM-CRITICAL Service Alert: PRDDC1FAS001/snapmirror-lagtime is CRITICAL ** (24x7 OOH Alerts)
或
** RECOVERY Service Alert: PRDDC1ADS001/CPU Load is WARNING ** (24x7 OOH Alerts)
或
... (Something in similar format)
因此,我需要拆分此字符串,并将Subject列替换为CSV文件中的这四个新列:
AlertType为 - FAN-CRITICAL Service Alert
或SM-CRITICAL Service Alert
或RECOVERY Service Alert
或...
MachineName是 - LTC-SW-NEX01
或PRDDC1FAS001
或PRDDC1ADS001
或...
AlertName是 - fan-status
或snapmirror-lagtime
或CPU Load
或...
状态为 - OK
或CRITICAL
或WARNING
或...
因此,在一天结束时,我的CSV文件应包含以下列:
我知道我需要使用 RegEx ,但我没有任何使用过的经验,所以有人可以帮助我,我该如何结合 PowerShell 和 RegEx 来实现我的目标。
提前致谢!
答案 0 :(得分:0)
([\w ]+?):\s*([^\/]+)\/(.*?)\s\w+\s(\w+)\s\*\*
你可以尝试一下。参见demo.Grab捕获。
答案 1 :(得分:0)
开始时的事情:
$messages = @(
"** FAN-CRITICAL Service Alert: LTC-SW-NEX01/fan-status is OK ** (24x7 OOH Alerts)",
"** SM-CRITICAL Service Alert: PRDDC1FAS001/snapmirror-lagtime is CRITICAL ** (24x7 OOH Alerts)",
"** RECOVERY Service Alert: PRDDC1ADS001/CPU Load is WARNING ** (24x7 OOH Alerts)"
)
$messages | %{
$_ -match "/*/*\s*(?<AlertType>[\w ]+?):\s*(?<MachineName>[^\/]+)\/(?<AlertName>.*?)\s\w+\s(?<Status>\w+)\s\*\*" | Out-Null
$matches["AlertType"]
$matches["MachineName"]
$matches["AlertName"]
$matches["Status"]
}
有关使用powershell here的正则表达式和捕获组的更多解释。
答案 2 :(得分:0)
试试这个:
$regex = '\*\* (.+?) Service Alert: (.+?)/(.+?) is (.+?) \*\*'
if ($text -match $regex)
{
$AlertName,$MachineName,$AlertType,$Status = $Matches[1..4]
}