turning string output to object

Tagged: 

This topic contains 2 replies, has 2 voices, and was last updated by Profile photo of Poshoholic Poshoholic 4 years ago.

  • Author
    Posts
  • #7079
    Profile photo of Dougie Carson
    Dougie Carson
    Participant

    Hi Guys,

    I am trying to take the output i am getting when querying a HP c-class enclosure and enter the output into an object. I am trying to create 6 properties and each column within the txt file would populate them.
    The part i am having issues with is taking each line and populating the object with the contents. The second part of this is on lines i have 6 fields and other i have 2. I am having issues trying to account for this. I have attached the txt file i am trying to convert into an object.

    Here is what i have so far.

    $test=get-content c:\temp\output.txt

    $lines = $test.ToString().split([char]10) |ForEach-Object {$_}

     foreach ($insin$lines){

    $bay = $ins.Substring(0,3).trim()

    $ar=$ins.Split()

    }

    $object=New-Object –TypeNamePSObject
    $object |Add-Member–MemberTypeNoteProperty–NameBay-Value ""
    $object |Add-Member–MemberTypeNoteProperty–NameServerName-Value ""
    $object |Add-Member–MemberTypeNoteProperty–NameSerialNumber-Value ""
    $object |Add-Member–MemberTypeNoteProperty–NameStatus-Value ""
    $object |Add-Member–MemberTypeNoteProperty–NamePower-Value ""
    $object | Add-Member–MemberTypeNoteProperty–NameUID-Value ""
    $object | Add-Member–MemberTypeNoteProperty–NamePartner-Value""

  • #7084
    Profile photo of Dougie Carson
    Dougie Carson
    Participant

    Apologies for the previous post.

    I am trying to take the output i am getting when querying a HP c-class enclosure and enter the output into an object. I am trying to create 6 properties and each column within the txt file would populate them.
    The part i am having issues with is taking each line and populating the object with the contents. The second part of this is on lines i have 6 fields and other i have 2. I am having issues trying to account for this. I have attached the txt file i am trying to convert into an object.

    Here is what i have so far

    `$content = get-content c:\temp\output.txt

    $lines = $test.ToString().split([char]10) | ForEach-Object {$_ }
    foreach($ins in $lines){

    #$bay = $ins.Substring(0,3).trim()

    $ar = $ins.Split()

    }

    #Create empty object
    $object = New-Object –TypeNamePSObject
    $object | Add-Member –MemberType NoteProperty –Name Bay -Value ""
    $object | Add-Member –MemberType NoteProperty –Name ServerName -Value ""
    $object | Add-Member –MemberType NoteProperty –Name SerialNumber -Value ""
    $object | Add-Member –MemberType NoteProperty –Name Status -Value ""
    $object | Add-Member –MemberType NoteProperty –Name Power -Value ""
    $object | Add-Member –MemberType NoteProperty –Name UID -Value ""
    $object | Add-Member –MemberType NoteProperty –Name Partner -Value ""

  • #7096
    Profile photo of Poshoholic
    Poshoholic
    Member

    Looking at your text file, it seems that the following conclusions can be drawn:

    1. The first value will always be a number.
    2. The second value may contain spaces.
    3. The remaining values will not contain spaces.
    4. All values except for the first two will be separated by two or more spaces.

    If that is the case, you can split each row up into properties quite easily using regular expressions. Consider this snippet as an example:

    $contents = Get-Content enc.txt
    $indexedValues = @{}
    foreach ($row in $contents) {
    # trim the leading and trailing whitespace in the row
    $row = $row -replace '^\s+|\s+$'
    # pull the index from the start of the row
    $index = $row -replace '^(\d+)\s+.*$','$1' -as [int]
    # pull the values from the rest of the row (splitting on 2+ spaces)
    $fields = $row -replace '^\d+\s+' -split '\s{2,}'
    # now process the values (or put them in a table as an example)
    $indexedValues[$index] = $fields
    }

    Once that is done, you can then process each item in the table like this:

    foreach ($index in $indexedValues.Keys) {
    $indexedValues[$index]
    }

    Checking for the count of the number of values in there should allow you to handle the scenario where some rows have x fields and others y.

    From there you should be able to create meaningful objects. If you're still having difficulty with this (perhaps my conclusions at the top were incorrect), let me know.

You must be logged in to reply to this topic.