Multiple Parameter Entries Issue

Welcome Forums General PowerShell Q&A Multiple Parameter Entries Issue

  • This topic has 2 replies, 2 voices, and was last updated 2 months ago by
    J
    Participant
    .
Viewing 2 reply threads
  • Author
    Posts
    • #200465
      J
      Participant
      Topics: 3
      Replies: 6
      Points: 42
      Rank: Member

      Wondering if anyone can help with this issue please.

      I’m running the following script to check whether a user exists in an AD Group. If they do, return a message saying that they already exist in the AD Group. If they don’t, add them to the AD Group and return a message to say that they have been added.

      Param(
      
        [Parameter(
      
            Mandatory=$True)]
      
        [string[]]
      
        $Username
      
                  )
      
      $GroupMemberUsernames = (Get-ADGroupMember -Identity "ADGroupX" -recursive | get-aduser -properties SamAccountName).SamAccountName
      
       
          If ($GroupMemberUsernames -contains $Username) {
      
            Write-Host "$Username already exists in ADGroupX"
      
           } 
      
          Else {
      
            Add-ADGroupMember -id "ADGroupX" -members $Username
      
            Write-Host "$Username has been added to ADGroupX"
      
                }

      I am being prompted for multiple usernames one after the other until I leave one blank, at which point the search runs. This is what I wanted because it means that I can perform the script for multiple users and is why I used parameters instead of read-host.

      The script works to a point in that it adds multiple users to ADGroupX if they aren’t already in it.

      Unfortunately, it seems to run the search for the multiple users at the same time rather than individually. i.e. when I run the script for UserX and User Y, the output shows:

      ‘UserX UserY has been added to ADGroupX’

      rather than:

      ‘UserX has been added to ADGroupX

      UserY has been added to ADGroupX’

      This also means that I never get a message saying that ‘UserX already exists in ADGroupX’ even when they do. I think because it is looking for ‘User X User Y’ in ADGroup X rather than just User X, then just User Y?

       

    • #200468
      Senior Moderator
      Topics: 3
      Replies: 121
      Points: 638
      Helping Hand
      Rank: Major Contributor

      When you assign multiple values to $Username you are creating an array. When you call the $Username variable later (e.g. line 16, 18, 24, 26), you are calling the entire array, rather than the values (usernames) one at a time.

      Probably the easiest way for you to accomplish what you want is to loop through the names in your array with foreach so that they can be checked one at a time.

    • #200591
      J
      Participant
      Topics: 3
      Replies: 6
      Points: 42
      Rank: Member

      Thanks for your help grokkit

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