Solved

Powershell String Parsing

Posted on 2016-11-15
3
35 Views
Last Modified: 2016-11-15
I am reading in data from a 'configuration' file, made up of a key and a value. One of the values I want to read in is a string formatted as such:
server1,database1,table1|server2,database2,table2|server3,databse3,table3[|...]
I want to then parse this into a table/array that will contain:
SERVER   DATABASE   TABLE
server1   database1    table1
server2   database2    table2
server3   database3    table3
...

That I can use in a foreach loop such as
ForEach (rowData in dataRows)
{
    $server = rowData.element1
    $database = rowData.element2
    $table = rowData.element3
    ...
}

Note that the elements of each row will be comma delimited and each row will be pipe delimited. Note sure I can get titles but they aren't necessary if I can just reference the element in the rowData.
0
Comment
Question by:dbbishop
  • 2
3 Comments
 
LVL 83

Accepted Solution

by:
oBdA earned 500 total points
ID: 41888348
$dataRows = 'server1,database1,table1|server2,database2,table2|server3,databse3,table3' -split '\|' | ConvertFrom-Csv -Header Server, Database, Table

Open in new window

Or when reading from a file:
$dataRows = (Get-Content .\file.txt) -split '\|' | ConvertFrom-Csv -Header Server, Database, Table

Open in new window

And then
ForEach ($row in $dataRows)
	$row.Server
	# ...

Open in new window

0
 
LVL 83

Expert Comment

by:oBdA
ID: 41888376
Sorry, forget about the part with reading from a file, missed the part with the "key and a value". Just in case: you don't need to reinvent the wheel to read a file like that, ConvertFrom-StringData will do it.
If it's really just "key = value" pairs in the file, like this:
FirstKey = server1,database1,table1|server2,database2,table2|server3,databse3,table3
SecondKey = foo
ThirdKey = bar

Open in new window

Then you can read it like that:
$Config = Get-Content C:\Temp\config.txt | ConvertFrom-StringData
$dataRows = $Config.'FirstKey' -split '\|' | ConvertFrom-Csv -Header Server, Database, Table
ForEach ($row in $dataRows)
	$row.Server
	# ...
}

Open in new window

1
 
LVL 15

Author Closing Comment

by:dbbishop
ID: 41888425
Works like a champ and super quick response. Thanks.
0

Featured Post

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

This is a PowerShell web interface I use to manage some task as a network administrator. Clicking an action button on the left frame will display a form in the middle frame to input some data in textboxes, process this data in PowerShell and display…
Synchronize a new Active Directory domain with an existing Office 365 tenant
A short film showing how OnPage and Connectwise integration works.
Concerto provides fully managed cloud services and the expertise to provide an easy and reliable route to the cloud. Our best-in-class solutions help you address the toughest IT challenges, find new efficiencies and deliver the best application expe…

929 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

10 Experts available now in Live!

Get 1:1 Help Now