Having trouble importing users to AD from a csv. The csv contains: firstname, lastname, password, ou<\/p>\n
We will say the record is: test, user1, password, All Users<\/p>\n
Here’s the script (redacted/changed/ya know):<\/p>\n
Import-Module ActiveDirectory \n$Users = Import-Csv -Delimiter \",\" -Path \"filepath.csv\" \nforeach ($User in $Users) \n{ \n $OU = $User.OU +\",DC=domain,DC=com\"\n\n $Password = $User.password \n $Detailedname = $User.firstname + \" \" + $User.lastname \n $UserFirstname = $User.Firstname\n $FirstLetterFirstname = $UserFirstname.substring(0,1) \n $SAM = $FirstLetterFirstname + $User.lastname \n\n New-ADUser -DisplayName $Detailedname -SamAccountName $SAM -UserPrincipalName $SAM -GivenName $user.firstname -Surname $user.lastname -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -Force) -Enabled $true -Path $OU \n}\n\n<\/code><\/pre>\nRemote pssession. Script starts to run and powershell just stops. No errors. Just stops until ctrl C. Nothing popping up in any of my OUs. What am I missing here?<\/p>","upvoteCount":5,"answerCount":14,"datePublished":"2015-07-23T17:34:19.000Z","author":{"@type":"Person","name":"zfaircloth","url":"https://community.spiceworks.com/u/zfaircloth"},"acceptedAnswer":{"@type":"Answer","text":"
In the file you posted it was tab delimited, in your code you put the delimiter as a comma.<\/p>","upvoteCount":4,"datePublished":"2015-07-23T18:38:51.000Z","url":"https://community.spiceworks.com/t/issue-with-importing-users-to-ad/421969/13","author":{"@type":"Person","name":"chrisseiter","url":"https://community.spiceworks.com/u/chrisseiter"}},"suggestedAnswer":[{"@type":"Answer","text":"
Having trouble importing users to AD from a csv. The csv contains: firstname, lastname, password, ou<\/p>\n
We will say the record is: test, user1, password, All Users<\/p>\n
Here’s the script (redacted/changed/ya know):<\/p>\n
Import-Module ActiveDirectory \n$Users = Import-Csv -Delimiter \",\" -Path \"filepath.csv\" \nforeach ($User in $Users) \n{ \n $OU = $User.OU +\",DC=domain,DC=com\"\n\n $Password = $User.password \n $Detailedname = $User.firstname + \" \" + $User.lastname \n $UserFirstname = $User.Firstname\n $FirstLetterFirstname = $UserFirstname.substring(0,1) \n $SAM = $FirstLetterFirstname + $User.lastname \n\n New-ADUser -DisplayName $Detailedname -SamAccountName $SAM -UserPrincipalName $SAM -GivenName $user.firstname -Surname $user.lastname -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -Force) -Enabled $true -Path $OU \n}\n\n<\/code><\/pre>\nRemote pssession. Script starts to run and powershell just stops. No errors. Just stops until ctrl C. Nothing popping up in any of my OUs. What am I missing here?<\/p>","upvoteCount":5,"datePublished":"2015-07-23T17:34:19.000Z","url":"https://community.spiceworks.com/t/issue-with-importing-users-to-ad/421969/1","author":{"@type":"Person","name":"zfaircloth","url":"https://community.spiceworks.com/u/zfaircloth"}},{"@type":"Answer","text":"
Not sure why that’s not working the code looks good, maybe try enclosing the path in “quotes” and I have seen in some scripts where the $true can cause an issue maybe switch that to a 1 it means the same thing…<\/p>","upvoteCount":2,"datePublished":"2015-07-23T17:43:29.000Z","url":"https://community.spiceworks.com/t/issue-with-importing-users-to-ad/421969/2","author":{"@type":"Person","name":"nicholasfic","url":"https://community.spiceworks.com/u/nicholasfic"}},{"@type":"Answer","text":"
Also sorry not to ask a dumb question but you are running this as an administrator and with an account that has rights to create users in the OU’s they’re going in? Have you also tried just running your command line without the variables with one user just as a test?<\/p>","upvoteCount":0,"datePublished":"2015-07-23T17:46:05.000Z","url":"https://community.spiceworks.com/t/issue-with-importing-users-to-ad/421969/3","author":{"@type":"Person","name":"nicholasfic","url":"https://community.spiceworks.com/u/nicholasfic"}},{"@type":"Answer","text":"\n\n
<\/div>\n
SCCM BiK:<\/div>\n
\nAlso sorry not to ask a dumb question but you are running this as an administrator and with an account that has rights to create users in the OU’s they’re going in? Have you also tried just running your command line without the variables with one user just as a test?<\/p>\n<\/blockquote>\n<\/aside>\n
Yes, as admin. And yes, ran the cmdlets alone and was able to create a new user in the designated OU. I’m not sure where you mean on quoting the path - the variable definition is in quotes. If you mean where I call it, the rest of the variables aren’t quoted so I’m not sure why that would be the issue. But I will try it and using 1 vs. $true.<\/p>","upvoteCount":0,"datePublished":"2015-07-23T17:48:44.000Z","url":"https://community.spiceworks.com/t/issue-with-importing-users-to-ad/421969/4","author":{"@type":"Person","name":"zfaircloth","url":"https://community.spiceworks.com/u/zfaircloth"}},{"@type":"Answer","text":"
\n\nIs $Users populated?<\/p>\n<\/li>\n
\nComment out the New-ADUser and return each of the variables in the ForEach loop. Are they populated?<\/p>\n<\/li>\n
\nConvert the password outside the New-ADUser with the rest of the variables, then uncomment the line and call for that variable in the command.<\/p>\n<\/li>\n<\/ul>","upvoteCount":0,"datePublished":"2015-07-23T17:51:03.000Z","url":"https://community.spiceworks.com/t/issue-with-importing-users-to-ad/421969/5","author":{"@type":"Person","name":"chrisseiter","url":"https://community.spiceworks.com/u/chrisseiter"}},{"@type":"Answer","text":"
One thing I noticed was with this line.<\/p>\n
$OU = $User.OU +\",DC=domain,DC=com\"\n<\/code><\/pre>\nUsing your sample data, this would yield an OU string of<\/p>\n
All Users,DC=domain,DC=com\n<\/code><\/pre>\nSwitching it to this should yield a proper string.<\/p>\n
$OU = \"OU=\" + $User.OU + \",DC=domain,DC=com\"\n<\/code><\/pre>\nOther than that, I don’t notice any problems with the script, though I can’t test it right now.<\/p>","upvoteCount":3,"datePublished":"2015-07-23T17:52:13.000Z","url":"https://community.spiceworks.com/t/issue-with-importing-users-to-ad/421969/6","author":{"@type":"Person","name":"gungnir","url":"https://community.spiceworks.com/u/gungnir"}},{"@type":"Answer","text":"\n\n
<\/div>\n
Gungnir:<\/div>\n
\nOne thing I noticed was with this line.<\/p>\n
$OU = $User.OU +\",DC=domain,DC=com\"\n<\/code><\/pre>\nUsing your sample data, this would yield an OU string of<\/p>\n
All Users,DC=domain,DC=com\n<\/code><\/pre>\nSwitching it to this should yield a proper string.<\/p>\n
$OU = \"OU=\" + $User.OU + \",DC=domain,DC=com\"\n<\/code><\/pre>\nOther than that, I don’t notice any problems with the script, though I can’t test it right now.<\/p>\n<\/blockquote>\n<\/aside>\n
Good catch. Same result unfortunately.<\/p>","upvoteCount":1,"datePublished":"2015-07-23T18:00:35.000Z","url":"https://community.spiceworks.com/t/issue-with-importing-users-to-ad/421969/7","author":{"@type":"Person","name":"zfaircloth","url":"https://community.spiceworks.com/u/zfaircloth"}},{"@type":"Answer","text":"
It’s got to be something during the import or I’m calling an incorrect variable. Tested cmdlets alone. Everything that is used in new-aduser is working correctly when done outside of the script. Changed this up for the example and attached.<\/p>\n
Edit: okay apparently attaching doesn’t work right now.<\/p>\n
A1 firstname B1 lastname C1 password D1 OU<\/p>\n
A2 Test B2 User C2 password D2 All users<\/p>\n
etc.<\/p>","upvoteCount":0,"datePublished":"2015-07-23T18:08:22.000Z","url":"https://community.spiceworks.com/t/issue-with-importing-users-to-ad/421969/8","author":{"@type":"Person","name":"zfaircloth","url":"https://community.spiceworks.com/u/zfaircloth"}},{"@type":"Answer","text":"
Spiceworks has a bug with CSV attachments that causes them to 404 when accessed. You’ll need to re-upload it as a TXT file if you want us to take a look at it.<\/p>","upvoteCount":1,"datePublished":"2015-07-23T18:10:27.000Z","url":"https://community.spiceworks.com/t/issue-with-importing-users-to-ad/421969/9","author":{"@type":"Person","name":"gungnir","url":"https://community.spiceworks.com/u/gungnir"}},{"@type":"Answer","text":"\n\n
<\/div>\n
Gungnir:<\/div>\n
\n\nSpiceworks has a bug with CSV attachments that causes them to 404 when accessed. You’ll need to re-upload it as a TXT file if you want us to take a look at it.<\/p>\n<\/blockquote>\n<\/blockquote>\n<\/aside>\n
Usersexample1_-_Copy.txt<\/a> (93 Bytes)<\/p>","upvoteCount":0,"datePublished":"2015-07-23T18:14:17.000Z","url":"https://community.spiceworks.com/t/issue-with-importing-users-to-ad/421969/10","author":{"@type":"Person","name":"zfaircloth","url":"https://community.spiceworks.com/u/zfaircloth"}},{"@type":"Answer","text":"If you run this<\/p>\n
Get-ADOrganizationalUnit -Filter 'Name -like \"*\"' | FT Name, DistinguishedName -A\n<\/code><\/pre>\ndoes the format of the “All Users” OU match the format of the $OU variable?<\/p>","upvoteCount":2,"datePublished":"2015-07-23T18:21:12.000Z","url":"https://community.spiceworks.com/t/issue-with-importing-users-to-ad/421969/11","author":{"@type":"Person","name":"chrisseiter","url":"https://community.spiceworks.com/u/chrisseiter"}},{"@type":"Answer","text":"\n\n
<\/div>\n
Chris Seiter (LBFF):<\/div>\n
\nIf you run this<\/p>\n
Get-ADOrganizationalUnit -Filter 'Name -like \"*\"' | FT Name, DistinguishedName -A\n<\/code><\/pre>\ndoes the format of the “All Users” OU match the format of the $OU variable?<\/p>\n<\/blockquote>\n<\/aside>\n
It does.<\/p>","upvoteCount":0,"datePublished":"2015-07-23T18:25:41.000Z","url":"https://community.spiceworks.com/t/issue-with-importing-users-to-ad/421969/12","author":{"@type":"Person","name":"zfaircloth","url":"https://community.spiceworks.com/u/zfaircloth"}},{"@type":"Answer","text":"
Thanks Chris. That takes care of the stopping issue. I started with a text and switched to a spreadsheet when I thought it would be easier to add and remove. Missed that.<\/p>\n
Of course fixing one issue brings up another and now I’m getting several errors of null-values. Think I will work on some other stuff and start from scratch when I’m fresh.<\/p>","upvoteCount":1,"datePublished":"2015-07-23T18:49:45.000Z","url":"https://community.spiceworks.com/t/issue-with-importing-users-to-ad/421969/14","author":{"@type":"Person","name":"zfaircloth","url":"https://community.spiceworks.com/u/zfaircloth"}}]}}
Having trouble importing users to AD from a csv. The csv contains: firstname, lastname, password, ou
We will say the record is: test, user1, password, All Users
Here’s the script (redacted/changed/ya know):
Import-Module ActiveDirectory
$Users = Import-Csv -Delimiter "," -Path "filepath.csv"
foreach ($User in $Users)
{
$OU = $User.OU +",DC=domain,DC=com"
$Password = $User.password
$Detailedname = $User.firstname + " " + $User.lastname
$UserFirstname = $User.Firstname
$FirstLetterFirstname = $UserFirstname.substring(0,1)
$SAM = $FirstLetterFirstname + $User.lastname
New-ADUser -DisplayName $Detailedname -SamAccountName $SAM -UserPrincipalName $SAM -GivenName $user.firstname -Surname $user.lastname -AccountPassword (ConvertTo-SecureString $Password -AsPlainText -Force) -Enabled $true -Path $OU
}
Remote pssession. Script starts to run and powershell just stops. No errors. Just stops until ctrl C. Nothing popping up in any of my OUs. What am I missing here?
5 Spice ups
Not sure why that’s not working the code looks good, maybe try enclosing the path in “quotes” and I have seen in some scripts where the $true can cause an issue maybe switch that to a 1 it means the same thing…
2 Spice ups
Also sorry not to ask a dumb question but you are running this as an administrator and with an account that has rights to create users in the OU’s they’re going in? Have you also tried just running your command line without the variables with one user just as a test?
Yes, as admin. And yes, ran the cmdlets alone and was able to create a new user in the designated OU. I’m not sure where you mean on quoting the path - the variable definition is in quotes. If you mean where I call it, the rest of the variables aren’t quoted so I’m not sure why that would be the issue. But I will try it and using 1 vs. $true.
chrisseiter
(Chris Seiter (LBFF))
July 23, 2015, 5:51pm
5
Is $Users populated?
Comment out the New-ADUser and return each of the variables in the ForEach loop. Are they populated?
Convert the password outside the New-ADUser with the rest of the variables, then uncomment the line and call for that variable in the command.
gungnir
(Gungnir)
July 23, 2015, 5:52pm
6
One thing I noticed was with this line.
$OU = $User.OU +",DC=domain,DC=com"
Using your sample data, this would yield an OU string of
All Users,DC=domain,DC=com
Switching it to this should yield a proper string.
$OU = "OU=" + $User.OU + ",DC=domain,DC=com"
Other than that, I don’t notice any problems with the script, though I can’t test it right now.
3 Spice ups
Gungnir:
One thing I noticed was with this line.
$OU = $User.OU +",DC=domain,DC=com"
Using your sample data, this would yield an OU string of
All Users,DC=domain,DC=com
Switching it to this should yield a proper string.
$OU = "OU=" + $User.OU + ",DC=domain,DC=com"
Other than that, I don’t notice any problems with the script, though I can’t test it right now.
Good catch. Same result unfortunately.
1 Spice up
It’s got to be something during the import or I’m calling an incorrect variable. Tested cmdlets alone. Everything that is used in new-aduser is working correctly when done outside of the script. Changed this up for the example and attached.
Edit: okay apparently attaching doesn’t work right now.
A1 firstname B1 lastname C1 password D1 OU
A2 Test B2 User C2 password D2 All users
etc.
gungnir
(Gungnir)
July 23, 2015, 6:10pm
9
Spiceworks has a bug with CSV attachments that causes them to 404 when accessed. You’ll need to re-upload it as a TXT file if you want us to take a look at it.
1 Spice up
Gungnir:
Spiceworks has a bug with CSV attachments that causes them to 404 when accessed. You’ll need to re-upload it as a TXT file if you want us to take a look at it.
Usersexample1_-_Copy.txt (93 Bytes)
chrisseiter
(Chris Seiter (LBFF))
July 23, 2015, 6:21pm
11
If you run this
Get-ADOrganizationalUnit -Filter 'Name -like "*"' | FT Name, DistinguishedName -A
does the format of the “All Users” OU match the format of the $OU variable?
2 Spice ups
Chris Seiter (LBFF):
If you run this
Get-ADOrganizationalUnit -Filter 'Name -like "*"' | FT Name, DistinguishedName -A
does the format of the “All Users” OU match the format of the $OU variable?
It does.
chrisseiter
(Chris Seiter (LBFF))
July 23, 2015, 6:38pm
13
In the file you posted it was tab delimited, in your code you put the delimiter as a comma.
4 Spice ups
Thanks Chris. That takes care of the stopping issue. I started with a text and switched to a spreadsheet when I thought it would be easier to add and remove. Missed that.
Of course fixing one issue brings up another and now I’m getting several errors of null-values. Think I will work on some other stuff and start from scratch when I’m fresh.
1 Spice up