How can I change this Powershell script to output the full PO and Customer ShipTo address?

Source Data in a file called source.txt:
ISA*00*0000000000*00*0000000000*00*1234567891     *AB*ABCDEFGH       *123456*0123*A*00123*000000000*0*A*>~
GS*PO*1234567891*ABCDEFGH*20150619*0123*0*X*001234~
ST*850*123456789~
BEG*00*SA*0012-1234567-0234**20150619*A~
REF*AA*1116207*COMPANY NAME~
REF*DP*012~
FOB*CD*OR*SOMEWHERE~
CSH*N~
ITD*01*0*****00~
DTM*037*20150624~
DTM*038*20150628~
TD5*B*01*USE SOMETHING~
N1*BY*COMPANY DEST NAME              0123*00*0234~
N3*ADDRESS~
N4*CITY*ST*1234*AA~
PO1*1*5*AA*1.00*UM*AA*01234567*AB*ITEM1-A*AA*123456712345~
CTP*AB*ABC*1.05********0~
PID*A*00***DESC~
PO4*10~
PO1*2*3*AA*2.00*UM*CB*123456789*AB*ITEM2-A*AA*234567891234~
CTP*AB*RES*3.00********0~
PID*B*01***DESC 2~
PO4*6~
CTT*1*0~
SE*23*123456446~
ST*850*1123456457~
BEG*00*SA*0013-1234567-0567**20150619*A~
REF*AA*1116207*COMPANY NAME~
REF*DP*012~
FOB*CD*OR*SOMEWHERE ELSE~
CSH*N~
ITD*01*0*****00~
DTM*037*20150624~
DTM*038*20150628~
TD5*B*01*USE SOMETHING~
N1*BY*COMPANY DEST NAME              0345*00*0234~
N3*ADDRESS~
N4*CITY*ST*1234*AA~
PO1*1*5*AA*3.00*UM*AA*01234567*AB*ITEM1-B*AA*123456712678~
CTP*AB*ABC*1.06********0~
PID*A*00***DESC~
PO4*10~
PO1*2*3*AA*4.00*UM*CB*123456789*AB*ITEM2-B*AA*234567896802~
CTP*AB*RES*3.01********0~
PID*B*01***DESC 2~
PO4*6~
CTT*1*0~
SE*23*123456789~
GE*00*0~
IEA*1*000000011~
~

Open in new window

Powershell script to create output file:
$InputFile = "C:\Source\source.txt"
$OutputFile = "C:\Output\changed.txt"


Function ParseText ($OutputFile,$InputFile){
Begin{
 $line,$poline,$totline,$total,$i = $null,$null,$null,$null,0
 Set-Content $OutputFile $null
 $Data = Get-Content -Path $InputFile | ?{$_ -match "(BEG\*)|(PO1\*)|(PID\*)|(SE\*)"}
 }
 Process{
  $Data | % {
  #Check the end of data set
  If ($_ -match "SE\*"){
    If($poline -ne $null -and $total -ne $null){
     $Totline = "Total: `$$Total"
     Write-host "This is Total `$$total"
     #Write the data collection to output file
     "$poline$line`r`n$totline`r`n" | out-file $OutputFile -Encoding UTF8 -Append
     #reset the variables
     $line,$poline,$totline,$total,$i = $null,$null,$null,$null,0
     }
  }
  #Collect the PO Number
  If($_ -match "BEG\*" -and $_ -match "\*(\w{2}\*)(\d{3,10})"){
     $poline = "PO Number: $($Matches[2])"
  }
  #Collect details
  If($_ -match "PO1\*{1,}\d\*(\d{1,})\*\w{1,}\*(\d{1,}(\.\d{1,})?).*\*(\d{1,})~"){
     $count = $Matches[1]
		 $Price = $Matches[2]
		 $UPC = $Matches[4]
		 $total += [double]$count * [double]$Matches[2]
  }
  If($_ -match "PID\*"){
      $i++
      $Desc = $_ -Replace "PID\*\w\*{1,}(\d+\*{1,})?|<NL>|~"
      $line += "`r`nItem $i`: UPC: $UPC, Desc: $Desc, Qty: $count Price  `$$Price"
     }
   }
  }
 }

 If(Test-Path $InputFile){
  ParseText $OutputFile $InputFile
 }

Open in new window


