Click here to monitor SSC
SQLServerCentral is supported by Red Gate Software Ltd.
 
Log in  ::  Register  ::  Not logged in
 
 
 
        
Home       Members    Calendar    Who's On


Add to briefcase

Concat hard value to each row of Hash Table Expand / Collapse
Author
Message
Posted Thursday, April 17, 2014 1:46 PM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 9:11 AM
Points: 7,128, Visits: 6,291
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 Administrator

Webpage: http://www.BrandieTarvin.net
LiveJournal Blog: http://brandietarvin.livejournal.com/
On LinkedIn!, Google+, and Twitter.

Freelance Writer: Shadowrun
Latchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.


  Post Attachments 
SampleData.txt (2 views, 122 bytes)
Powershell results pic.png (2 views, 39.16 KB)
Post #1562838
Posted Thursday, April 17, 2014 3:11 PM This worked for the OP Answer marked as solution


Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: 2 days ago @ 6:45 AM
Points: 60, Visits: 880
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

Post #1562867
Posted Friday, April 18, 2014 4:32 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 9:11 AM
Points: 7,128, Visits: 6,291
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 Administrator

Webpage: http://www.BrandieTarvin.net
LiveJournal Blog: http://brandietarvin.livejournal.com/
On LinkedIn!, Google+, and Twitter.

Freelance Writer: Shadowrun
Latchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.
Post #1562960
Posted Friday, April 18, 2014 5:24 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 9:11 AM
Points: 7,128, Visits: 6,291
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 Administrator

Webpage: http://www.BrandieTarvin.net
LiveJournal Blog: http://brandietarvin.livejournal.com/
On LinkedIn!, Google+, and Twitter.

Freelance Writer: Shadowrun
Latchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.
Post #1562968
Posted Friday, April 18, 2014 11:54 AM This worked for the OP Answer marked as solution


Valued Member

Valued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued MemberValued Member

Group: General Forum Members
Last Login: 2 days ago @ 6:45 AM
Points: 60, Visits: 880
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
}

Post #1563120
Posted Monday, April 21, 2014 7:08 AM


SSCertifiable

SSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiableSSCertifiable

Group: General Forum Members
Last Login: 2 days ago @ 9:11 AM
Points: 7,128, Visits: 6,291
That works wonderfully. Thanks.

Brandie Tarvin, MCITP Database Administrator

Webpage: http://www.BrandieTarvin.net
LiveJournal Blog: http://brandietarvin.livejournal.com/
On LinkedIn!, Google+, and Twitter.

Freelance Writer: Shadowrun
Latchkeys: Nevermore, Latchkeys: The Bootleg War, and Latchkeys: Roscoes in the Night are now available on Nook and Kindle.
Post #1563469
« Prev Topic | Next Topic »

Add to briefcase

Permissions Expand / Collapse