1

Could you please help me!?

This is what I'm trying to achieve with PowerShell - export emails to CSV file from particular mailbox with needed data (Subject, FromDisplayName, SubmittedDateTime, MessageTrackingReportId). So I made it using following four strings:

$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

But the problem I'm facing is that Subject of email may look like this:

** FAN-CRITICAL Service Alert: LTC-SW-NEX01/fan-status is OK ** (24x7 OOH Alerts)

or

** SM-CRITICAL Service Alert: PRDDC1FAS001/snapmirror-lagtime is CRITICAL ** (24x7 OOH Alerts)

or

** RECOVERY Service Alert: PRDDC1ADS001/CPU Load is WARNING ** (24x7 OOH Alerts)

or

... (Something in similar format)

So I need to split this string and replace Subject column with those four new columns in my CSV file:

  1. AlertType which is - FAN-CRITICAL Service Alert or SM-CRITICAL Service Alert or RECOVERY Service Alert or ...

  2. MachineName which is - LTC-SW-NEX01 or PRDDC1FAS001 or PRDDC1ADS001 or ...

  3. AlertName which is - fan-status or snapmirror-lagtime or CPU Load or ...

  4. Status which is - OK or CRITICAL or WARNING or ...

So in the end of the day my CSV file should have these following columns:

  • AlertType
  • MachineName
  • AlertName
  • Status
  • FromDisplayName
  • SubmittedDateTime
  • MessageTrackingReportId

I know that I need to use RegEx but I don't have any experience working with it, so could anyone please help me, how can I combine PowerShell and RegEx to achieve my goals.

Thanks in advance!

3
  • This is not a viable long-term solution. While this will work for now, you really ought to have your monitoring system recording to individual fields in a database so that this data is more easily stored, retrieved & reported on. Commented Dec 9, 2014 at 12:15
  • I know, I import CSV file to DB afterwards! Commented Dec 9, 2014 at 13:08
  • No, I'm saying eliminate the email processing altogether and record directly to a database. Commented Dec 9, 2014 at 14:21

3 Answers 3

1

Something to start with:

$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"]
}

More explanations on regex and capture groups with powershell here.

Sign up to request clarification or add additional context in comments.

Comments

0
([\w ]+?):\s*([^\/]+)\/(.*?)\s\w+\s(\w+)\s\*\*

You can try this.See demo.Grab the capture.

https://regex101.com/r/iY3eK8/9

2 Comments

Nice one, thanx but how to combine it with PowerShell in my case?
@user4340729 havent used Powershell :(
0

Try this:

$regex = '\*\* (.+?) Service Alert: (.+?)/(.+?) is (.+?) \*\*'

if ($text -match $regex)
  {
   $AlertName,$MachineName,$AlertType,$Status = $Matches[1..4]
  }

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.