×
INTELLIGENT WORK FORUMS
FOR COMPUTER PROFESSIONALS

Log In

Come Join Us!

Are you a
Computer / IT professional?
Join Tek-Tips Forums!
  • Talk With Other Members
  • Be Notified Of Responses
    To Your Posts
  • Keyword Search
  • One-Click Access To Your
    Favorite Forums
  • Automated Signatures
    On Your Posts
  • Best Of All, It's Free!

*Tek-Tips's functionality depends on members receiving e-mail. By joining you are opting in to receive e-mail.

Posting Guidelines

Promoting, selling, recruiting, coursework and thesis posting is forbidden.

Students Click Here

Help converting list of strings to Object

Help converting list of strings to Object

Help converting list of strings to Object

(OP)
Hello.

I'm hoping somebody could help me figure out the following.

I have data that I've pulled down from a website using invoke-webrequest. I've got the data cleaned up and now is in an system.string type object. I want to create a hash table and possibly convert to a pscustomobject, but I'm having a hard time figuring out the best approach.

The first 11 lines define the property names, and then the rest of the data are records:

>write-output $data
Hostname
PAST
GSS
Server Status
Barcode
Serial Number
Premium
OS Name
OS Version
CCG
Project

ServerA
Support Team 1A
GSS-50
Active
L50000383
0539AK
N
SOLARIS
SunOS 5.10

ATIOP

SERVERB
Support Team 3C
GSS-40
Retired
L3838282
0538AK
Y
SOLARIS
SunOS 5.11

SWEPA

SERVERC
..
..
..
 

Desired new object output:

CODE

Hostname      : SERVERA
PAST          : Support Team 1A
GSS           : GSS-50
Server Status : Active
Barcode       : L50000383
Serial Number : 0539AK
Premium       : N
OS Name       : SOLARIS
OS Version    : SunOS 5.10
CCG           :
Project       : ATIOP 

This is what I have come up so far but seems like there would be a better way than having to use so many IF statements:

CODE

$hash=@{}
for ($i=0; $i -lt $data.count; $i++) {
if ($i % 12 -eq 0 -and $i -ne 0) { $n=0 } #Figures out where the new record starts
if ( $n -eq 0 -and $i -gt 0) { 
$hash.Add("server",$data[$i])
}
if ( $n -eq 3 -and $i -gt 5 ) {
$hash.Add("Retention",$data[$i])
}
if ( $n -eq 10 -and $i -gt 13) {
$hash.Add("Project",$data[$i])
[PSCustomObject]$hash | Select -Property Server,Project,Retention | Format-List
$hash.Clear()
}
$n++
} 

Red Flag This Post

Please let us know here why this post is inappropriate. Reasons such as off-topic, duplicates, flames, illegal, vulgar, or students posting their homework.

Red Flag Submitted

Thank you for helping keep Tek-Tips Forums free from inappropriate posts.
The Tek-Tips staff will check this out and take appropriate action.

Reply To This Thread

Posting in the Tek-Tips forums is a member-only feature.

Click Here to join Tek-Tips and talk with other members! Already a Member? Login

Close Box

Join Tek-Tips® Today!

Join your peers on the Internet's largest technical computer professional community.
It's easy to join and it's free.

Here's Why Members Love Tek-Tips Forums:

Register now while it's still free!

Already a member? Close this window and log in.

Join Us             Close