GUI PowerShell csv to array to variable

I need to parse some data out of a string of text, and place data after a certain "field" into a variable.
It's one long contiguous string like below, data is CSV'd but it's also got "inline" headers/fields if you will. I'd like to act on 2 or 3 certain headers and put what follows into a variable.
date: 1-2-2015, server: server_01, id name: asdfjkl, other: items, go: here, stuff: mo-stuff, end time: 1-3-2015, something: qwerty

Open in new window

I want the data after "date:", "server:", "id name:", and "end time:" put into their own variables that I can use further on in the script. I want to use this GUI too, not the CLI, the data doesn't have to be echo'd to the GUI, but I do need the data after the "headers" to be placed into variables.

My code so far only output's the input data into split data. I don't know how to get the data after the colon's basically into their own variables. date:->$date, server:->$server, id name: ->$IDname, end time:->$EndTime (note that id name and end time are not quoted and contain a space). I've tried using convertfrom-csv and even converto-csv (since we are not using a file, it's pasted data) but since the data isn't well formed (no quotes) I don't think those will help, but again I know not what I'm doing, any method that gets the end result will be fine with me :)

I want to copy and paste the data line above, into the GUI form below, and have those items parsed into their respective variables to use in later in the script. This is actually my first powershell, so "just put it into an array" or "parse out the headers and ..." I won't understand... I'm stuck, I have been for a few days, I'm lost for the next step.
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$form = New-Object System.Windows.Forms.Form 
$form.Text = "CSV-input"
$form.Size = New-Object System.Drawing.Size(500,500) 
$form.StartPosition = "CenterScreen"

$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(75,380)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = "OK"
$OKButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$form.AcceptButton = $OKButton
$form.Controls.Add($OKButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(150,380)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = "Cancel"
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$form.Controls.Add($CancelButton)

$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20) 
$label.Size = New-Object System.Drawing.Size(280,20) 
$label.Text = "Please enter the CSV log data below:"
$form.Controls.Add($label) 

$textBox = New-Object System.Windows.Forms.TextBox 
$textBox.Location = New-Object System.Drawing.Point(10,40) 
$textBox.Size = New-Object System.Drawing.Size(360,320)
$textBox.Multiline = $true
$form.Controls.Add($textBox) 

$form.Topmost = $True

$form.Add_Shown({$textBox.Select()})
$result = $form.ShowDialog()

if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
    $x = $textBox.Text
    $x -split ","   
}

Open in new window

-rich
LVL 38
Rich RumbleSecurity SamuraiAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Nadav SolomonCommented:
Use import-csv filename |%{
# $_ = current line
# use $_.nameOfOneOfTheColumns to get its data from current row
$_.columnHeaderName
}
0
Rich RumbleSecurity SamuraiAuthor Commented:
You did not read the question :) It's not exactly columnar data, have a look at it's structure, and note that I'm not reading a file (so import-csv won't work), I am reading from a text-field.
-rich
0
Nadav SolomonCommented:
Sorry, I "read" it on the phone (wont tell you where :P).
Anyway how about collecting the data into custom objects array?
Here is an example on how you can create and use custom objects and to put the into array is pretty simple
$newArray = @()
$newArray += $customObject

then you'll have an array of your objects to use anywhere in the script.
0
footechCommented:
The data does look to be somewhat delimited (you could say it is twice delimited).
One approach is to replace line 45 with the following:
$date, $server, $id, $other, $go, $stuff, $endtime, $something = $x -split "," | % { (($_ -split ":")[-1]).trim()}

Open in new window


A couple other methods (substitute for line 45):
$hash = $x -split "," -replace ":","=" | Out-String | ConvertFrom-StringData
# this gives you a hashtable, where you can later reference items like
# $hash["date"]

$array = $x -split "," | ConvertFrom-Csv -Delimiter ":" -Header key,value
# this gives you an array, where you can reference an item like
# $array | Where { $_.key -eq "date" } | Select -expand Value

Open in new window

0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Rich RumbleSecurity SamuraiAuthor Commented:
Thanks for providing actual code, and not suggestions (just put it in an array and...)!
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.