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:
[pre]
>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
..
..
..
[/pre]
Desired new object output:
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:
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:
[pre]
>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
..
..
..
[/pre]
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++
}