Solved

Powershell Script Help - creating new AD user

Posted on 2014-11-03
13
490 Views
Last Modified: 2014-11-05
Here's what Ive got so far:

$theOU = read-host "enter OU name"
$txtBoxDisplayName = ($txtBoxSurname , $txtBoxGivenName)
$txtBoxDescription = read-host "Enter descr"
$txtBoxSurname = read-Host "enter surname"
$txtBoxGivenName = $txtBoxSurname + $txtBoxGivenName
$txtBoxTelephone = read-host "Enter phone #"
$txtBoxPassword = "P@$$WORD1"
$name = "$givenname $surname"
$proxyaddress = read-host "Enter the proxy address"

Import-Module activedirectory
import-module servermanager

	
		New-ADUser -path $myOU -DisplayName $txtBoxDisplayName -Description $txtBoxDescription -Surname $txtBoxSurname -givenname $txtboxgivenname -OfficePhone $txtBoxTelephone -AccountPassword (ConvertTo-SecureString $txtBoxPassword -AsPlainText -force) -enabled:$true

Open in new window


The given and display name variables are not working.  I'm testing different syntax's.. both of which were wrong.  I'd like to be prompted for the first and last names.  Then have it auto form the display name of "last , first" then the CN of "last , first" and the SAMAccountName of "first letter+last name"

Eventually I'm going to try to get it to also set an smtp: proxy address, postal codes, City, State, title, extentionattribute1 and employeeID.

Right now though I cannot get the new-aduser -name to work correctly.  I've tried quotes, (single and double), and parenthesis to no avail.
0
Comment
Question by:Ben Hart
  • 9
  • 4
13 Comments
 
LVL 39

Expert Comment

by:footech
ID: 40420456
The issue I see is the order of the variables.  You're using some variables before they're defined.  Also, it seems like you would want to redefine $txtBoxGivenName as it doesn't make much sense above.
$txtBoxSurname = read-Host "enter surname"
$txtBoxGivenName = read-Host "enter givenname"
$txtBoxDisplayName = "$txtBoxSurname, $txtBoxGivenName"

Open in new window


You have to supply New-ADUser with a SAMAccountName.  If you add that your New-ADUser command should work.
0
 
LVL 14

Author Comment

by:Ben Hart
ID: 40420491
All of our SAMAccountNames are first letter of first name, + last name.  i.e. Sam Crowley = SCrowley

Is there a syntax I can use to create that as a $ variable?
0
 
LVL 14

Author Comment

by:Ben Hart
ID: 40420507
If I can determine how to create that format of samaccountname I could probably then use it to create a set-aduser pipe for the proxyaddress.
0
Are your AD admin tools letting you down?

Managing Active Directory can get complicated.  Often, the native tools for managing AD are just not up to the task.  The largest Active Directory installations in the world have relied on one tool to manage their day-to-day administration tasks: Hyena. Start your trial today.

 
LVL 39

Accepted Solution

by:
footech earned 500 total points
ID: 40420647
Sure there is.
$sam = "$($txtBoxGivenName.substring(0,1))$txtBoxSurname"

Open in new window

0
 
LVL 14

Author Comment

by:Ben Hart
ID: 40421849
Awesome let me test this.
0
 
LVL 14

Author Comment

by:Ben Hart
ID: 40422982
Hmmm no likey.  Once I place the first parenthesis it breaks the line and complains about how

