I've struggled with this all day... I'm trying to import a csv, filter out certain stores, zero pad all store ids to 5 digits, then re-export the csv with a different delimiter and remove quotes. I've got it all working except zero padding (input3 isn't working).
#Sleep -seconds 20 #wait for RW report generation
$input = Import-Csv E:\RWS\SysFiles\reports\CAST\ClientExport.csv -Header 'store','desc','status','ip','tcpip','timezone','drive','path','col9','col10'
$input2 = $input | where-object { $_.store -match "[0-9]" -and -not $_.store.StartsWith("99") }
#$input3 = $input2 | ForEach-Object { $_.store = $_.store.PadLeft(5,'0') }
$input3 | ConvertTo-Csv -NoTypeInformation -Delimiter ';' | % {$_ -replace '"', ""} | out-file E:\RWS\SysFiles\reports\CAST\CleanClientExport.csv -force
example input:
32013,"SHREVEPORT, LA",ENABLED,10.4.43.11,(TCP/IP),-6,C:,\Program Files\Remote\,, 7045,ELIZABETHTOWN-KY,ENABLED,10.82.240.11,(TCP/IP),-5,C:,\Program Files\Remote\,,
example out:
32013;SHREVEPORT, LA;ENABLED;10.4.43.11;(TCP/IP);-6;C:;\Program Files\Remote\;; 07045;ELIZABETHTOWN-KY;ENABLED;10.82.240.11;(TCP/IP);-5;C:;\Program Files\Remote\;;
I'm just getting a blank csv...and I'm struggling with viewing $input in a PS console, can't figure out how to display what i've imported on the host screen!
Something like this should work:
$infile = "E:\RWS\SysFiles\reports\CAST\ClientExport.csv"
$outfile = "E:\RWS\SysFiles\reports\CAST\CleanClientExport.csv"
Import-Csv $infile -Header 'store','desc','status','ip','tcpip','timezone','drive','path','col9','col10' `
| ? { $_.store -notmatch '^99' } `
| % { $_.store = "{0:D5}" -f [int]$_.store; $_ } `
| ConvertTo-Csv -NoTypeInformation -Delimiter ';' `
| % { $_ -replace '"', '' } `
| Out-File $outfile -Force
If you import the CSV into a variable, you can display the imported data by entering a line cotaining just the variable:
PS C:\> $csv = Import-Csv $infile -Header 'store','desc','status','ip',...
PS C:\> $csv
store : 32013
desc : SHREVEPORT, LA
status : ENABLED
ip : 10.4.43.11
tcpip : (TCP/IP)
timezone : -6
drive : C:
path : \Program Files\Remote\
col9 :
col10 :
store : 07045
desc : ELIZABETHTOWN-KY
status : ENABLED
ip : 10.82.240.11
tcpip : (TCP/IP)
timezone : -5
drive : C:
path : \Program Files\Remote\
col9 :
col10 :