April 17, 2014 at 1:46 pm
I'm a newbie to Powershell and this one is driving me nuts. I have a document with many many rows of data and many columns for each row. I am sending this data via HTTPS post to a webserver and need to have each line prepended and appended with a "boundary". I can't seem to create a hash table with the first column of boundary, a second column blank (to be updated with Get-Content from the file), and a third column of boundary. And when I try a FOR loop to get the data prepended, the hard coded value ends up at the end of the file.
I've done about 30 different iterations of code, getting errors and making changes along the way. The below code is the closest I've come to desired results:
$bound = '------=_12253dsfae4654'
$Hash3 = @{};
$Hash3 = (Get-Content .\SampleData.txt | Select -First 10 |
Format-Table -Property FirstName,LastName,Location)
$Hash3
$Cnt2 = (Get-Content .\SampleData.txt | Measure-Object)
$Counter = $Cnt2.Count
$Test = $bound
for ($i=1;$i -le $Counter; $i++)
{ $Test = "$($Test) $($bound) $($Hash3[$i]) $($bound)" }
$Test
Screenshot of my results is loaded and for brevity's sake, I've created a sample data file with only 3 columns and 6 rows.
Here's what I'm looking to achieve, but results do NOT have to be ordered like they are in the sample file. My only requirement is that each record have this boundary value prepended and appended to each row.
------=_12253dsfae4654Mickey,Mouse,Florida------=_12253dsfae4654
------=_12253dsfae4654Donald,Duck,Orlando------=_12253dsfae4654
------=_12253dsfae4654Doctor,Who,Tardis------=_12253dsfae4654
...etc
My Google-Fu gives me all sorts of basic help files on Hash Tables and Arrays, but nothing that helps me with concating a hard coded value to each individual line. I would appreciate any links, suggestions, etc. that you can think of.
April 17, 2014 at 3:11 pm
Would something like this work or do you need the hashtable?
$bound = '------=_12253dsfae4654'
Foreach ($line In Get-Content SampleData.txt) {
$test += "{0}{1}{2}`n" -F $bound, $line, $bound
}
$test
April 18, 2014 at 4:32 am
BRILLIANT!
That's exactly what I needed. I thought I had to load the file into Powershell before playing with it (ala a temp table).
Thank you. Now I just have to figure out my headers and the send method. @=)
April 18, 2014 at 5:24 am
Actually, it just occurs to me... Can I use WebClient.UploadFile method with this code? Do I have to create a physical file for this or can I just stream it from Powershell to the HTTPS server?
April 18, 2014 at 11:54 am
I've only used the WebClient class to download data, not upload. Maybe someone else will be able to answer. But reading the docs it looks like that method requires a file. If you wanted to try streaming it you might look into the OpenWrite method.
Since you said you're working with many rows and columns instead of loading everything into an array you can just write to a file as you go.
Here's a modification of my first answer.
$OutFile = 'FileName.txt'
Foreach ($line In Get-Content SampleData.txt) {
"{0}{1}{0}`n" -F $bound, $line | Out-File -FilePath $OutFile -Append
}
April 21, 2014 at 7:08 am
That works wonderfully. Thanks.
Viewing 6 posts - 1 through 5 (of 5 total)
You must be logged in to reply to this topic. Login to reply