Search code examples
azurepowershellazure-active-directorywvd

How to use loop powershell to assign multiple users to access an app group?


I want to add multiple users to my app group at a time, is there someone know how to user loop powershell to do that?

The Add-RdsAppGroupUser cmdlet as below can assigns a user to access the specified app group. This cmdlet only takes in a single user principal name (UPN) at a time and only applies to users (not groups).

Add-RdsAppGroupUser -TenantName "contoso" -HostPoolName "contosoHostPool" -AppGroupName "Desktop Application Group" -UserPrincipalName "user1@contoso.com"

For example: I have 1000 users from user1@contoso.com to user1000@contoso.com, what should I write the code and how can i check the result after I finish it.


Solution

  • here's a demo of the idea. i don't have access to the cmdlet you use, so the loop shows its resulting parameter splat.

    #region >>> fake getting a list of users
    #    in real life, use Get-Content or some other method
    $UserList = @(
        'One@contoso.com'
        'Two@contoso.com'
        'Three@contoso.com'
        'Four@contoso.com'
        'Five@contoso.com'
        )
    #endregion >>> fake getting a list of users
    
    foreach ($UL_Item in $UserList)
        {
        # the following structure is called "Splatting"
        #    it puts some - or all - the parameters into a hashtable
        #    that can be fed to the cmdlet by replacing the "$" with an "@"
        $ARAGU_Params = @{
            TenantName = "contoso"
            HostPoolName = "contosoHostPool"
            AppGroupName = "Desktop Application Group"
            UserPrincipalName = $UL_Item
            }
        #Add-RdsAppGroupUser @ARAGU_Params
    
        # i don't have the above cmdlet, so this is just showing the parameters & values being passed to it
        $ARAGU_Params
        '=' * 30
        }
    

    output ...

    Name                           Value
    ----                           -----
    HostPoolName                   contosoHostPool
    UserPrincipalName              One@contoso.com
    TenantName                     contoso
    AppGroupName                   Desktop Application Group
    ==============================
    HostPoolName                   contosoHostPool
    UserPrincipalName              Two@contoso.com
    TenantName                     contoso
    AppGroupName                   Desktop Application Group
    ==============================
    HostPoolName                   contosoHostPool
    UserPrincipalName              Three@contoso.com
    TenantName                     contoso
    AppGroupName                   Desktop Application Group
    ==============================
    HostPoolName                   contosoHostPool
    UserPrincipalName              Four@contoso.com
    TenantName                     contoso
    AppGroupName                   Desktop Application Group
    ==============================
    HostPoolName                   contosoHostPool
    UserPrincipalName              Five@contoso.com
    TenantName                     contoso
    AppGroupName                   Desktop Application Group
    ==============================