Quantcast
Channel: Windows PowerShell forum
Viewing all articles
Browse latest Browse all 21975

need some help -- regex

$
0
0

 

 I have this script where I'm tryying to parse a file based upon KEYWORDS and then setup an email with my content
from the parsing. The data appears as below example and will always have the word END as the last line. It is having a problem with the ReadAllText peiece and the parsing from SUBJECT down to the END line. All that info needs to go into
the body. There will be multiple files in folder so it needs to reset email info and move file to archive before processing next file.

 
 Not had any luck making this work.


 THanks.




<WHO:Jim
<SUBJECT:Email Test
Then just rows and rows of Text data that may have part of the word end
...
...
...
END


Function Write-Log($LogFile,$LogText)
{
If($LogFile -match '_fax'){
$LogEntry=@"
$($LogText)"@
}else{
$LogEntry=@"
$((Get-Date).ToString("dd-MM-yyyy HH:mm:ss")) > $($LogText)"@
}
$LogEntry
$LogEntry | Add-Content $LogFile     
}

$writelogdir = 'c:\'

$def = "DONOTREPLY@tty.com"


$datapath='c:\'
$Archive='c:\Archive\'
gci $datapath*.mai | %{$testdata=gc $_.PSPath
for($i=0;$i -le $testdata.count;$i++){
  	$txt=[IO.File]::ReadAllText($testdata)
	$sender=[regex]::Match($txt,'(?<=<WHO:\s*).*').Value
	$subject=[regex]::Match($txt,'(?<=<SUBJECT:\s*).*').Value
	$body=[regex]::Match($txt,'(?s)(?<=<SUBJECT:\s*).*(?=\r\nEND)').Value
	$sendto = ($sender)+'@tty.com'
   	# added check if no Email Then Drop to ELSE 
   If($sendto){
    $from = "DONOTREPLY@tty.com"
    #Send-MailMessage -from $from -To $sendto -subject $sub -body $body -SmtpServer 'smail' -ErrorAction Continue
        if($?){
            Write-Log "$writelogdir\$($_.Name)_email.log" "Email Sent Successfully"
            Write-Log "$writelogdir\$($_.Name)_email.log" "Moved file $($file) to $rootfolder\archive\"
            }
			else
			{
            Write-Log "$writelogdir\$($_.Name)_email.log" "Email Send failed: $($error[0])";break
            }
		$sender='';$subject='';$body='';$sendto=''
    }
   Else
   {
     # added Message about No Email address Found 
     Write-Log "$writelogdir\$($_.Name)_email.log" "Email Not Sent No Address Defined in ---> $_.Name "
	 Write-Log "$writelogdir\$($_.Name)_email.log" "Moved file $($_.Name) to $rootfolder\archive\"
	 }    }
  }
  $_.MoveTo($Archive + $_.Name)


Viewing all articles
Browse latest Browse all 21975

Trending Articles