New-ADUser form

Ask questions about creating Graphical User Interfaces (GUI) in PowerShell and using WinForms controls.
Forum rules
Do not post any licensing information in this forum.

Any code longer than three lines should be added as code using the 'Select Code' dropdown menu or attached as a file.
This topic is 4 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked
User avatar
SCSBIT
Posts: 1
Last visit: Thu Jun 20, 2019 12:53 pm

New-ADUser form

Post by SCSBIT »

Please forgive me if this seems extremely basic. I have been going through the forum and have not found the help that I am looking for. Two problems I have questions on. I am creating a GUI form to create a new active directory user. I am having issues with the password part and the -Name parameter. I am assuming I do not have it formatted correctly. I have tried it with quotes and without quotes but because I am using the two variables $FirstName and $LastName it errors. Also I am having issues with the password.

The error I get for the name is this: New-ADUser : The name provided is not a properly formed account name
The password error is this: ConvertTo-SecureString : Cannot bind argument to parameter 'String' because it is an empty string

Here is my code:

Code: Select all

#Variables
$textboxPassword.UseSystemPasswordChar = $true
$textboxPasswordConfirm.UseSystemPasswordChar = $true
$FirstName = $textboxFirstName.Text
$LastName = $textboxLastName.Text
$DisplayName = $textboxDisplayName.Text
$Initials = $textboxInitials.Text
$Description = $textboxDescription.Text
$Password = $textboxPassword.Text | ConvertTo-SecureString -AsPlainText -Force
$CostCenter = $textboxCostCenter.Text
$State = $textboxState.Text
$Title = $textboxTitle.Text
$SamAccountName = $TextboxSamAccountName.Text

# Auto Populate Domains
$Forest = Get-ADForest
If ($null -eq $Forest)
{
	$InfoMessage = 'No Active Directory Forest was found when running Get-ADForest'
	$InfoTitle = "Warning"
	$comboboxDomains.Enabled = $false
}
Else
{
	$comboboxDomains.Enabled = $true
	$UPNs = @()
	$extraUPNS = ($Forest | Select-Object UPNSuffixes -ExpandProperty UPNSuffixes)
	If ($extraUPNs -ne $Null)
	{
		foreach ($extraUPN in $extraUPNs)
		{
			$UPNs += "@" + $extraUPN
		}
	}
	$UPNs += "@" + ($Forest | Select-Object -ExpandProperty Name)
	Update-ComboBox -ComboBox $comboboxDomains -Items $UPNs
}

# Auto Populate Ou's
$OUTree = (Get-ADOrganizationalUnit -Filter * -Properties CanonicalName | Sort-Object | Select-Object -Property CanonicalName).CanonicalName
If ($null -eq $OUTree)
{
	$InfoMessage = 'Could not load Organizational Unit structure'
	$InfoTitle = "Warning"
	$comboboxOUTree.Enabled = $false
}
Else
{
	Update-ComboBox -ComboBox $comboboxOUTree -Items $OUTree
	$comboboxOUTree.Enabled = $true
}

# Auto Populate Groups
$Groups = Get-ADGroup -Filter * | Where-Object { ($_.Name -ne "Domain Users") -and ($_.Name -ne "Domain Computers") } | Select-Object -ExpandProperty Name | Sort-Object
If ($null -eq $Groups)
{
	$InfoMessage = 'No Active Directory Group objects were found when running Get-ADGroup'
	$InfoTitle = "Warning"
}
Else
{
	Update-ListBox -ListBox $checkedlistboxGroups -Items $Groups
}

$buttonCreateUser_Click = {
	#TODO: Place custom script here
	 New-ADUser `
									  -Name "$FirstName $LastName" `
									  -DisplayName "$FirstName $LastName" `
									  -Description $Description `
									  -Initials $Initials `
									  -Company $CostCenter `
									  -State $State `
									  -Title $Title `
									  -SamAccountName $SamAccountName `
									  -UserPrincipalName "$SamAccountName@DomainName.com `
									  -AccountPassword $Password
	
}
User avatar
davidc
Posts: 5913
Last visit: Mon Jul 08, 2019 8:55 am
Been upvoted: 2 times

Re: New-ADUser form

Post by davidc »

[TOPIC MOVED TO POWERSHELL GUIS FORUM BY MODERATOR]
David
SAPIEN Technologies, Inc.
jvierra
Posts: 15439
Last visit: Tue Nov 21, 2023 6:37 pm
Answers: 30
Has voted: 4 times
Been upvoted: 33 times

Re: New-ADUser form

Post by jvierra »

Here is how to do this and how to manage a large number of parameters. This also avoids scoping issues.

Code: Select all

$buttonCreateUser_Click = {
    
    $userprops = @{
        Name = '{0} {1}' -f $textboxFirstName.Text,$textboxLastName.Text
        DisplayName = '{0} {1}' -f $textboxFirstName.Text, $textboxLastName.Text
        Description = $textboxDescription.Text
        Initials = $textboxInitials.Text
        Company = $textboxCostCenter.Text
        State = $textboxState.Text
        Title = $textboxTitle.Text
        SamAccountName = $TextboxSamAccountName.Text
        UserPrincipalName = '{0}@DomainName.com' -f $TextboxSamAccountName.Text
        AccountPassword = $textboxPassword.Text | ConvertTo-SecureString -AsPlainText -Force
    }
    
    New-ADUser @userprops
}
This topic is 4 years and 9 months old and has exceeded the time allowed for comments. Please begin a new topic or use the search feature to find a similar but newer topic.
Locked