txtBoxGivenName.substring : The term 'txtBoxGivenName.substring' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included,
verify that the path is correct and try again.
At line:10 char:12
+ $name = "$(txtBoxGivenName.substring(0,1))$txtBoxSurname"
+            ~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (txtBoxGivenName.substring:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundExcept

If I remove that first set of () it says:

New-ADUser : The name provided is not a properly formed account name
At line:24 char:1
+ New-ADUser -path $myOU -name $name -DisplayName $txtBoxDisplayName -Surname $txt ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : NotSpecified: (CN=buck.substri...C=root01,DC=org:String) [New-ADUser], ADException
    + FullyQualifiedErrorId : ActiveDirectoryServer:1315,Microsoft.ActiveDirectory.Management.Commands.NewADUser

Here's the code as it sits right now:

$theOU = read-host "enter OU name"

#$txtBoxDescription = read-host "Enter descr"
$txtBoxSurname = read-Host "enter surname"

#$txtBoxTelephone = read-host "Enter phone #"
$txtBoxGivenName = read-host "Enter first name"
$txtBoxDisplayName = "$txtBoxSurname , $txtBoxGivenName"
$txtBoxPassword = "P@$$WORD1"
$name = "$txtBoxGivenName.substring(0,1)$txtBoxSurname"
#$proxyaddress = read-host "Enter the proxy address"

Import-Module activedirectory
import-module servermanager

	#Modify SearchBase to match your AD structure
	#$myOU =  Invoke-Expression "(Get-ADOrganizationalUnit -LDAPfilter '(Name=$TheOU)' -SearchBase 'OU=people,DC=DIFC,DC=root01,DC=org').DistinguishedName"
	#$myOU =  Invoke-Expression "(Get-ADOrganizationalUnit -LDAPfilter $comboboxOU.text -SearchBase 'OU=people,DC=DIFC,DC=root01,DC=org').DistinguishedName"
	$myOU = Get-AdOrganizationalUnit -Filter "Name -eq '$theOU'" -Searchbase 'OU=People,DC=DIFC,DC=Root01,DC=org'
	
	#if ($checkboxEnabled.Checked -eq $true) {
	write-host	$myou
write-host $name
New-ADUser -path $myOU -name $name -DisplayName $txtBoxDisplayName -Surname $txtBoxSurname -givenname $txtboxgivenname -AccountPassword (ConvertTo-SecureString $txtBoxPassword -AsPlainText -force) -enabled:$true
	

Open in new window

0
 
LVL 14

Author Comment

by:Ben Hart
ID: 40423015
If I type into a console new-aduser -name "bcherry" it's accepted and the user object created.

However in my code it says the name provided is not properly formed which $givenName.substring(0,1)+$surname should result in bcherry.
0
 
LVL 14

Author Comment

by:Ben Hart
ID: 40423025
Somethings wrong here I think:

PS C:\Windows\system32> $fname = "buck"

PS C:\Windows\system32> $lname = "cherry"

PS C:\Windows\system32> $displayname = "$fname.substring(0,1)+$lname"

PS C:\Windows\system32> $displayname
buck.substring(0,1)+cherry


Everything that I've read indicated that the .substring() is exactly how you pick which and how many characters to display.
0
 
LVL 14

Author Comment

by:Ben Hart
ID: 40423031
My bad.. it was the quotation marks.
0
 
LVL 39

Expert Comment

by:footech
ID: 40423081
Check what I posted.  In this command
$name = "$(txtBoxGivenName.substring(0,1))$txtBoxSurname"
you're missing a $.
0
 
LVL 14

Author Comment

by:Ben Hart
ID: 40423790
$name = "$(txtBoxGivenName.substring(0,1))$txtBoxSurname"

Nah brah I've got the $'s in there.  What ended up working for me was removing the quotation marks.
0
 
LVL 39

Expert Comment

by:footech
ID: 40424332
No, you don't.
"$(txtBoxGivenName.substring(0,1))$txtBoxSurname"
"$($txtBoxGivenName.substring(0,1))$txtBoxSurname"
See the difference?

It's good that you found the right syntax for contatenation.  I just want to point out the right syntax for a subexpression.
0
 
LVL 14

Author Comment

by:Ben Hart
ID: 40424980
Egad! Stupid symbols lol.

Sorry Foo I didnt scroll up enough it seems.
0

Featured Post

Netscaler Common Configuration How To guides

If you use NetScaler you will want to see these guides. The NetScaler How To Guides show administrators how to get NetScaler up and configured by providing instructions for common scenarios and some not so common ones.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

Create and license users in Office 365 in bulk based on a CSV file. A step-by-step guide with PowerShell script examples.
This script can help you clean up your user profile database by comparing profiles to Active Directory users in a particular OU, and removing the profiles that don't match.
This Micro Tutorial hows how you can integrate  Mac OSX to a Windows Active Directory Domain. Apple has made it easy to allow users to bind their macs to a windows domain with relative ease. The following video show how to bind OSX Mavericks to …
Windows 10 is mostly good. However the one thing that annoys me is how many clicks you have to do to dial a VPN connection. You have to go to settings from the start menu, (2 clicks), Network and Internet (1 click), Click VPN (another click) then fi…

813 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

13 Experts available now in Live!

Get 1:1 Help Now