[Webinar] Learn how to a build a cloud-first strategyRegister Now

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 96
  • Last Modified:

Powershell script update

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
AMATERASOU
Asked:
AMATERASOU
1 Solution
 
oBdACommented:
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
 
AMATERASOUAuthor Commented:
Thanks
0

Featured Post

A Cyber Security RX to Protect Your Organization

Join us on December 13th for a webinar to learn how medical providers can defend against malware with a cyber security "Rx" that supports a healthy technology adoption plan for every healthcare organization.

Tackle projects and never again get stuck behind a technical roadblock.
Join Now