Solved

Powershell Script Help - creating new AD user

Posted on 2014-11-03
13
473 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
 
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
Is Your AD Toolbox Looking More Like a Toybox?

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 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

Optimizing Cloud Backup for Low Bandwidth

With cloud storage prices going down a growing number of SMBs start to use it for backup storage. Unfortunately, business data volume rarely fits the average Internet speed. This article provides an overview of main Internet speed challenges and reveals backup best practices.

Question has a verified solution.

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

Suggested Solutions

In this previous article (https://oddytee.wordpress.com/2016/05/05/provision-new-office-365-user-and-mailbox-from-exchange-hybrid-via-powershell/), we made basic license assignments to users in O365. When I say basic, the method is the simplest way …
This article explains how to prepare an HTML email signature template file containing dynamic placeholders for users' Azure AD data. Furthermore, it explains how to use this file to remotely set up a department-wide email signature policy in Office …
This video shows how to remove a single email address from the Outlook 2010 Auto Suggestion memory. NOTE: For Outlook 2016 and 2013 perform the exact same steps. Open a new email: Click the New email button in Outlook. Start typing the address: …
Delivering innovative fully-managed cloud services for mission-critical applications requires expertise in multiple areas plus vision and commitment. Meet a few of the people behind the quality services of Concerto.

937 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

5 Experts available now in Live!

Get 1:1 Help Now