Using powershell, how can I extract a 7 digit number from a subject line (email), regular expressions?

I have the following code listing the first 5 items in my inbox (Outlook).

How can I extract only part of the number (say - 7 digits of arbitrary numbers that are embedded in other text)? Then, using Powershell commands, I would really like to take these extracted numbers and upload them to a CSV file (that way, they can easily be included in an existing table that I use).

Here is what I tried:

$outlook = new-object -com Outlook.Application
$sentMail = $outlook.Session.GetDefaultFolder(6) # == olFolderInbox
$sentMail.Items | select -last 10 TaskSubject    # ideally, grabbing first 20

 $matches2 =  "\d+$"
 $res = gc $sentMail.Items | ?{$_ -match $matches2 | %{ $_ -match $matches2 | out-null; $matches[1] }

but this does not work correctly, but rather ... keeps me hanging with a pending input character: like this:

>>
>>
>>

Do I need to create a separate variable between the 1st part and the 2nd part?

+4
2

, $matches, - .

:

$sentMail.Items | % { $_.TaskSubject | Select-String -Pattern '^\d{3}-\d{3}-\d{4}' | % {([string]$_).Substring(0,12)} }

:

$sentMail.Items | % { ($_.Body).Split("`n") | Select-String -Pattern '^\d{3}-\d{3}-\d{4}' |% {([string]$_).Substring(0,12)}  }

Select-String, . https://technet.microsoft.com/library/hh849903.aspx

, , . http://blogs.technet.com/b/heyscriptingguy/archive/2011/03/24/use-powershell-to-search-a-group-of-files-for-phone-numbers.aspx

!


7- . , , . , Select * 100 .

$outlook = New-Object -com Outlook.Application
$Mail = $outlook.Session.GetDefaultFolder(6) # Folder Inbox

$Mail.Items | select -First 100 TaskSubject | 
% { $_.TaskSubject | Select-String -Pattern '\s\d{7}\s'} |
% {((Select-String -InputObject $_ -Pattern '\s\d{7}\s').Line).split(" ") | 
% {if(($_.Length -eq 7) -and ($_ -match '\d{7}')) {$_ | Out-File -FilePath "C:\Temp\SomeFile.csv" -Append}}} 
+3

/, .

, Select-String -AllMatches. . , .

$sentMail.Items | select -last 10 TaskSubject 

10 , , . .

-match , , if blocks where. , . , :

$res = gc $sentMail.Items | ?{$_ -match $matches2 | %{ $_ -match $matches2 | out-null; $matches[1] }

: Get-Content (gc) . Get-Content , $sentMail.Items . . . ?{$_ -match $matches2 | %{ $_ -match $matches2 | out-null; $matches[1] } ... , .

$outlook = new-object -com Outlook.Application
$sentMail = $outlook.Session.GetDefaultFolder(6) # == olFolderInbox
$matches2 =  "\d+$"
$sentMail.Items | select -last 10 -ExpandProperty TaskSubject | ?{$_ -match $matches2} | %{$Matches[0]}

10 , $matches2. , .

+1

All Articles