how to iterate over all elements of each group in powershell

Welcome Forums General PowerShell Q&A how to iterate over all elements of each group in powershell

Viewing 2 reply threads
  • Author
    Posts
    • #233848
      Participant
      Topics: 11
      Replies: 9
      Points: 110
      Rank: Participant

      I have a input csv file as below

      Loginname AtlasOrgname Projectname Dbname roleName
      usera organisation1 project1 marketing read
      usera organisation1 project1 sales read
      userb organisation1 project2 sales read
      userb organisation1 project2 marketing read

      I have created groups as below, code is as below

      $inputfile = Import-Csv -path $csvfile
      $groups=$inputfile|Group-Object loginname,Projectname,AtlasOrgname|Select-Object @{n='loginname';e={$_.Group[0].loginname}},
      @{n='Projectname';e={$_.Group[0].Projectname}},@{n='AtlasOrgname';e={$_.Group[0].AtlasOrgname}},
      @{n='Dbname';e={$_.Group[0].Dbname}},@{n='roleName';e={$_.Group[0].roleName}},Count 
      $groups | ForEach-Object { 
        $dbname = $_.Dbname
        $usrname = $_.loginname
        $prjname=$_.Projectname
        $dbname=$_.Dbname
        $rolename=$_.rolename
         write-Host "databaseName:" $dbname "," "roleName:" $rolename "," "projectname" $prjname "," "username" $usrname
         }
      
      In the Output , i am able to see only first record of each group , how can i loop over all records in each group
      To be more specific,Inside each group ,How can i iterate over each record of each group
      My plan is to use Invoke-RestMethod for each record in each group and then after processing all records in a group ,i want to use Send-MailMessage function at the end of each group instead of each record
      can some one please help , how can i iterate over each record in each group , Thank you
      • 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.
    • #233857
      Participant
      Topics: 5
      Replies: 2384
      Points: 6,066
      Helping Hand
      Rank: Community MVP

      pallavi

      Please, when you post code, error messages, sample data or console output format it as code, please.
      In the “Text” view you can use the code tags “PRE“, in the “Visual” view you can use the format template “Preformatted“. You can go back edit your post and fix the formatting – you don’t have to create a new one.
      Thanks in advance.

      What would be the desired output? Why don’t you simply iterate over all elements in input file?

    • #234319
      Participant
      Topics: 12
      Replies: 24
      Points: -5
      Rank: Member

      When Powershell imports your file on line #1 it creates an object of your values in your sheet so you can just iterate over that variable(object).

      $inputfile = Import-Csv -path $csvfile

      Foreach($row in $inputFile){
      $row
      }

       

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