how to read next lines in a group using powershell

Welcome Forums General PowerShell Q&A how to read next lines in a group using powershell

  • This topic has 4 replies, 2 voices, and was last updated 1 month ago by
    Participant
    .
Viewing 4 reply threads
  • Author
    Posts
    • #234337
      Participant
      Topics: 11
      Replies: 9
      Points: 110
      Rank: Participant

      Hi
      My input csv file is as below

      Loginname	Orgname	Projectname	Dbname	roleName
      usera	Organisation1	project1	sales	read
      usera	Organisation1	project1	atlasAdmin	read
      userb	Organisation1	project2	marketing	read
      userb	Organisation1	project2	sales	read
      userb	Organisation1	project2	atlasAdmin	admin
      Below code creates two groups
      $inputfile = Import-Csv -path $csvfile
      $groups=$inputfile|Group-Object loginname,Projectname,Orgname|Select-Object @{n='loginname';e={$_.Group[0].loginname}},
      @{n='Projectname';e={$_.Group[0].Projectname}},@{n='Orgname';e={$_.Group[0].Orgname}},
      @{n='Dbname';e={$_.Group[0].Dbname}},@{n='roleName';e={$_.Group[0].roleName}},Count 
      $groups
      foreach($Obj in $groups) {
          for ($i=1; $i -le $obj.count; $i++) {
          $dbname=$obj.Dbname
          $rolename=$obj.roleName
          $usrname=$obj.loginname
          $prjname=$obj.Projectname
          write-Host "username" $usrname "projectname" $prjname "databasename" $dbname "rolename" $roleName 
          }
          }
      In the Output , i am able to see only first record of each group , I am not able to retrieve next lines from each group
      Although my nested loop is iterating as per count of each group ,instead of reading next lines,same first line is repeated in each group,please suggest, how we can read next lines in each group
      Below is the output returned from above code
      loginname    : usera
      Projectname  : project1
      AtlasOrgname :
      Dbname       : sales
      roleName     : read
      Count        : 2
      
      loginname    : userb
      Projectname  : project2
      AtlasOrgname :
      Dbname       : marketing
      roleName     : read
      Count        : 3
      username usera projectname project1 databasename sales rolename read
      username usera projectname project1 databasename sales rolename read
      username userb projectname project2 databasename marketing rolename read
      username userb projectname project2 databasename marketing rolename read
      username userb projectname project2 databasename marketing rolename read
      • This topic was modified 1 month, 1 week ago by pk30.
      • This topic was modified 1 month, 1 week ago by pk30.
      • This topic was modified 1 month, 1 week ago by pk30.
      • This topic was modified 1 month, 1 week ago by pk30.
      • This topic was modified 1 month, 1 week ago by pk30.
    • #234355
      Participant
      Topics: 5
      Replies: 2384
      Points: 6,066
      Helping Hand
      Rank: Community MVP

      pallavi

      I already asked this in your other thread. This way it’s a lot of work to use your source data. So please format sample data as code as well.

      And I already asked this in the other thread as well. What’s the desired output? You group the source data and then you un-group it again. That does not make that much sense. We might be able to recommend or help if you’d answer.

      Thanks in advance.

    • #234370
      Participant
      Topics: 11
      Replies: 9
      Points: 110
      Rank: Participant

      Hi ,

      Existing code:
      Currently i am calling Invoke-RestMethodwith POST or PATCH for every record from csv , and sending an email to user after processing each record. The problem with that approach is , if the csv has two records with same organisation ,project and username but different values for database,role names , i am sending two emails.

      Proposed Change :
      if the username,organisation name and project name is same for couple of records , Send a consolidated email to user after doing post/patch instead of sending multiple emails to same user.

      To implement that :
      1) First i used Group-Object on username,projectname,organisation name .
      2) Next i am trying to iterate in each group , and for each record in a group , am trying to call POST/PATCH method
      3) After processing all the records in a group , i am planning to send consolidated email with the list
      of Database names and Role names

      Issue Faced :
      1) After creating groups from input csv file , I am only able to read first line from a group, and not able to read next lines with in a group

      Please correct me if there is any simpler approach of if my existing code needs any changes to read all the records in a group.

    • #234409
      Participant
      Topics: 5
      Replies: 2384
      Points: 6,066
      Helping Hand
      Rank: Community MVP

      Do you use tabs as delimiters in your CSV file?

      This could be a starting point:

      $inputfile = Import-Csv -path $csvfile
      $groups = 
      $inputfile | 
          Group-Object loginname, Projectname, Orgname 
      foreach ($Obj in $groups) {
          [PSCustomObject]@{
              loginname = $Obj.Group[0].loginname
              ProjectName = $Obj.Group[0].ProjectName
              OrgName     = $Obj.Group[0].OrgName
              ProjectData = 
              foreach($project in $Obj.Group){
                  [PSCustomObject]@{
                      DBName = $project.DBName
                      RoleName = $project.RoleName
                  }
              }
          }
      }
    • #234511
      Participant
      Topics: 11
      Replies: 9
      Points: 110
      Rank: Participant

      Thank you for the suggestions !!

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