?
Solved

Powershell String Parsing

Posted on 2016-11-15
3
Medium Priority
?
83 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 2
3 Comments
 
LVL 85

Accepted Solution

by:
oBdA earned 2000 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 85

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

Free Tool: SSL Checker

Scans your site and returns information about your SSL implementation and certificate. Helpful for debugging and validating your SSL configuration.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

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

A quick Powershell script I wrote to find old program installations and check versions of a specific file across the network.
In the absence of a fully-fledged GPO Management product like AGPM, the script in this article will provide you with a simple way to watch the domain (or a select OU) for GPOs changes and automatically take backups when policies are added, removed o…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…
Michael from AdRem Software explains how to view the most utilized and worst performing nodes in your network, by accessing the Top Charts view in NetCrunch network monitor (https://www.adremsoft.com/). Top Charts is a view in which you can set seve…

752 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