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.
Locked
User avatar
SCSBIT
Posts: 1
Joined: Fri Jan 26, 2018 10:57 am

New-ADUser form

Post by SCSBIT » Thu Jun 20, 2019 7:42 am

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
Joined: Thu Aug 18, 2011 4:56 am

Re: New-ADUser form

Post by davidc » Thu Jun 20, 2019 7:52 am

[TOPIC MOVED TO POWERSHELL GUIS FORUM BY MODERATOR]
David
SAPIEN Technologies, Inc.

User avatar
jvierra
Posts: 13682
Joined: Tue May 22, 2007 9:57 am
Contact:

Re: New-ADUser form

Post by jvierra » Thu Jun 20, 2019 8:33 am

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
}

Locked