Jump to content

Recommended Posts

Posted

Hello all,

 

I'm trying to create a Powershell script to automate bulk user creation as it becomes a bit of a pain with the new intake of students each year. I have found a script online and I have slightly modified it but it doesn't appear to be working in a test environment I have.

 

This is the script:

 

import-module activedirectory
import-module grouppolicy

$mydom = (get-addomain).distinguishedname

$ouName = "Sales"

$oudn = "OU=$ouName,$mydom"

$userCount = 10

$datetime = get-date -format G
$desc = "Test user created $datetime"

$OU = get-ADOrganizationalUnit -Filter { name -eq $ouName }
if($OU -eq $Null)
{New-ADOrganizationalUnit -Name $OUName -Path $mydom}
else
{write-host "The OU" $ou "already exists - this is good..."}

$i = 1
while ($i -le $userCount)
{
$uname = "User" + $i
$UDdname = "Test User" + $i
New-ADUser -Name $uname -SamAccountName $uname -DisplayName $UDdname -Path $oudn -Enabled $true -ChangePasswordAtLogon $true -description $desc -AccountPassword (ConvertTo-SecureString "Pa$$w0rd" -AsPlainText -force) -PassThru
$i = $i + 1
}

 

And I am getting this error when I attempt to run the script:

 

New-ADUser : Directory object not found
At C:\Users\James\Documents\test.ps1:26 char:11
+ New-ADUser <<<<  -Name $uname -SamAccountName $uname -DisplayName $UDdname -Path $oudn -Enabled $t
AtLogon $true -description $desc -AccountPassword (ConvertTo-SecureString "Pa$$w0rd" -AsPlainText -f
   + CategoryInfo          : ObjectNotFound: (CN=User10,OU=Sa...gramming,DC=com:String) [New-ADUser
  undException
   + FullyQualifiedErrorId : Directory object not found,Microsoft.ActiveDirectory.Management.Comman

 

Can anyone point in the direction as to why this isn't working?

 

Many thanks.

Posted

Is it actually creating the sales OU? Or any users?

 

Can't scroll error very well on phone but looks like it can't find the ou. But wanted to check if it's doing anything or just bombing out at start?

 

Steve

Posted

A part of the script is set to look for the OU I want to create users in and it comes back saying that it has been found.

 

$OU = get-ADOrganizationalUnit -Filter { name -eq $ouName }
if($OU -eq $Null)
{New-ADOrganizationalUnit -Name $OUName -Path $mydom}
else
{write-host "The OU" $ou "already exists - this is good..."}

 

It keeps getting stuck at this part of the script

 

New-ADUser -Name $uname -SamAccountName $uname -DisplayName $UDdname -Path $oudn -Enabled $true -ChangePasswordAtLogon $true -description $desc -AccountPassword (ConvertTo-SecureString "Pa$$w0rd" -AsPlainText -force) -PassThru

 

If I remove "-Name $uname" it will prompt me to enter User names, which I'm guessing that the script isn't designed to do so (bearing in mind this script originally came from a random blog with comments that it worked).

Posted

Have you considered just using an batch script it is a lot easier than power shell, the command is: net user {username} {password} /add

You can also add /fullname "firstname surname" if you want more details added.

Using excel it this is very fast to create, followed by a quick copy and paste to a batch file then run it on the server, use ad to move users to the right ou, selct all of them and change the home path and profile if needed.

 

eg. Joe blogs

net user jblogs Password1 /add /fullname "Joe Blogs"

 

If you want a copy of my excel sheet let me know.

Posted
Have you considered just using an batch script it is a lot easier than power shell, the command is: net user {username} {password} /add

You can also add /fullname "firstname surname" if you want more details added.

Using excel it this is very fast to create, followed by a quick copy and paste to a batch file then run it on the server, use ad to move users to the right ou, selct all of them and change the home path and profile if needed.

 

eg. Joe blogs

net user jblogs Password1 /add /fullname "Joe Blogs"

 

If you want a copy of my excel sheet let me know.

 

If you could upload your Excel spreadsheet so I can take a gander, that would be brilliant.

 

Thanks :)

Posted
It looks like the script is complaining that the user object doesn't exist. I use VB for this stuff and you have to create the user before you can set the password or it'll give the exact same error. Try setting the password on a separate line after the user object has been created.

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now



×
×
  • Create New...