Solved

How can I use a Powershell script to output data for an Invoice

Posted on 2014-10-30
2
145 Views
Last Modified: 2014-11-03
Input Data, in a file called Source.txt

ISA*00*          *00*          *12*1234567891     *ZZ*COMPANYCOL     *213456*1234*A*00123*123000010*1*P*>~
GS*IN*2134567891*1234567891*20141024*1234*00*X*001234~
ST*999*1234561~
BIG*20141020*123456.*20140917*1234567.~
N1*RI*COMPANY*0*000000000~
N1*BT*COMPANY 2*0*12345678910000~
ITD*00*2*****00~
DTM*000*20141020~
FOB*CC~
IT1*1*200*CA*5.4**UN*123456789123*VN*01234A~
IT1*2*50*CA*1.4**UN*213456789122*VN*02345A~
IT1*3*60*CA*12.4**UN*789456123123*VN*06789A~
TDS*1234567~
ISS*1234*CA*12345*LB~
SE*12*123456~
GE*1*10~
IEA*1*123000010~


For each ISA to IEA data set in file, and there could be several, I would like the following
desired Output Data, in a file called Output.txt:

Company:  COMPANYCOL  (these are the words next found next to ZZ)
Type of document:  999  (these are the numbers next to the ST)
Identifier: 123000010  (the number found in IEA)

Then below the above output, I would like a blank line, and then I would like the data from the Source.txt to be displayed underneath it.

So the Output.txt file should look something like this, in this case:

Company:  COMPANYCOL
Type of document:  999
Identifier: 123000010


ISA*00*          *00*          *12*1234567891     *ZZ*COMPANYCOL     *213456*1234*A*00123*123000010*1*P*>~
GS*IN*2134567891*1234567891*20141024*1234*00*X*001234~
ST*999*1234561~
BIG*20141020*123456.*20140917*1234567.~
N1*RI*COMPANY*0*000000000~
N1*BT*COMPANY 2*0*12345678910000~
ITD*00*2*****00~
DTM*000*20141020~
FOB*CC~
IT1*1*200*CA*5.4**UN*123456789123*VN*01234A~
IT1*2*50*CA*1.4**UN*213456789122*VN*02345A~
IT1*3*60*CA*12.4**UN*789456123123*VN*06789A~
TDS*1234567~
ISS*1234*CA*12345*LB~
SE*12*123456~
GE*1*10~
IEA*1*123000010~
0
Comment
Question by:100questions
2 Comments
 
LVL 10

Accepted Solution

by:
JoeKlimis earned 500 total points
ID: 40413703
Good afternoon


Please try the following

 
$dataFile		= "data2.txt"
$resultsFile	= "Output.txt"
out-file $resultsFile  -encoding ascii
$data 		 	= get-content $dataFile
$result			= ""
write-output "Processing $($data.count) lines of data"
$count  = 0
#Company:  COMPANYINC  (these are the words next found next to ZZ)
# Type of document:  567  (these are the numbers next to the ST)
# Document generated for a:  888 (the number found in the AK2)
# Status:  A - (the letter found in the AK5) also on this line add - A (Accepted) R (Rejected)  
# Status of Line Items:  A - , 3 Line Items (letter found in AK9, then count the amount of 1* found in the line)
# Control Number:  123456789 (this is the number show in the IEA 
$count=0
$lines=@()
foreach ($line in $data)
{
$lines+=$line
	$SplitLine =$line.split("*")
	switch ( $splitLine[0] )
	{
	"ISA" 	{
			$company = $splitline[8]
			$doctype=""
			$Ident=""
			}

	"ST" 	{ 	$count++
				$doctype  =  $splitline[1]}
	
	"IEA" 	{ 
				$ident = $SplitLine[2].split("~")[0]
				"Company:  $company"|out-file $resultsFile -append -encoding ASCII
				"Type of document:  $doctype"| out-file $resultsFile -append -encoding ASCII
				"Identifier: $ident"| out-file $resultsFile -append -encoding ASCII
				"" | out-file $resultsFile -append -encoding ASCII
				$lines | out-file $resultsFile -append -encoding ASCII
				$lines=@()
				"" | out-file $resultsFile -append -encoding ASCII
			}
	}
}

Open in new window



