add-adgroupmember in winfoms

matt chen 1 Reputation point
2020-12-24T17:21:59.56+00:00

I am trying to create a winform to create domain users . I am stuck on "add-adgroupmember" It's keep giving me error messgae" Add-ADGroupMember: Cannot convert 'Group1 Group2 Group3 Group4' to the type 'Microsoft.ActiveDirectory.Management.ADGroup' required by parameter 'Identity'. Specified method is not supported." All you need to do is change the "UserPrincipalName" and the code will work on any domain.

[reflection.assembly]::LoadWithPartialName("System.Windows.Forms") | Out-Null
Import-Module Microsoft.PowerShell.Security

$Form = New-Object System.Windows.Forms.Form
$textboxName = New-Object System.Windows.Forms.TextBox
$textboxPassword = New-Object System.Windows.Forms.TextBox
$textboxDescription = New-Object System.Windows.Forms.TextBox
$addbutton = New-Object System.Windows.Forms.Button
$droplist = New-Object System.Windows.Forms.ComboBox
$labelname = New-Object System.Windows.Forms.Label
$labelPassword = New-Object System.Windows.Forms.Label
$labelDescription = New-Object System.Windows.Forms.Label
$labelOU = New-Object System.Windows.Forms.Label
$checklist = New-Object System.Windows.Forms.CheckedListBox
$Password = New-Object System.Security.SecureString

$textboxName.Location = '23,23'
$textboxName.Size = '150,23'
$labelname.Text = 'Name:'
$labelname.Location = '23,5'

$textboxPassword.Location = '23,83'
$textboxPassword.Size = '150,23'
$labelPassword.Text ='Password:'
$labelPassword.Location = '23,65'

$textboxDescription.Location = '23,143'
$textboxDescription.Size = '150,23'
$labelDescription.Text = 'Description:'
$labelDescription.Location = '23,125'

$droplist.Location = '150,200'
$droplist.width = 240
$droplist.Height = 20
$oulist = Get-ADOrganizationalUnit -Filter 'Name -like "*"'
$droplist.Items.AddRange($oulist)
$labelOU.Text = 'OU'
$labelOU.Location = '150,180'

$checklist.Location = '185,80'
$checklist.Size = '200,100'
$checklist.CheckOnClick = $true
Get-ADGroup -Filter * |Select-Object Name | Out-File -FilePath C:\Windows\Temp\adgroup.txt
$grouplist = Get-Content C:\Windows\Temp\adgroup.txt
$checklist.Items.AddRange($grouplist)

$addbutton.Text = 'Add'
$addbutton.Location = '196,23'
$addbutton.Add_Click({
$Password = $textboxPassword.Text | ConvertTo-SecureString -AsPlainText -Force
If($textboxName.Text){New-ADUser $textboxName.Text -Enabled $true -AccountPassword $Password -ChangePasswordAtLogon $true -Description $textboxDescription.text -UserPrincipalName ($textboxName.Text+'@Matt Jemmett .lab') -Path $droplist.Text | Add-ADGroupMember -Identity $checklist.CheckedItems -Members $textboxName.Text -Confirm}
})

$Form.Controls.Add($textboxName)
$Form.Controls.Add($textboxPassword)
$Form.Controls.Add($textboxDescription)
$Form.Controls.Add($addbutton)
$Form.Controls.Add($droplist)
$Form.Controls.Add($labelname)
$Form.Controls.Add($labelPassword)
$Form.Controls.Add($labelDescription)
$Form.Controls.Add($labelOU)
$Form.Controls.Add($checklist)

$Form.ShowDialog()

Windows Forms
Windows Forms
A set of .NET Framework managed libraries for developing graphical user interfaces.
1,903 questions
Windows Server
Windows Server
A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.
13,234 questions
Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
6,651 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,554 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Andreas Baumgarten 111.5K Reputation points MVP
    2020-12-24T17:38:54.83+00:00

    The error message Cannot convert 'Group1 Group2 Group3 Group4' sounds like the property $checklist.CheckedItems contains all group names.

    You need something like a foreach to work with the groups one after another.


    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten


  2. Rich Matheisen 46,811 Reputation points
    2020-12-24T22:15:54.733+00:00

    The Add-AdGroupMember cmdlet's -Identity parameter takes only a single item, not an array of items. Also, you're piping the user from the New-ADUser and also providing the -Members parameter. I don't think the pipe is necessary.

    Another potential problem is that if you have more than one Domain Controller your Add-AdGroupMember may use a different DC to the one used by the New-ADUser! If that happens and replication hasn't yet completed (which is very likely) the Add-AdGroupMember may fail. You should find one DC and use it in both cmdlets.

    Try this modification to your code (note tat I haven't added the "-Server" parameter to either cmdlet!):

    $addbutton.Add_Click( {
            $Password = $textboxPassword.Text | ConvertTo-SecureString -AsPlainText -Force
            If ($textboxName.Text) { 
                New-ADUser $textboxName.Text -Enabled $true -AccountPassword $Password -ChangePasswordAtLogon $true -Description $textboxDescription.text -UserPrincipalName ($textboxName.Text + '@matt.lab') -Path $droplist.Text
                $checklist.CheckedItems |
                    ForEach-Object{
                        Add-ADGroupMember -Identity $_ -Members $textboxName.Text -Confirm 
                    }
            }
        })
    
    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.