Solved

Powershell script update

Posted on 2016-11-07
2
70 Views
Last Modified: 2016-11-21
I had to udate the script below  and include the samaccount name which will be as follow :

"Username.it.us.sys"

And also include each created account in the folowing group :

"G_ID_NetworkTeam" which i don't know how to update in the below script.

Can you Help ?

Below is the Ps script.


Import-Module ActiveDirectory

$MYPASSWORD = Read-Host "Enter Password" -AsSecureString

[int] $counter = 8000

do {

     #SET VARIABLES
     $USERNAME = "TCA000"+ $counter.tostring()
     $USERPRINCIPALNAME = $USERNAME + "@it.us.sys"
     $SAMACCOUNTNAME = $USERNAME + "@it.us.sys"
     $DISPLAYNAME = "Network Engineer"
     $FIRSTNAME = "Network Team"
     $LASTNAME = "P2V"
     $OUPATH = "OU=Network,OU=OU_Engineer,DC=it,DC=us,DC=sys"
     $EMAILADDRESS = "p2v_tca@eafentreprise.us"

     #CREATE USERS
     New-ADUser -Name $USERNAME -UserPrincipalName $USERPRINCIPALNAME -sAMAccountName $SAMACCOUNTNAME -DisplayName $DISPLAYNAME -GivenName $FIRSTNAME -Surname $LASTNAME -Path $OUPATH -Email $EMAILADDRESS -AccountPassword $MYPASSWORD -PasswordNeverExpires $True -Enabled $False

     #LOG OF CREATED USERS
     Get-ADUser -Identity $USERNAME -Properties EmailAddress,DisplayName,PasswordNeverExpires,Enabled | Select-Object Name,GivenName,Surname,DisplayName,EmailAddress,DistinguishedName,SamAccountName,UserPrincipalName,PasswordNeverExpires,Enabled | Out-file "NewUserResults.txt" -Append

     $counter++

} while ($counter -le 9999)



Thank You
0
Comment
Question by:AMATERASOU
[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
2 Comments
 
LVL 85

Accepted Solution

by:
oBdA earned 500 total points
ID: 41877312
You can not use a samAccountName with a '@' as found in your script (OK, technically, with New-ADUser (the GUI will not allow this), you could - but it's pointless, because the user won't be able to logon with it).
Note, too, that the samAccountName is limited to 20 characters, and ".it.us.sys" takes away half of that, so with your current names, you're at the limit.

Import-Module ActiveDirectory

$MYPASSWORD = Read-Host "Enter Password" -AsSecureString

[int] $counter = 8000

$GroupMembers = @()
Do {

	#SET VARIABLES
	$USERNAME = "TCA000" + $counter.tostring()
	$USERPRINCIPALNAME = $USERNAME + ".it.us.sys"
	$SAMACCOUNTNAME = $USERNAME + "@it.us.sys"
	$DISPLAYNAME = "Network Engineer"
	$FIRSTNAME = "Network Team"
	$LASTNAME = "P2V"
	$OUPATH = "OU=Network,OU=OU_Engineer,DC=it,DC=us,DC=sys"
	$EMAILADDRESS = "p2v_tca@eafentreprise.us"

	#CREATE USERS
	New-ADUser -Name $USERNAME -UserPrincipalName $USERPRINCIPALNAME -sAMAccountName $SAMACCOUNTNAME -DisplayName $DISPLAYNAME -GivenName $FIRSTNAME -Surname $LASTNAME -Path $OUPATH -Email $EMAILADDRESS -AccountPassword $MYPASSWORD -PasswordNeverExpires $True -Enabled $False

	#LOG OF CREATED USERS
	Get-ADUser -Identity $USERNAME -Properties EmailAddress,DisplayName,PasswordNeverExpires,Enabled | Select-Object Name,GivenName,Surname,DisplayName,EmailAddress,DistinguishedName,SamAccountName,UserPrincipalName,PasswordNeverExpires,Enabled | Out-file "NewUserResults.txt" -Append

	$GroupMembers += $SAMACCOUNTNAME
	$counter++

} While ($counter -le 9999)
Add-ADGroupMember -Identity "G_ID_NetworkTeam" -Members $GroupMembers

Open in new window

0
 

Author Closing Comment

by:AMATERASOU
ID: 41896521
Thanks
0

Featured Post

Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

Recently, Microsoft released a best-practice guide for securing Active Directory. It's a whopping 300+ pages long. Those of us tasked with securing our company’s databases and systems would, ideally, have time to devote to learning the ins and outs…
After seeing many questions for JRNL_WRAP_ERROR for replication failure, I thought it would be useful to write this article.
Are you ready to implement Active Directory best practices without reading 300+ pages? You're in luck. In this webinar hosted by Skyport Systems, you gain insight into Microsoft's latest comprehensive guide, with tips on the best and easiest way…
This video shows how to use Hyena, from SystemTools Software, to update 100 user accounts from an external text file. View in 1080p for best video quality.

688 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