Reputation: 79
I've written a function that will create an Active Directory user based on the supplied parameters. The user creates fine; however the problem that I'm running into is the returned data from the function. I'm merely looking to return a boolean response based on if the user was created or not. Instead, it's passing back an array.
A sample of the function:
Function CreateUser () {
param([string]$ParentDN,
[string]$FirstName,
[string]$LastName,
[string]$Username,
[string]$EmailAddress,
[string]$Password);
Try {
$UserOU = [ADSI] "LDAP://$LDAPServer/$ParentDN";
$NewUser = $UserOU.Create("user","cn=$Username");
$NewUser.Put("sAMAccountName","$Username");
$NewUser.Put("givenName","$FirstName");
$NewUser.Put("sn","$LastName");
$NewUser.Put("UserPrincipalName","$Username@$NetworkFQDN");
$NewUser.Put("mail","$EmailAddress");
$NewUser.SetInfo();
$NewUser.SetPassword("$Password");
$NewUser.SetInfo();
$flag = $NewUser.userAccountControl.Value -bxor 65536; #Password Never Expires flag
$NewUser.userAccountControl = $flag;
$NewUser.InvokeSet("AccountDisabled","False") #Enables Account
$NewUser.SetInfo();
return $true;
}
Catch {
return $false;
}
}
And I'm calling it using the following syntax:
$CreateUserResults = CreateUser -FirstName $User_FirstName -LastName $User_LastName -EmailAddress $User_EmailAddress -ParentDN $User_ParentOU -Password $User_Password -Username $User_SamAccountName
Any advise or direction would be appreciated.
Thanks, Ryan
Upvotes: 1
Views: 165
Reputation: 68331
I'm not where I can test, but I suspect those setinfo()
methods are returning data that needs to be redirected to $null
to prevent it from being returned by the function.
Upvotes: 2