Loop Through Variables

Welcome Forums General PowerShell Q&A Loop Through Variables

Viewing 7 reply threads
  • Author
    Posts
    • #194144
      Participant
      Topics: 21
      Replies: 41
      Points: 303
      Rank: Contributor

      Hello,

      I have the following code which lists Azure resource group owners and displays the output in a custom table:

      What I would like to do is run this for multiple resource groups and then display the values in the custom table?  With this code, I can only run it for one resource group at a time.

      Thanks,

      Frank

    • #194153
      Participant
      Topics: 13
      Replies: 1753
      Points: 3,136
      Helping Hand
      Rank: Community Hero

      The first thing you should check is if the -ResouceGroupName accepts a string[] array by checking the documentation. This would allow you to do something like this:

      This would handle the for loop inside of the cmdlet, however, this parameter only accepts a string which means you need to perform the for loop yourself:

      Note: Did not test this code, but this should be close.

    • #194168
      Participant
      Topics: 21
      Replies: 41
      Points: 303
      Rank: Contributor

      That almost worked! When I ran the following:

      It returned the error:

      You had $rgName as the variable for -ResourceGroupName? I changed that to $roleGroup. As for the “The width key is not valid” message, I changed the SELECT statement to:

      It now returns records, but it’s not showing a resource group name (which I understand why). I guess I now need to figure out how to avoid the “width” error message so I can include the $roleGroups values?

    • #194246
      Participant
      Topics: 13
      Replies: 1753
      Points: 3,136
      Helping Hand
      Rank: Community Hero

      Sorry, I didn’t see width in that expression, that is only applicable to Format-Table, try this:

    • #194252
      Participant
      Topics: 21
      Replies: 41
      Points: 303
      Rank: Contributor

      That worked! Thanks for all the help!

    • #194705
      Participant
      Topics: 21
      Replies: 41
      Points: 303
      Rank: Contributor

      Not sure if I should open a new discussion topic, or continue adding to this one since it’s the same piece of code?

      This is a 2-part question:

      1 – I’m placing an if-else statement inside the custom table to return a predetermined value for a returned value (i.e., returned value = “f873d82f-7491-4045-8dbe”, but want it to be “DEV”. This piece is figured out, but for other values additional items are returned:

      Should be: use2-dev-rg
      Returned value: {use2-dev-rg, $null}

      2 – Eventually I’m going to need to define additional “if-else” statements. Would I continue to add the code like this and is this the best approach?

      The code I’m using is:

    • #194711
      Participant
      Topics: 13
      Replies: 1753
      Points: 3,136
      Helping Hand
      Rank: Community Hero

      You are returning two things if it is not the DEV value, the value and the $result. just change your if you return one or the other:

    • #194912
      Participant
      Topics: 21
      Replies: 41
      Points: 303
      Rank: Contributor

      Gotcha. So having the $result variable is returning two values?

Viewing 7 reply threads
  • The topic ‘Loop Through Variables’ is closed to new replies.