3
\$\begingroup\$

Is there a simple way to combine the functions of this Powershell script? It would be nice to have it file to one output file instead of several output files.

    GET-DATE
WRITE-EVENTLOG -logname Application -Source BLERG -EventID 1000 -entrytype Information -message "START CBM_PARSE.ps1" -category 1
######################################################################
######################################################################
$SA_Location = "E:\Users\jsweeton\Documents\SITES\CBM\Storage Analysis\CBM-FS3"
SET-LOCATION $SA_Location
$filenames = @(GET-CHILDITEM $SA_Location -recurse -include *.csv)
$regex = '^([^-]+-[^-]+)-([A-Z])-(\d{4})(\d\d)(\d\d).+'

# $PIPE = ","
$PIPE = "$([char]0x2C)"

# $SLASH = "\"
$SLASH = "$([char]0x5C)"

# $DSLASH = "\"
$DSLASH = "$([char]0x5C)$([char]0x5C)"

foreach ($file in $filenames) {$outfile = "$file" + ".out"
$ReplaceString = ($file | Split-Path -leaf) -replace $regex,'$1|$3-$4-$5|$2:'
((GET-CONTENT $file| Out-String).Substring(7))  | FOREACH-OBJECT -Verbose {
       $_ -replace "\\","\\" `
        -replace $PIPE,"|" `
        -replace "E:",$ReplaceString `
    } | SET-CONTENT  $outfile
}
GET-DATE
WRITE-EVENTLOG -logname Application -Source BLERG -EventID 1002 -entrytype Information -message "END Parsing" -category 1
ECHO "END PARSE"
######################################################################
######################################################################

GET-DATE
WRITE-EVENTLOG -logname Application -Source BLERG -EventID 1000 -entrytype Information -message "START CBM-FS3" -category 1
GET-CONTENT (GET-CHILDITEM "E:\Users\jsweeton\Documents\SITES\CBM\Storage Analysis\CBM-FS3\*.out") | out-file -encoding "UTF8" "C:\Users\jsweeton\Documents\SITES\CBM\Storage Analysis\CBM-FS3\CBM-FS3.txt"
WRITE-EVENTLOG -logname Application -Source BLERG -EventID 1002 -entrytype Information -message "END CBM-FS3" -category 1
ECHO "END CBM-FS3"
<#
GET-DATE
WRITE-EVENTLOG -logname Application -Source BLERG -EventID 1000 -entrytype Information -message "START CBM-FS56" -category 1
GET-CONTENT (GET-CHILDITEM "C:\Users\jsweeton\Documents\SITES\CBM\Storage Analysis\CBM-FS56\*.out") | out-file -encoding "UTF8" "C:\Users\jsweeton\Documents\SITES\CBM\Storage Analysis\CBM-FS04\CBM-FS04.txt"
WRITE-EVENTLOG -logname Application -Source BLERG -EventID 1002 -entrytype Information -message "END CBM-FS56" -category 1
ECHO "END CBM-FS56"
#>
GET-DATE
WRITE-EVENTLOG -logname Application -Source BLERG -EventID 1002 -entrytype Information -message "END CBM_PARSE.ps1" -category 1
ECHO "END END"
######################################################################
######################################################################
\$\endgroup\$
  • \$\begingroup\$ Could you please describe this code? \$\endgroup\$ – Jamal Aug 4 '14 at 17:22
  • \$\begingroup\$ This script takes a CSV file, pulls information from the file name (machine name, date) and add it to columns in the csv. It then take all the output files, and rolls them up into one big txt file. From there, I import it into MySQL. \$\endgroup\$ – Jerry Sweeton Aug 4 '14 at 17:23
1
\$\begingroup\$

Assuming you commented out the second file generation of these two:

GET-CONTENT (GET-CHILDITEM "E:\Users\jsweeton\Documents\SITES\CBM\Storage Analysis\CBM-FS3\*.out") | out-file -encoding "UTF8" "C:\Users\jsweeton\Documents\SITES\CBM\Storage Analysis\CBM-FS3\CBM-FS3.txt"

GET-CONTENT (GET-CHILDITEM "C:\Users\jsweeton\Documents\SITES\CBM\Storage Analysis\CBM-FS56\*.out") | out-file -encoding "UTF8" "C:\Users\jsweeton\Documents\SITES\CBM\Storage Analysis\CBM-FS04\CBM-FS04.txt"

You can combine the above lines using the -LiteralPath parameter to Get-ChildItem, like so:

GET-CONTENT (GET-CHILDITEM -LiteralPath 'E:\Users\jsweeton\Documents\SITES\CBM\Storage Analysis\CBM-FS3\*.out', 'C:\Users\jsweeton\Documents\SITES\CBM\Storage Analysis\CBM-FS56\*.out') | out-file -encoding "UTF8" "C:\Users\jsweeton\Documents\SITES\CBM\Storage Analysis\CBM-FS3\CBM-FS3.txt"

If that's not what you're looking for or you need more input, just let me know and I'll update my answer accordingly.

|improve this answer|||||
\$\endgroup\$

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.