I tested this against this dataset
ISA*00*          *00*          *12*1234567891     *ZZ*COMPANYCOL     *213456*1234*A*00123*123000010*1*P*>~
GS*IN*2134567891*1234567891*20141024*1234*00*X*001234~
ST*999*1234561~
BIG*20141020*123456.*20140917*1234567.~
N1*RI*COMPANY*0*000000000~
N1*BT*COMPANY 2*0*12345678910000~
ITD*00*2*****00~
DTM*000*20141020~
FOB*CC~
IT1*1*200*CA*5.4**UN*123456789123*VN*01234A~
IT1*2*50*CA*1.4**UN*213456789122*VN*02345A~
IT1*3*60*CA*12.4**UN*789456123123*VN*06789A~
TDS*1234567~
ISS*1234*CA*12345*LB~
SE*12*123456~
GE*1*10~
IEA*1*123000010~
ISA*00*          *00*          *12*1234567891     *ZZ*COMPANYCOL     *213456*1234*A*00123*123000010*1*P*>~
GS*IN*2134567891*1234567891*20141024*1234*00*X*001234~
ST*999*1234561~
BIG*20141020*123456.*20140917*1234567.~
N1*RI*COMPANY*0*000000000~
N1*BT*COMPANY 2*0*12345678910000~
ITD*00*2*****00~
DTM*000*20141020~
FOB*CC~
IT1*1*200*CA*5.4**UN*123456789123*VN*01234A~
IT1*2*50*CA*1.4**UN*213456789122*VN*02345A~
IT1*3*60*CA*12.4**UN*789456123123*VN*06789A~
TDS*1234567~
ISS*1234*CA*12345*LB~
SE*12*123456~
GE*1*10~
IEA*1*123000010~
ISA*00*          *00*          *12*1234567891     *ZZ*COMPANYCOL     *213456*1234*A*00123*123000010*1*P*>~
GS*IN*2134567891*1234567891*20141024*1234*00*X*001234~
ST*999*1234561~
BIG*20141020*123456.*20140917*1234567.~
N1*RI*COMPANY*0*000000000~
N1*BT*COMPANY 2*0*12345678910000~
ITD*00*2*****00~
DTM*000*20141020~
FOB*CC~
IT1*1*200*CA*5.4**UN*123456789123*VN*01234A~
IT1*2*50*CA*1.4**UN*213456789122*VN*02345A~
IT1*3*60*CA*12.4**UN*789456123123*VN*06789A~
TDS*1234567~
ISS*1234*CA*12345*LB~
SE*12*123456~
GE*1*10~
IEA*1*123000010~

Open in new window


and achieved these results
Company:  COMPANYCOL    
Type of document:  999
Identifier: 123000010

ISA*00*          *00*          *12*1234567891     *ZZ*COMPANYCOL     *213456*1234*A*00123*123000010*1*P*>~
GS*IN*2134567891*1234567891*20141024*1234*00*X*001234~
ST*999*1234561~
BIG*20141020*123456.*20140917*1234567.~
N1*RI*COMPANY*0*000000000~
N1*BT*COMPANY 2*0*12345678910000~
ITD*00*2*****00~
DTM*000*20141020~
FOB*CC~
IT1*1*200*CA*5.4**UN*123456789123*VN*01234A~
IT1*2*50*CA*1.4**UN*213456789122*VN*02345A~
IT1*3*60*CA*12.4**UN*789456123123*VN*06789A~
TDS*1234567~
ISS*1234*CA*12345*LB~
SE*12*123456~
GE*1*10~
IEA*1*123000010~

Company:  COMPANYCOL    
Type of document:  999
Identifier: 123000010

ISA*00*          *00*          *12*1234567891     *ZZ*COMPANYCOL     *213456*1234*A*00123*123000010*1*P*>~
GS*IN*2134567891*1234567891*20141024*1234*00*X*001234~
ST*999*1234561~
BIG*20141020*123456.*20140917*1234567.~
N1*RI*COMPANY*0*000000000~
N1*BT*COMPANY 2*0*12345678910000~
ITD*00*2*****00~
DTM*000*20141020~
FOB*CC~
IT1*1*200*CA*5.4**UN*123456789123*VN*01234A~
IT1*2*50*CA*1.4**UN*213456789122*VN*02345A~
IT1*3*60*CA*12.4**UN*789456123123*VN*06789A~
TDS*1234567~
ISS*1234*CA*12345*LB~
SE*12*123456~
GE*1*10~
IEA*1*123000010~

Company:  COMPANYCOL    
Type of document:  999
Identifier: 123000010

ISA*00*          *00*          *12*1234567891     *ZZ*COMPANYCOL     *213456*1234*A*00123*123000010*1*P*>~
GS*IN*2134567891*1234567891*20141024*1234*00*X*001234~
ST*999*1234561~
BIG*20141020*123456.*20140917*1234567.~
N1*RI*COMPANY*0*000000000~
N1*BT*COMPANY 2*0*12345678910000~
ITD*00*2*****00~
DTM*000*20141020~
FOB*CC~
IT1*1*200*CA*5.4**UN*123456789123*VN*01234A~
IT1*2*50*CA*1.4**UN*213456789122*VN*02345A~
IT1*3*60*CA*12.4**UN*789456123123*VN*06789A~
TDS*1234567~
ISS*1234*CA*12345*LB~
SE*12*123456~
GE*1*10~
IEA*1*123000010~

Let me know how you get on
Joe
0
 

Author Closing Comment

by:100questions
ID: 40420218
Works very well, excellent.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Title # Comments Views Activity
Copy with attributes 2 26
passing parameters to powershell script 2 27
remote WMI RPC server is unavailable. 4 11
ADModify LDAP Query Help 7 13
How to sign a powershell script so you can prevent tampering, and only allow users to run authorised Powershell scripts
Synchronize a new Active Directory domain with an existing Office 365 tenant
This video shows how to quickly and easily add an email signature for all users on Exchange 2016. The resulting signature is applied on a server level by Exchange Online. The email signature template has been downloaded from: www.mail-signatures…
With Secure Portal Encryption, the recipient is sent a link to their email address directing them to the email laundry delivery page. From there, the recipient will be required to enter a user name and password to enter the page. Once the recipient …

830 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question