Output file currently looks like this:
PO Number: 0012
Item 1: UPC: 123456712345, Desc: DESC, Qty: 5 Price  $1.00
Item 2: UPC: 234567891234, Desc: DESC 2, Qty: 3 Price  $2.00
Total: $11

PO Number: 0013
Item 1: UPC: 123456712678, Desc: DESC, Qty: 5 Price  $3.00
Item 2: UPC: 234567896802, Desc: DESC 2, Qty: 3 Price  $4.00
Total: $27

Open in new window


Desired Output file:
Company Shipto:  COMPANY DEST NAME              0123
PO Number: 0012-1234567-0234
Item 1: UPC: 123456712345, Desc: DESC, Qty: 5 Price  $1.00
Item 2: UPC: 234567891234, Desc: DESC 2, Qty: 3 Price  $2.00
Total: $11


Company Shipto: COMPANY DEST NAME              0345
PO Number: 0013-1234567-0567
Item 1: UPC: 123456712678, Desc: DESC, Qty: 5 Price  $3.00
Item 2: UPC: 234567896802, Desc: DESC 2, Qty: 3 Price  $4.00
Total: $27

Open in new window

100questionsAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Mauro CazabonnetSenior .NET Software EngineerCommented:
$InputFile = "C:\Source\source.txt"
$OutputFile = "C:\Output\changed.txt"


Function ParseText ($OutputFile,$InputFile){
Begin{
 $line,$poline,$totline,$total,$i = $null,$null,$null,$null,0
 Set-Content $OutputFile $null
 $Data = Get-Content -Path $InputFile | ?{$_ -match "(BEG\*)|(PO1\*)|(PID\*)|(SE\*)|N1"}
 }
 Process{
  $Data | % {
  #Check the end of data set
  If ($_ -match "N1"){
    $strArray = $_.split("*")
    $currString = $strArray[2] -replace '\s+', ' '
    $splitString = $currString.split(" ")
    $stringCnt = $splitString.count - 1
    $i = 0
    for ($i = 0; $i -le $stringCnt - 1; $i++)
    {
        $companyName += $splitString[$i] + " "
    }
    if ($companyName -ne $null)
    {
        $companyName = $companyName.TrimEnd()
        $companyID = $splitString[$stringCnt]
        $companyOut = "Company Shipto: " + $companyName + " " + $companyID
        $companyOut | out-file $OutputFile -Encoding UTF8 -Append
        $companyName = $null
    }
  }
  If ($_ -match "SE\*"){
    If($poline -ne $null -and $total -ne $null){
     $Totline = "Total: `$$Total"
     Write-host "This is Total `$$total"
     #Write the data collection to output file
     "$poline$line`r`n$totline`r`n" | out-file $OutputFile -Encoding UTF8 -Append
     #reset the variables
     $line,$poline,$totline,$total,$i = $null,$null,$null,$null,0
     }
  }
  #Collect the PO Number
  If($_ -match "BEG\*" -and $_ -match "\*(\w{2}\*)(\d{3,10})"){
     $poline = "PO Number: $($Matches[2])"
  }
  #Collect details
  If($_ -match "PO1\*{1,}\d\*(\d{1,})\*\w{1,}\*(\d{1,}(\.\d{1,})?).*\*(\d{1,})~"){
     $count = $Matches[1]
		 $Price = $Matches[2]
		 $UPC = $Matches[4]
		 $total += [double]$count * [double]$Matches[2]
  }
  If($_ -match "PID\*"){
      $i++
      $Desc = $_ -Replace "PID\*\w\*{1,}(\d+\*{1,})?|<NL>|~"
      $line += "`r`nItem $i`: UPC: $UPC, Desc: $Desc, Qty: $count Price  `$$Price"
     }
   }
  }
 }

 If(Test-Path $InputFile){
  ParseText $OutputFile $InputFile
 }

Open in new window

100questionsAuthor Commented:
@mcazabonnet - Thanks.  This works with the exception that the PO is still not showing in it's entirety.  
It's showing as:

PO Number: 0012

as opposed to:

0012-1234567-0234

Is there an adjustment that can be made to the script?

Thanks again.
Mauro CazabonnetSenior .NET Software EngineerCommented:
This shoudl resolve the PO line section

#Collect the PO Number
  If($_ -match "BEG\*") {  
     $strArray = $_.split("*")
     $currpo = $strArray[3]
     $poline = "PO Number: $currpo"
  }

Open in new window

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
100questionsAuthor Commented:
Thank you.  Your script, modified with the last change works well.
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.