Solved

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

Posted on 2014-10-30
2
142 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

Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
In this video I am going to show you how to back up and restore Office 365 mailboxes using CodeTwo Backup for Office 365. Learn more about the tool used in this video here: http://www.codetwo.com/backup-for-office-365/ (http://www.codetwo.com/ba…

920 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

Need Help in Real-Time?

Connect with top rated Experts

14 Experts available now in Live!

Get 1:1 Help Now