SQL Clone
SQLServerCentral is supported by Redgate
 
Log in  ::  Register  ::  Not logged in
 
 
 


Concat hard value to each row of Hash Table


Concat hard value to each row of Hash Table

Author
Message
Brandie Tarvin
Brandie Tarvin
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14565 Visits: 8991
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.

Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.
Attachments
SampleData.txt (7 views, 122 bytes)
Powershell results pic.png (9 views, 39.00 KB)
bruce 1565
bruce 1565
SSC-Enthusiastic
SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)

Group: General Forum Members
Points: 189 Visits: 1022
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


Brandie Tarvin
Brandie Tarvin
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14565 Visits: 8991
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. @=)

Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.
Brandie Tarvin
Brandie Tarvin
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14565 Visits: 8991
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?

Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.
bruce 1565
bruce 1565
SSC-Enthusiastic
SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)SSC-Enthusiastic (189 reputation)

Group: General Forum Members
Points: 189 Visits: 1022
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
}


Brandie Tarvin
Brandie Tarvin
SSChampion
SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)SSChampion (14K reputation)

Group: General Forum Members
Points: 14565 Visits: 8991
That works wonderfully. Thanks.

Brandie Tarvin, MCITP Database AdministratorLiveJournal Blog: http://brandietarvin.livejournal.com/On LinkedIn!, Google+, and Twitter.Freelance Writer: ShadowrunLatchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.
Go


Permissions

You can't post new topics.
You can't post topic replies.
You can't post new polls.
You can't post replies to polls.
You can't edit your own topics.
You can't delete your own topics.
You can't edit other topics.
You can't delete other topics.
You can't edit your own posts.
You can't edit other posts.
You can't delete your own posts.
You can't delete other posts.
You can't post events.
You can't edit your own events.
You can't edit other events.
You can't delete your own events.
You can't delete other events.
You can't send private messages.
You can't send emails.
You can read topics.
You can't vote in polls.
You can't upload attachments.
You can download attachments.
You can't post HTML code.
You can't edit HTML code.
You can't post IFCode.
You can't post JavaScript.
You can post emoticons.
You can't post or upload images.

Select a forum

































































































































































SQLServerCentral


Search