DarkLite1 DarkLite1 - 1 year ago 37
PowerShell Question

Regex to retrieve value from table

We have users replying on an e-mail and we need to retrieve the date mentioned in the table.

For the moment I made the following code to do that for us:

$temp = ($Mail.Body.Text -creplace '(?m)^\s*\r?\n' -split "User name`r`n`tLogon name`r`n`tEnd date`r`n`tNew end date")[1]
(($temp -split "`r`n`t")[3] -split "`r`n")[0]

As you can see, it's quite long and very much relying on
. Is there an easier way to retrieve the date? With
or a
or something?

The table format in the e-mail is always the same, as the users needs to fill in the date in the HTML table. So we need to be able to read that table. In the example below the result would be

Example of such an e-mail:

Date added below

From: GBR Service Desk
Sent: Wendesday 29 juni 2016 7:00
To: Bond, James (London) GBR
Subject: REMINDER Expiring user:
Importance: High

Dear James

This is a reminder e-mail to inform you that the following Windows user account will expire soon:

User name

Logon name

End date

New end date

Smith, Jobn (Manchester) GBR




Because you are registered as the manager of this user, we would like to ask you to verify if this account is still nee
ded after its end date. More text...

Yours sincerely

Thank you for your help.

Answer Source

You could use Select-String to find date-like patterns and select the last one:

$EndDate = ($Mail.Body.Text |Select-String '\d{2}/\d{2}/\d{4}' -AllMatches).Matches[-1].Value