Here's one approach to extracting information from an e-mail address (it's version 2 of the code sample I posted yesterday):
$m = select-string -Path c:\junk\billing.txt -Pattern "BILLING ADDRESS" -SimpleMatch -CaseSensitive -Context 0,20
$GotIt = $false
$m.Context.PostContext|
ForEach-Object{
if ($_ -match "Congratulations on the sale."){
$GotIt = $true
}
}
if ($GotIt){
####################
# Get the text name (alternative 1)
# $names = $m.Context.PostContext[1] -split " " # ASSUMES! name is on this line
# Switch ($names.count){
# 2 {$Fname = $names[0]; $Lname = $names[1]; break}
# 3 {$Fname = $names[0]; $Lname = $names[2]; break}
# 4 {$Fname = $names[0]; $Lname = $names[3]; break} # maternal or paternal -- your choice -- maybe both if "Otto Von Bismark"! Good luck!
# }
####################
# Take first name from user part of email address (alternative 2)
# Take last name as the domain name (minus the top-level domain name)
$m.Context.PostContext|
ForEach-Object{
if ($_ -match "(.+@.+\..+)"){
$Eaddr = $matches[1].trim()
}
}
if ($Eaddr){
$Eaddr |
ForEach-Object{
$parts = $_ -split "@" # separate email user name from domain name
$Fname = $parts[0] # email user name may include periods (also other punctuation see RFC822)
$p2 = $parts[1] -split('\.') # split the FQDN
$Lname = $p2[(0..($p2.count - 2))] -join "." # use all but the top-level domain (e.g., edu, org, com, etc.)
}
}
[PSCustomObject]@{
Fname = $Fname
Lname = $Lname
Ename = $Eaddr
}
}