0

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.

Arthur
  • 103
  • 11
  • store your user list in a $Var - likely an array. then iterate thru it with `foreach` and replace your `-UserPrincipalName "user1@contoso.com"` with `-UserPrincipalName "$CurrentUserFromList"`. you may need to expand that item if the data you need is in a property of the item. – Lee_Dailey Feb 26 '20 at 00:40
  • @Lee_Dailey Can you give me a sample code, I am sorry i don't know powershell very well. Thanks so much. – Arthur Feb 26 '20 at 01:44
  • please take a look at the Answer i posted. if you have questions, please ask ... but it is getting close to my bedtime, so my response may be delayed ... [*grin*] – Lee_Dailey Feb 26 '20 at 02:12

1 Answers1

1

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
==============================
Lee_Dailey
  • 7,292
  • 2
  • 22
  • 26
  • thanks so much for your demo, I can understand it now. By the way, do you have some advice about **# in real life, use Get-Content or some other method**, because I have to add more than 1000 users. – Arthur Feb 26 '20 at 02:21
  • @Arthur - you are most welcome! [*grin*] you likely can query your Active Directory setup for the users. the result would likely give you something like `$UL_Item.UPN` instead of a simple `$UL_Item` that i used. – Lee_Dailey Feb 26 '20 at 02:27
  • @Arthur - you are most welcome! good luck to you ... [*grin*] – Lee_Dailey Feb 26 '20 at 14:16
  • Hi, Lee, thanks for your advice and I found it can run very well. By the way, can you check my other post **How to change the loop Powershell content from the csv file automatically?"**, hope you can help me achieve the goal. – Arthur Mar 10 '20 at 11:48
  • @Arthur - i'll go take a look ... [*grin*] – Lee_Dailey Mar 10 '20 at 20:35