ForEach-Object loop get back to 1 line

Welcome Forums General PowerShell Q&A ForEach-Object loop get back to 1 line

Viewing 11 reply threads
  • Author
    Posts
    • #270047
      Participant
      Topics: 12
      Replies: 60
      Points: 187
      Rank: Participant

      Hello,

      As the $foreach.movenext(), i need to go to the precedent line, but how ?

      As you can see :

      Thx for reply.

    • #270080
      Senior Moderator
      Topics: 9
      Replies: 1422
      Points: 5,289
      Helping Hand
      Rank: Community MVP

      Can you tell us what are you trying to achieve ?, it will help folks here to understand the code better.

    • #270131
      Participant
      Topics: 12
      Replies: 60
      Points: 187
      Rank: Participant

      Hello Kv,

      It’s a little bit tricky too explain, and i don’t have the file to show you, but i can try to create the file :

      IMAGE server.powershell.org server.powershell.org_12345678
      FRAG 1 1 0 0 0 0 @aaaab
      FRAG 2 1 0 0 0 0 @aaaac
      FRAG 1 2 0 0 0 0 @aaaab
      IMAGE server2.powershell.org server2.powershell.org_12345678
      FRAG 1 1 0 0 0 0 @aaaac
      FRAG 1 2 0 0 0 0 @aaaab
      FRAG 1 3 0 0 0 0 @aaaab

      In this file i need to seach, each line with the word “*IMAGE*”:
      After this line i must to go to next line and search if i have the Media-ID like “@aaaac”.
      If i have i must take a part from the first line “server.powershell.org_12345678_C2”, add the text “_C”+2 of the third line what is the number of the copy.
      and put the result in a file.
      i done the same thing for each line until the new line IMAGE, where i must donne exactly the same thing.

      But the problm in my code is my test Until, to done the test i’m already athe the second line IMAGE, so when i come back to the If, i don’t read it !
      It’s why i need to know if it’s possible to come back to the precedent line like the $foreach.movenext(), why not $foreach.moveprevious()

      I hope my explication are clear for you 😉

      Thx .

    • #270149
      Participant
      Topics: 17
      Replies: 1869
      Points: 3,631
      Helping Hand
      Rank: Community Hero

      The attempted parsing looks very VBScript. Here is an example showing you how to parse each line as you have a somewhat simple pattern:

      This walks you through the parsing a bit:

      This would produce the following from the file:

    • #270155
      Participant
      Topics: 5
      Replies: 177
      Points: 686
      Helping Hand
      Rank: Major Contributor

      Renaud,

      You had me until “If I have I must take a part from the first line “server.powershell.org_12345678_C2”, add the text “_C”+2 of the third line what is the number of the copy and put the result in a file.”  Maybe if you gave an example of using this input, this should be the output would help.  Regardless, I don’t think there is any easy way to “moveprevious()”, but I have another idea to do what you want that might be a little simplar.

      In this example, I read the entire contents of your file as a single string instead of an array of strings by using the -raw switch.  Using your example just get rid of the original split on new line.  By doing this, I can use the split operator to make an array for each group that starts with “IMAGE”.  From there you can use Select-String or any other way you want to look for your criteria.

      • This reply was modified 3 weeks, 3 days ago by Mike R..
    • #270325
      Participant
      Topics: 12
      Replies: 60
      Points: 187
      Rank: Participant

      Hello Rob & Mike ^^,

       

      Just  information it’s just an little exemple of the file format i have, Really it’s a file with more 10 000 line.

      So i think the Rob way could not be used, perhap’s i’m wrong.

       



      @Mike
      :

      What i want to do when i have found a line with the mediaID : @aaaac

      I just explain more what the file contain :

      IMAGE server.powershell.org server.powershell.org_12345678 
      FRAG 1 1 0 0 0 0 @aaaab
      FRAG 2 1 0 0 0 0 @aaaac
      FRAG 1 2 0 0 0 0 @aaaab
      IMAGE server2.powershell.org server2.powershell.org_12345678
      FRAG 1 1 0 0 0 0 @aaaac
      FRAG 1 2 0 0 0 0 @aaaab
      FRAG 1 3 0 0 0 0 @aaaab
      It’s a part of a log create by an other script from Veritas NetBackup, so a Backup solution.
      IMAGE and FRAG is the Type :
      IMAGE is the Backup
      FRAG is fragment of the Backup behind.
      Ligne IMAGE :
      – Column 1 : IMAGE
      – Column 2 : Name of the server backup
      – Column 3 : Backup-ID of the backup
      Ligne FRAG :
      – Column 2 : 1 is the number of copy, can be 1 / 2 / 3 …, but there is in my cas almost Copy 1 or 2
      – Column 3 : 1 / 2 / 3 … number of the Fragment
      – Column 4 / 5 / 6 / 7 : i don’t know
      Raw 8 : Is the Media-ID so the ID of the storage where the backup is hosted.
      We have 4 storages different, so 4 Media-ID, but i’m looking for one example the @aaaac.
      So Now, when i have found the line IMAGE, i keep the Backup-ID in variable
      Search below the Media-ID @aaac until to the next line IMAGE.
      If i found @aaaac :
      – Take the number of Copy = $CopyNumber
      – Concat Backup-ID+”_C”+$CopyNumber and put it in a file with the Out-File.
      Exemple of a real File but just the 2 firt Backup, here the test for was searching the @aaaaz:
    • #270406
      Participant
      Topics: 12
      Replies: 60
      Points: 187
      Rank: Participant

      Hello all,

       

      Ok i’v found out ^^ :

       

      In fact Rob you make me on the good Way 😉

      Thx you both 😉

    • #270409
      Participant
      Topics: 5
      Replies: 177
      Points: 686
      Helping Hand
      Rank: Major Contributor

      Here’s my idea of a solution.  I used your example to test, but changed one of the media IDs to make sure it works since none in your sample were @aaaac

       

       

    • #270526
      Participant
      Topics: 12
      Replies: 60
      Points: 187
      Rank: Participant

      I will test it Mike 😉
      Thx and have a nice day or evening 😉

    • #270538
      Participant
      Topics: 9
      Replies: 170
      Points: 836
      Helping Hand
      Rank: Major Contributor

      I have no idea how my post wound up here … I was replying to a completely different topic. DISREGARD this.

       

    • #271840
      Participant
      Topics: 12
      Replies: 60
      Points: 187
      Rank: Participant

      Hello Mike,

       

      I have test your script and  works fine ^^

      Better than mine i think.

      But i dont’ understand all :

       

       

      Could you exlain to me what are doing the Select-Object -Skip 1
      And after you call a Method $rows = $group -split [environment]::NewLine, could you explain this line what is this method.

      Thx a lot.

    • #271882
      Participant
      Topics: 5
      Replies: 177
      Points: 686
      Helping Hand
      Rank: Major Contributor

      -Skip 1 parameter/argument ignores the first item in the array.  I do this on line 7 because

      will produce an array with a null item in the first position.  This is because the contents of the file starts with “IMAGE” and that is what we are splitting it on.  Here is a simplified example that illustrates the issue.

      Notice when I split on “Hello” the first item in the array is empty and the second item is “, World”.  This is because the string starts with the expression I am splitting on.  The second example I split on “,” and since it is NOT the first item in the string, the first element in the array is “Hello” and the second element is ” World”

      On line 14 I use the skip for a different reason.  On this line skipping the first item in the array will skip the IMAGE line leaving just the FRAG lines to process for the current IMAGE.

Viewing 11 reply threads
  • You must be logged in to reply to this topic.