Solved

Powershell Script Help - creating new AD user

Posted on 2014-11-03
13
526 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 9
  • 4
13 Comments
 
LVL 40

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
Free eBook: Backup on AWS

Everything you need to know about backup and disaster recovery with AWS, for FREE!

 
LVL 40

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 40

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 40

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

Free Tool: ZipGrep

ZipGrep is a utility that can list and search zip (.war, .ear, .jar, etc) archives for text patterns, without the need to extract the archive's contents.

One of a set of tools we're offering as a way to say thank you for being a part of the community.

Question has a verified solution.

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

I thought I'd write this up for anyone who has a request to create an anonymous whistle-blower-type submission form created using SharePoint 2010 (this would probably work the same for 2013). It's not 100% fool-proof but it's as close as you can get…
Synchronize a new Active Directory domain with an existing Office 365 tenant
I've attached the XLSM Excel spreadsheet I used in the video and also text files containing the macros used below. https://filedb.experts-exchange.com/incoming/2017/03_w12/1151775/Permutations.txt https://filedb.experts-exchange.com/incoming/201…
Exchange organizations may use the Journaling Agent of the Transport Service to archive messages going through Exchange. However, if the Transport Service is integrated with some email content management application (such as an antispam), the admini…

726 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