Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7308221
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 28, 20262026-05-28T23:31:40+00:00 2026-05-28T23:31:40+00:00

I am parsing text output from a disk array that lists information about LUN

  • 0

I am parsing text output from a disk array that lists information about LUN snapshots in a predictable format. After trying every other way to get this data out of the array in a useable manner, the only thing I can do is generate this text file and parse it. The output looks like this:

SnapView logical unit name:  deleted_for_security_reasons
SnapView logical unit ID:  60:06:01:60:52:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX:XX
Target Logical Unit:  291
State:  Inactive

This repeats all through the file with one line break between each group. I want to identify a group, parse each of the four lines, create a new PSObject, add the value for each line as a new NoteProperty, and then add the new object to a collection.

What I can figure out is, once I identify the first line in the block of four lines, how to then process the text from lines two, three, and four. I’m looping through each line, finding the start of a block, and then processing it. Here’s what I have so far, with comments where the magic goes:

$snaps = get-content C:\powershell\snaplist.txt
$snapObjects = @()

foreach ($line in $snaps)
    {

        if ([regex]::ismatch($line,"SnapView logical unit name"))
        {
            $snapObject = new-object system.Management.Automation.PSObject
            $snapObject | add-member -membertype noteproperty -name "SnapName" -value $line.replace("SnapView logical unit name:  ","")
            #Go to the next line and add the UID
            #Go to the next line and add the TLU
            #Go to the next line and add the State
            $snapObjects += $snapObject
        }
}

I have scoured the Google and StackOverflow attempting to figure out how I can reference the line number of the object I’m iterating through, and I can’t figure it out. I may rely on foreach loops too much and so that’s affecting my thinking, I don’t know.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-28T23:31:40+00:00Added an answer on May 28, 2026 at 11:31 pm

    As you say, I think you’re thinking too much foreach when you should be thinking for. The below modification should be more along the lines of what you’re looking for:

    $snaps = get-content C:\powershell\snaplist.txt
    $snapObjects = @()
    
    for ($i = 0; $i -lt $snaps.length; $i++)
        {
            if ([regex]::ismatch($snaps[$i],"SnapView logical unit name"))
            {
                $snapObject = new-object system.Management.Automation.PSObject
                $snapObject | add-member -membertype noteproperty -name "SnapName" -value ($snaps[$i]).replace("SnapView logical unit name:  ","")
                # $snaps[$i+1] Go to the next line and add the UID
                # $snaps[$i+2] Go to the next line and add the TLU
                # $snaps[$i+3] Go to the next line and add the State
                $snapObjects += $snapObject
            }
    }
    

    A while loop may be even cleaner because then you can increment $i by 4 instead of 1 when you hit this case, but since the other 3 lines won’t trigger the “if” statement… there’s no danger, just a few wasted cycles.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I know plenty about the different ways of handling parsing text for information. For
I'm parsing text from a file and storing it in a string. The problem
I am parsing text that has a heading and then data that applies to
Apart from just inserting and parsing text into a blank Word field, is there
I am parsing an RSS feed from the following URL: http://rss.sciam.com/ScientificAmerican-Global?format=xml // $xml_text is
We have a command line exe that takes input from a text file and
I'm trying to reverse-engineer a program that does some basic parsing: text in, text
My Perl program takes some text from a disk file as input, wraps it
Say that you want to do some fancy formatting of some tabular output from
Good day, I am retrieving information from various websites using cURL and various parsing

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.