# Loop Through Variables

**URL:** <https://forums.powershell.org/t/loop-through-variables/13561>\
**Category:** PowerShell Help\
**Created:** [December 18, 2019, 9:36am UTC](https://forums.powershell.org/t/loop-through-variables/13561 "2019-12-18T09:36:32Z")\
**Posts on this page:** 9\
**Page:** 1

<div class="post-metadata">

**Author:** ![anelliaf](https://avatars.discourse-cdn.com/v4/letter/a/cab0a1/32.png) [@anelliaf](https://forums.powershell.org/u/anelliaf)\
**Post date:** [December 18, 2019, 9:36am UTC](https://forums.powershell.org/t/loop-through-variables/13561/1 "2019-12-18T09:36:32Z")

</div>

Hello,

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

[pre]

$rgName = ‘rg-name’

## 

## constructing a custom table for the following 3 elements:

## data column, heading, and width

## 

$cusTab1 = @{Expression={$_.DisplayName};Label=“Display Name”;width=25}, `  
@{Expression={$_.SignInName};Label=“Login Name”;width=40}, `  
@{Expression={$\_.RoleDefinitionName};Label=“Role”;width=20}, `  
@{Expression={$rgName};Label=“Resource Group”;width=30}

$List = Get-AzRoleAssignment -ResourceGroupName $rgName | where {($\_.RoleDefinitionName -in “Owner”)} | Select DisplayName,SignInName,RoleDefinitionName  
$List | Format-Table $cusTab1

[/pre]

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

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [December 18, 2019, 10:16am UTC](https://forums.powershell.org/t/loop-through-variables/13561/2 "2019-12-18T10:16:06Z")

</div>

The first thing you should check is if the -ResouceGroupName accepts a string array by checking [the documentation](https://docs.microsoft.com/en-us/powershell/module/az.resources/get-azroleassignment?view=azps-3.2.0). This would allow you to do something like this:

```
Get-AzRoleAssignment -ResourceGroupName 'rg1','rg2','rg3' ...
```

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:

```
$roleGroups = 'rg1','rg2','rg3'

$results = foreach ($roleGroup in $roleGroups) {
    $cusTab1 = @{Expression={$_.DisplayName};Label="Display Name";width=25}, `
               @{Expression={$_.SignInName};Label="Login Name";width=40}, `
               @{Expression={$_.RoleDefinitionName};Label="Role";width=20}, `
               @{Expression={$roleGroup};Label="Resource Group";width=30}

    Get-AzRoleAssignment -ResourceGroupName $rgName | 
    where {($_.RoleDefinitionName -in "Owner")} | 
    Select -Property $cusTab1
}

$results | Format-Table
```

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

---

<div class="post-metadata">

**Author:** ![anelliaf](https://avatars.discourse-cdn.com/v4/letter/a/cab0a1/32.png) [@anelliaf](https://forums.powershell.org/u/anelliaf)\
**Post date:** [December 18, 2019, 11:00am UTC](https://forums.powershell.org/t/loop-through-variables/13561/3 "2019-12-18T11:00:19Z")

</div>

That almost worked! When I ran the following:

[pre]  
$results = foreach ($roleGroup in $roleGroups) {  
$cusTab1 = @{Expression={$_.DisplayName};Label=“Display Name”;width=25}, `  
@{Expression={$_.SignInName};Label=“Login Name”;width=40}, `  
@{Expression={$_.RoleDefinitionName};Label=“Role”;width=20}, `  
@{Expression={$roleGroup};Label=“Resource Group”;width=30}  
Get-AzRoleAssignment -ResourceGroupName $roleGroup | where {($_.RoleDefinitionName -in “Owner”)} | Select -Property $cusTab1  
}

$results | Format-Table  
[/pre]

It returned the error:

[pre]  
Select : The width key is not valid.  
At line:6 char:104

- … ere {($\_.RoleDefinitionName -in “Owner”)} | Select -Property $cusTab1
- 

```
                                            ~~~~~~~~~~~~~~~~~~~~~~~~~

```

  - CategoryInfo : InvalidArgument: (🙂 [Select-Object], NotSupportedException
  - FullyQualifiedErrorId : DictionaryKeyIllegal,Microsoft.PowerShell.Commands.SelectObjectCommand  
Select : The width key is not valid.  
At line:6 char:104

- … ere {($\_.RoleDefinitionName -in “Owner”)} | Select -Property $cusTab1
- 

```
                                            ~~~~~~~~~~~~~~~~~~~~~~~~~

```

  - CategoryInfo : InvalidArgument: (🙂 [Select-Object], NotSupportedException
  - FullyQualifiedErrorId : DictionaryKeyIllegal,Microsoft.PowerShell.Commands.SelectObjectCommand  
[/pre]

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:

[pre]  
Get-AzRoleAssignment -ResourceGroupName $roleGroup | where {($\_.RoleDefinitionName -in “Owner”)} | Select DisplayName,SignInName,RoleDefinitionName  
[/pre]

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?

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [December 18, 2019, 12:29pm UTC](https://forums.powershell.org/t/loop-through-variables/13561/4 "2019-12-18T12:29:17Z")

</div>

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

```
$roleGroups = 'rg1','rg2','rg3'

$results = foreach ($roleGroup in $roleGroups) {

    Get-AzRoleAssignment -ResourceGroupName $rgName | 
    where {($_.RoleDefinitionName -in "Owner")} | 
    Select -Property @{Name='RoleGroup';Expression={$roleGroup}},DisplayName,SignInName,RoleDefinitionName

}

$results | Format-Table -Property @{Expression={$_.DisplayName};Label="Display Name";width=25}, `
                                  @{Expression={$_.SignInName};Label="Login Name";width=40}, `
                                  @{Expression={$_.RoleDefinitionName};Label="Role";width=20}, `
                                  @{Expression={$_.RoleGroup};Label="Resource Group";width=30}
```

---

<div class="post-metadata">

**Author:** ![anelliaf](https://avatars.discourse-cdn.com/v4/letter/a/cab0a1/32.png) [@anelliaf](https://forums.powershell.org/u/anelliaf)\
**Post date:** [December 18, 2019, 12:44pm UTC](https://forums.powershell.org/t/loop-through-variables/13561/5 "2019-12-18T12:44:40Z")

</div>

That worked! Thanks for all the help!

---

<div class="post-metadata">

**Author:** ![anelliaf](https://avatars.discourse-cdn.com/v4/letter/a/cab0a1/32.png) [@anelliaf](https://forums.powershell.org/u/anelliaf)\
**Post date:** [December 20, 2019, 1:06pm UTC](https://forums.powershell.org/t/loop-through-variables/13561/6 "2019-12-20T13:06:10Z")

</div>

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:

[pre]  
$roleGroups = ‘use2-dev-rg’,‘use2-prd-rg’,‘use2-qa-rg’

$results = foreach ($roleGroup in $roleGroups) {

```
Get-AzRoleAssignment -ResourceGroupName $roleGroup | 
where {($_.RoleDefinitionName -in "Owner") -and ($_.DisplayName -ne "AZ Administrators")} | 
Select -Property @{Name='RoleGroup';Expression={$roleGroup}},DisplayName,SignInName,RoleDefinitionName,Scope

```

}

$results | Format-Table -Property @{Expression={$_.DisplayName};Label=“Display Name”;width=25}, `  
@{Expression={$_.SignInName};Label=“Login Name”;width=40}, `  
@{Expression={$_.RoleDefinitionName};Label=“Role”;width=20}, `  
@{Expression={$_.RoleGroup};Label=“Resource Group”;width=30}, `  
@{Expression={if ($_.Scope.Split(‘/’)[-1] -eq “f873d82f-7491-4045-8dbe”) { $result = “DEV” } else {$_.Scope.Split(‘/’)[-1]} $result };Label=“Scope”;width=150}

[/pre]

---

<div class="post-metadata">

**Author:** ![rob-simmers](https://sea1.discourse-cdn.com/flex019/user_avatar/forums.powershell.org/rob-simmers/32/1010_2.png) [@rob-simmers](https://forums.powershell.org/u/rob-simmers)\
**Post date:** [December 20, 2019, 1:40pm UTC](https://forums.powershell.org/t/loop-through-variables/13561/7 "2019-12-20T13:40:27Z")

</div>

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:

```
@{Expression={if ($_.Scope.Split('/')[-1] -eq "f873d82f-7491-4045-8dbe") { "DEV" } else {$_.Scope.Split('/')[-1]} };Label="Scope";width=150}
```

---

<div class="post-metadata">

**Author:** ![anelliaf](https://avatars.discourse-cdn.com/v4/letter/a/cab0a1/32.png) [@anelliaf](https://forums.powershell.org/u/anelliaf)\
**Post date:** [December 23, 2019, 8:15am UTC](https://forums.powershell.org/t/loop-through-variables/13561/8 "2019-12-23T08:15:27Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![dotnVo](https://avatars.discourse-cdn.com/v4/letter/d/4af34b/32.png) [@dotnVo](https://forums.powershell.org/u/dotnVo)\
**Post date:** [May 16, 2024, 8:30pm UTC](https://forums.powershell.org/t/loop-through-variables/13561/9 "2024-05-16T20:30:52Z")

</div>

Closed on May 16, 2024, 8:30pm UTC


