question

mattchen-2761 avatar image
0 Votes"
mattchen-2761 asked RichMatheisen-8856 answered

add-adgroupmember in winfoms

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.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-serverwindows-server-powershellwindows-active-directorywindows-forms
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

AndreasBaumgarten avatar image
0 Votes"
AndreasBaumgarten answered mattchen-2761 commented

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


· 1
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.

I am using Powershell 7 and visual studio code. I added a "%" in front of $checklist.checkeditems . And It did't work . No error messages pop up . But the user was not in the selected group.

0 Votes 0 ·
RichMatheisen-8856 avatar image
0 Votes"
RichMatheisen-8856 answered

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 
                 }
         }
     })
5 |1600 characters needed characters left characters exceeded

Up to 10 attachments (including images) can be used with a maximum of 3.0 MiB each and 30.0 MiB total.