Solved

Powershell Script Help - creating new AD user

Posted on 2014-11-03
13
461 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
Better Security Awareness With Threat Intelligence

See how one of the leading financial services organizations uses Recorded Future as part of a holistic threat intelligence program to promote security awareness and proactively and efficiently identify threats.

 
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

IT, Stop Being Called Into Every Meeting

Highfive is so simple that setting up every meeting room takes just minutes and every employee will be able to start or join a call from any room with ease. Never be called into a meeting just to get it started again. This is how video conferencing should work!

Join & Write a Comment

Hi all.   The other day I had to change the passwords for a bunch of users on the fly. Because they were so many, I decided to do it in an automated way and I would like to share it with you all.   If you are not doing it directly in a Domain Co…
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…
This video gives you a great overview about bandwidth monitoring with SNMP and WMI with our network monitoring solution PRTG Network Monitor (https://www.paessler.com/prtg). If you're looking for how to monitor bandwidth using netflow or packet s…
This video demonstrates how to create an example email signature rule for a department in a company using CodeTwo Exchange Rules. The signature will be inserted beneath users' latest emails in conversations and will be displayed in users' Sent Items…

746 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

9 Experts available now in Live!

Get 1:1 Help Now