I have this working script, but I think it could be done cleaner. So, to start with a little background..
I'm receiving an XML file from another company. The file contains a header, then all of the XML without any CRLF characters, then a trailer. We're uploading this file to a z/OS mainframe and it doesn't like the one incredibly long line.
I created this script to remove the header and trailer, reformat the XML and then put the header and trailer back on. It works, but at one point in the script I have to write a temporary file and then read the temporary file back in, which seems inefficient to me.
Anyone have any suggestions for getting rid of the "Temp_2_DAY2FileOUT" file in the script below:
############# CODE BEGINS #############
## Count the number of files matching the file mask provided
$x = ( Get-ChildItem "\\corp\dfs\Transfer\GAB\*DAY2FileOUT" | Measure-Object ).Count
## If the number of files from the previous command is not equal to 1 run the command with the verbose parameter to populate the log with the command results,
## then terminate with a bad exit code (indicating a failure)
If($x -NE 1){
Get-ChildItem "\\corp\dfs\Transfer\GAB\*DAY2FileOUT" -verbose
Exit 8}
## Read the file into a variable, named input, as a Powershell Object
$input = Get-Content "\\corp\dfs\Transfer\GAB\*DAY2FileOUT"
## Read the first record of the input variable into a new variable, named header, as a Powershell Object
$header = $input[0..0]
## Read all but the first and last record of the input variable into a new variable, named content, as a Powershell Object
$content = $input[1..($input.count - 2)]
## Read all the last record of the input variable into a new variable, named trailer, as a Powershell Object
$trailer = $input[($input.count - 1)..($input.count - 1)]
## Write the content variable out to a temporary file so that we have a file without the header and trailer records
$xml = [xml] $content
$xml.Save("\\corp\dfs\Transfer\GAB\Temp_2_DAY2FileOUT")
## Write the header record to the final output file
$header | Out-File "\\corp\dfs\Transfer\GAB\DAY2FileOUT.xml" -Encoding ascii
## Append the contents of the temproary file to the final output file
(Get-Content "\\corp\dfs\Transfer\GAB\Temp_2_DAY2FileOUT") | Out-File -FilePath "\\corp\dfs\Transfer\GAB\DAY2FileOUT.xml" -Encoding ascii -Append
## Append the trailer record to the final output file
$trailer | Out-File "\\corp\dfs\Transfer\GAB\DAY2FileOUT.xml" -Encoding ascii -Append
## Delete the input file and the temporary file
#Remove-Item "\\corp\dfs\Transfer\GAB\*DAY2FileOUT"
## Terminate with a success status
Exit 0
############## CODE ENDS ##############