How can I get this Powershell script to produce multiple output?

Source Data:

AIS*00*          *00*          *11*123456CORP     *01*1234567891     *00*123456*R*13345*123456789*0*A*>~
GS*PO*9999999998*4165551010*20140918*0123*200*Y*001000~
ST*850*235454~
BEG*00*SA*12345678**20140918~
PER*BD*NAME*PQ*1234567894~
FOB*PC*OR*                  55~
DTM*002*20141009~
TD5****M~
N9*L1**MESSAGE~
MSG*MESSAGE~
MSG*MESSAGE~
N1*AB*ALPHA DATA*0*1234564567898~
N3*STREET~
N4*LOC*BA*12345~
N1*VN*PRODUCT DESC*8*1234567894565~
PO1**10*CA*9.80*FX*UA*12345645454*PI*2134567~
PID*F****DESC 1~
PO4*10*10*OZ~
PO1**18*CA*21.72*FX*UA*456123789484*PI*987654~
PID*F****DESC 2~
PO4*12*8*OZ~
PO1**9*CA*40*FX*UA*123456789456*PI*7894561~
PID*F****DESC 3~
PO4*12*16*OZ~
PO1**76*CA*21*FX*UA*123849745687*PI*2345678~
PID*F****DESC 4~
PO4*12*10*OZ~
PO1**100*CA*7.08*FX*UA*123456789787*PI*1234567~
PID*F****DESC~
CTT*5**9999*LB*50*CF~
SE*40*123456~
GE*1*130~
EFG*1*000000100~
AIS*00*          *00*          *11*123456CORP     *01*1234567891     *00*123456*R*13345*123456789*0*A*>~
GS*PO*9999999788*4165551010*20140918*0123*200*Y*001000~
ST*850*235454~
BEG*00*SA*12344444**20140918~
PER*BD*NAME*PQ*1234567894~
FOB*PC*OR*                  55~
DTM*002*20141009~
TD5****M~
N9*L1**MESSAGE~
MSG*MESSAGE~
MSG*MESSAGE~
N1*AB*ALPHA DATA*0*1234564567898~
N3*STREET~
N4*LOC*BA*12345~
N1*VN*PRODUCT DESC*8*1234567894565~
PO1**11*CA*9.80*FX*UA*12345645454*PI*2134567~
PID*F****DESC 1~
PO4*10*10*OZ~
PO1**11*CA*21.72*FX*UA*456123789484*PI*987654~
PID*F****DESC 2~
PO4*12*8*OZ~
PO1**9*CA*40*FX*UA*123456789456*PI*7894561~
PID*F****DESC 3~
PO4*12*16*OZ~
PO1**75*CA*21*FX*UA*123849745687*PI*2345678~
PID*F****DESC 4~
PO4*12*10*OZ~
PO1**100*CA*7.08*FX*UA*123456789787*PI*1234567~
PID*F****DESC~
CTT*5**9999*LB*50*CF~
SE*40*123456~
GE*1*130~
EFG*1*000000100~



Script to work with:

$InputFile = "C:\Original.txt"
$OutputFile = "C:\New.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 "(SA\*)|(PID\*)|(PO1\*{2})|(IEA\*)"}
      }
Process{
      $Data | % {
      #Check the end of data set
            If ($_ -match "EFG\*"){
                  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 "(SA\*)(\d{0,10})"){
                  $poline = "PONumber: $($Matches[2])"
            }
            #Collect PO1 details and following PID Discription
            If($_ -match "(PO1\*{2})(\d{1,})\*(\w{1,})\*(\d{1,}(\.\d{1,})?)\*{2}(\w{1,})\*(\d{1,})"){
                  $i++
                  $total += [double]$Matches[2] * [double]$Matches[4]
                  $Desc = ($Data[([Array]::IndexOf($Data,$_)+1)] -split "\*\*\*\*|~")[1]
                  [String[]]$line += "`r`nItem $i`: UPC: $($Matches[7]), Desc: $Desc, Qty: $($Matches[2]) Price  `$$($Matches[4])"
            }
      }
 }
}

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


Sample of Output desired, not reflective of actual numbers in data above.

PONumber: 1234561234
 Item 1: UPC: 12345678912345, Desc: information here, Qty: 248 Price: $10.00
 Item 2: UPC: 23456789123452, Desc: information here, Qty: 329 Price: $1.00
 Item 3: UPC: 98765432198765, Desc: information here, Qty: 20 Price $2.00
 Total: $Calculated total

PONumber: 1234561235
 Item 1: UPC: 12345678912345, Desc: information here, Qty: 250 Price: $10.00
 Item 2: UPC: 23456789123452, Desc: information here, Qty: 200 Price: $1.00
 Item 3: UPC: 98765432198765, Desc: information here, Qty: 100 Price $2.00
 Total: $Calculated total


Currently, an output file is being created however it is blank.
100questionsAsked:
Who is Participating?
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.

SubsunCommented:
What changed in input file? compared to your previous question.
0
100questionsAuthor Commented:
The input file has different data, and for some reason it does not produce the output desired at all, it`s blank..
0
SubsunCommented:
OK I could see the PO1 line format has changed a little and the in line 8 pattern was incorrect, in actual code, end of the pattern was (IEA\*) instead of (EFG\*). I have made the changes in PO1 line matching pattern (line 28) . Check and see if it works..
$InputFile = "C:\Original.txt"
$OutputFile = "C:\New.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 "(SA\*)|(PID\*)|(PO1\*{2})|(EFG\*)"}
      }
Process{
      $Data | % {
      #Check the end of data set
            If ($_ -match "EFG\*"){
		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 "(SA\*)(\d{0,10})"){
                  $poline = "PONumber: $($Matches[2])"
            }
            #Collect PO1 details and following PID Discription
            If($_ -match "(PO1\*{2})(\d{1,})\*(\w{1,})\*(\d{1,}(\.\d{1,})?).*\*(\w{1,})\*(\d{1,})"){
                  $i++
                  $total += [double]$Matches[2] * [double]$Matches[4]
                  $Desc = ($Data[([Array]::IndexOf($Data,$_)+1)] -split "\*\*\*\*|~")[1]
                  [String[]]$line += "`r`nItem $i`: UPC: $($Matches[7]), Desc: $Desc, Qty: $($Matches[2]) Price  `$$($Matches[4])"
            }
      }
 }
}

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

Open in new window

0

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
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.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.