Extract Text From Email

Welcome Forums General PowerShell Q&A Extract Text From Email

This topic contains 7 replies, has 5 voices, and was last updated by

 
Participant
4 days, 6 hours ago.

  • Author
    Posts
  • #130707

    Participant
    Points: 42
    Rank: Member

    Hi All

    I'm new to the scene of PS & have been thrown in the deep end, from my perspective, as I don't done PS at all & just beginning out with it.

    I require help. I need to write a script that will go off to get the latest email from a specific account, then output the content to a csv, or assigned to variables.

     

    The email content will have the following;

    Email Example:

    First Name: First

    Last Name: Last

    Location: Where

    Role: What

    Manager: Who

     

    Require Output:

    First,Last,Where,What,Who

     

  • #131085

    Keymaster
    Points: 1,785
    Helping HandTeam Member
    Rank: Community Hero

    I would look at the ConvertFrom-String command. It's able to take a template you create and parse text based on that template.

  • #131156

    Participant
    Points: 514
    Helping Hand
    Rank: Major Contributor

    You don't say where you need to do this from.

    Desktop Outlook – you'd have to use the Outlook DOM (Document Object Model) with PowerShell and DCOM
    Enterprise Exchange – you'd have to leverage the EWS API.

  • #131160

    Participant
    Points: 35
    Rank: Member

    Postanote is correct.  If you're talking about Exchange/Outlook, a simple command will not do the job.  Those will only get you topical information about a message (sender, recipient, message subject, etc).  This requires a little fancy footwork using Exchange EWS or Outlook Interop Assemblies that can dig deep into an email.

    Check out this article by Ed Wilson to give you an idea:  https://blogs.technet.microsoft.com/heyscriptingguy/2011/05/26/use-powershell-to-data-mine-your-outlook-inbox/

  • #133209

    Participant
    Points: 42
    Rank: Member

    Hi All

    After some digging around. I found bits & pieces that i that would resolve my issue.

     

    Please forgive my coding. I've currently got it to output to the host, just to check if it works. To which after some testing, i can say this works for me.

    #Add-Type -Assembly "Microsoft.Office.Interop.Outlook"
    #$Outlook = New-Object -ComObject 'Outlook.Application'
    #$namespace = $Outlook.GetNameSpace("MAPI")
    #$inbox = $namespace.GetDefaultFolder(
    # [Microsoft.Office.Interop.Outlook.OlDefaultFolders]::olFolderInbox)

    $outlook = new-object -comobject outlook.application
    $namespace = $outlook.GetNameSpace("MAPI")
    $folder=$namespace.GetDefaultFolder(6)
    $folder.Items |
    #Select Email with matching subject line
    ?{$_.subject -match "New User Request" } |
    sort receivedtime -desc |
    %{
    echo $_.body #do stuff with body
    # Sets what to collect from the Email
    $RE = [RegEx]'(?sm)FirstName:\s+(?.*?)$.*?LastName:\s+(?.*?)$.*?Location\s*:\s*(?.*?)$.*Role:\s+(?.*?)$.*?Manager:\s+(?.*?)$.*?'
    #Collects User From Email
    ForEach ($item in $inbox.items){

    if (($item.body -match $RE )){
    [PSCustomObject]@{
    FirstName = $Matches.FirstName
    LastName = $Matches.LastName
    Location = $Matches.Location
    Role = $Matches.Role
    Manager = $Matches.Manager
    }
    }

    }
    #mark as read
    $_.Unread=$false
    }
    Write-Output $Matches.FirstName
    Write-Output $Matches.LastName
    Write-Output $Matches.Location
    Write-Output $Matches.Role
    Write-Output $Matches.Manager

  • #133373

    Participant
    Points: 282
    Helping Hand
    Rank: Contributor

    Yes, this will work.

    And you don't need to Write-Output, since PSCustomObject will produce an output for you.

    Thank you.

    • #135461

      Participant
      Points: 42
      Rank: Member

      the write-output was for me.

  • #135453

    Participant
    Points: 42
    Rank: Member

    So my next task is to log on to a specific email but still using the code above.

    what would i need to add to get this result

You must be logged in to reply to this topic.