Solved

Powershell Script Help - creating new AD user

Posted on 2014-11-03
13
557 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
Does Powershell have you tied up in knots?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

 
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

Industry Leaders: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

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

Utilizing an array to gracefully append to a list of EmailAddresses
Auditing domain password hashes is a commonly overlooked but critical requirement to ensuring secure passwords practices are followed. Methods exist to extract hashes directly for a live domain however this article describes a process to extract u…
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…
Monitoring a network: how to monitor network services and why? Michael Kulchisky, MCSE, MCSA, MCP, VTSP, VSP, CCSP outlines the philosophy behind service monitoring and why a handshake validation is critical in network monitoring. Software utilized …

724 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