Hoping to get some help converting strings into objects. I have a bunch of output that looks like the following:
Sample
LOGICAL UNIT NUMBER 68
Name TIER2_LUN068
Current owner: SP B
LOGICAL UNIT NUMBER 110
Name TIER1_LUN110
Current owner: SP A
LOGICAL UNIT NUMBER 203
Name TIER1_LUN203
Current owner: SP B
LOGICAL UNIT NUMBER 81
Name TIER1_LUN081
Current owner: SP A
End Sample
I want to parse through the string output and bring each of the (3) properties into a hash table.
The pseudo code of what I’m trying to accomplish is as follows:
Gather the output into a variable ($getLuns)
Loop through the output line by line, ($Line) and breaking it apart by blank spaces ($Parts)
Search each part for specific value
When found add the item to hash table (Name, value)
My function doesn’t appear to be working since the key value name is repeating. It gives the error that “Item has already been added”
Again I’m still new, so if there’s a better way to accomplish (Maybe using a switch statement instead of if) please let me know. Thank you in advance for any and all help.
So, let’s break out just creating a custom object. Although it works, using Add-Member IMHO is the most inefficient way to build an object.
Function Get-It {
begin{
#Generate a blank array to place objects into
$LHash = @()
}
process{
for($i=0;$i -le 5;$i++) {
#The main issue is understanding that each "row" is an object,
#so you are generating an object for each item processed
$object = New-Object -TypeName psobject
#Now there is a blank object with no properties, so
#to add properties you are using Add-Member.
$object|Add-Member NoteProperty 'Number' -Value $i
#Now you have to append the row to the blank array
$LHash += $object
}
}
end{
#Return your variable
$LHash
}
}
In the above example you are doing A LOT of work with mulitple variables to generate a single object. Here is the best way I’ve found (Thank you Dave) to create a object:
Function Get-It {
begin{
#Generate a blank array to place objects into
$object = @()
}
process{
#Take all results from the for loop and add them to the blank array
$object = for($i=0;$i -le 5;$i++) {
#Create variables for all your individual results (e.g. your switch regex)
$myValue = "AValue{0}" -f $i
#Generate a new object and assign the appropriate variable to the property
New-Object -TypeName PSObject -Property @{Number=($myValue)}
}
}
end{
#Return your variable
$object
}
}
Good uses for Add-Member would be adding a static (same value) to all rows at once like:
This type of list output looks fairly straightforward to parse. In your sample text, each new object begins with a LOGICAL UNIT NUMBER line, so we can use that line as the indicator to finish creating the previous object (if any), and start building a new one. The code might look something like this:
Quick Question; What does the conditional statement at the end do?
if ($LHash.Count -gt 0) {
New-Object psobject -Property $LHash
Also, I noticed that I can’t sort based on LUN_ID, any idea how to fix that. When looking at Get-Member, it looks like the noteproperty’s are not expandable (Not sure if that matters)