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
}