Possible poorly formatted excel csv or text file.

I have a bunch of users that I am trying to create. I tried running new-aduser against them and I am getting the commandlet new-aduser cannot be found when running this in a script.

However, when I run the command by itself and no script it works perfectly. The spreadsheet of data is unknown of where this came from. i believe the data is somehow invalid. Is there anyway I can get the data cleaned up to where it is readable to a powershell script.



import-csv file.csv | Foreach-Object{
    $uid = $_.UID
    $fname = $_.First
    $mi = $_.Initial
    $lname = $_.Last
    $dname = $_.Display
    $dpment = $_.Department
    $upn = $_.email
     $group = $_.group
    $dcpath = $_.Container

    New-ADUser -Name $dname -SamAccountName $uid -GivenName $fname -Initials $mi -surname $lname -DisplayName $dname -UserPrincipalName $upn -Path $dcpath -AccountPassword
}


I even rewrote the script and I am getting the same error message. I logged on to the Domain Controller and also ran this from my desktop. The only other conclusion I have came to is that this is the text and or csv file that I am running this against.

$Users = Import-Csv "file.csv"
foreach ($User in $Users)
{
    New-QADUser -Name $User.Name `
        -ParentContainer $User.container `
        -FirstName $User.FirstName `
      -Intials $User.Intials
        -LastName $User.LastName `
      -Displayname $User.Displayname
        -UserPassword $User.userPassword `
        -SamAccountName $User.UID `
      -Department $User.Department `
      -Email $User.Email `
      set-qaduser -identity $_.UID
      -UserMustChangePassword $true `
      -Service $DomainController `
}
techdriveAsked:
Who is Participating?
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

SubsunCommented:
I am getting the commandlet new-aduser cannot be found when running this in a script.
looks like the activedirectory module is not loaded.. Add following line as the first line your script and see if it works..

Import-Module activedirectory

Open in new window


If you are using New-QADUser then you need to add

Add-PSSnapin Quest.ActiveRoles.ADManagement as first line..
0
techdriveAuthor Commented:
Unfortunately I get the same error message after running both of these commands before the script on the first line.
0
SubsunCommented:
Can you post the script which you are running now and the screenshot of the error message?
0
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

Neil RussellTechnical Development LeadCommented:
Are you running this on the server?
0
techdriveAuthor Commented:
Script i am running

Add-PSSnapin Quest.ActiveRoles.ADManagement

Import-Csv "C:\temp\users.csv" | foreach-object {
$userprinc = $_."Logon Username"  + "@domain.com"
    New-QADUser -Name $_.Name  `
      -ParentContainer $_."container"  `
      -SamAccountName $_."Logon Username" `
      -userpassword "P@ssw0rd!"  `
      -FirstName $_.First `
        -Intials $_.Initials  `
        -LastName $_.LastName `
      -Displayname $_."name" `
        -UserPrincipalName $userPrinc `
      -Department $_.Department `
      -Email $_.Email ` ;
      set-qaduser -identity $_."Logon Username"  `
      -UserMustChangePassword $true `
      -Service $DomainController `
}


Here is the error message I receive.

New-QADUser : Cannot validate argument on parameter 'Name'. The argument is null or empty. Supply an argument that is n
ot null or empty and then try the command again.
At C:\temp\createusers13.ps1:5 char:22
+     New-QADUser -Name <<<<  $_.Name  `
    + CategoryInfo          : InvalidData: (:) [New-QADUser], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Quest.ActiveRoles.ArsPowerShellSnapIn.Powershell.Cmdlet
   s.NewUserCmdlet

Set-QADUser : Cannot validate argument on parameter 'Service'. The argument is null or empty. Supply an argument that i
s not null or empty and then try the command again.
At C:\temp\createusers13.ps1:18 char:10
+     -Service <<<<  $DomainController `
    + CategoryInfo          : InvalidData: (:) [Set-QADUser], ParameterBindingValidationException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Quest.ActiveRoles.ArsPowerShellSnapIn.Powershell.Cmdlet
   s.SetUserCmdlet
0
techdriveAuthor Commented:
I am running this one on my workstation.
0
SubsunCommented:
New-QADUser : Cannot validate argument on parameter 'Name'. The argument is null or empty.
This is a different error.. :-).. mostly related to your csv. can you open your csv file in notepad and post it here.. You may replace your company information with some bogus details..
0
techdriveAuthor Commented:
Logon Username      First      Initials      LastName      Displayname      Department      Email      Container
firstlogon      first      b      long      first logon      comp      firstlogon@some.com      some.com/OU/people
secondlogon      second      b      logon      second logon      comp      secondlogon@some.com      some.com/OU/people
thirdlogon      third      b      logon      third logon      comp      thirdlogon@some.com      some.com/OU/people
0
techdriveAuthor Commented:
it looks neat in the sheet but when I paste it is ugly.
0
SubsunCommented:
I think the csv format is not correct, also you didn't define $DomainController in script..

I have tried the following code and its working for me..
Replace ServerA with your domain controller.
Add-PSSnapin Quest.ActiveRoles.ADManagement
$DomainController = "ServerA"
Import-Csv "User1.csv" | % {
$userprinc = $_."Logon Username"  + "@domain.com"
    New-QADUser -Name $_.Name `
	-ParentContainer $_.container `
	-SamAccountName $_."Logon Username" `
	-userpassword "P@ssw0rd!" `
	-FirstName $_.First `
	-Initials $_.Initials `
	-LastName $_.LastName `
	-Displayname $_.name `
	-UserPrincipalName $userPrinc `
	-Department $_.Department `
	-Email $_.Email
	Set-qaduser -identity $_."Logon Username" `
	-UserMustChangePassword $true `
	-Service $DomainController
}

Open in new window

Attached sample CSV Format..
Input.csv
0
techdriveAuthor Commented:
First of all many thanks I got the script to run now. There is one more thing I need help with. If I run the scrip and remove line 16 and 17 it works. If I leave the following in I get the following error.

Set-qaduser -identity $_."Logon Username" `
      -UserMustChangePassword $true `





New-QADUser : A positional parameter cannot be found that accepts argument ' '.
At C:\temp\creator1.ps1:5 char:16
+     New-QADUser <<<<  -Name $_.Name `
    + CategoryInfo          : InvalidArgument: (:) [New-QADUser], ParameterBindingException
    + FullyQualifiedErrorId : PositionalParameterNotFound,Quest.ActiveRoles.ArsPowerShellSnapIn.Powershell.Cmdlets.New
   UserCmdlet

Set-QADUser : Cannot bind parameter 'Identity'. Cannot convert the "@{Name=; container= value of type
"System.Management.Automation.PSCustomObject" to type "Quest.ActiveRoles.ArsPowerShellSnapIn.Data.IdentityParameter".
At C:\temp\creator1.ps1:14 char:23
+     set-qaduser -identity <<<<  $_. "Logon Username" `
    + CategoryInfo          : InvalidArgument: (:) [Set-QADUser], ParameterBindingException
    + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Quest.ActiveRoles.ArsPowerShellSnapIn.Powershell.Cmdlets.
   SetUserCmdlet
0
SubsunCommented:
I am not clear.. The code which I posted should work if you have the csv file in the format as I mentioned.

Did you modify the code? Also Did you check if your CSV is in correct format?

BTB, if you want to remove Set-qaduser then the code should look like the following..
Add-PSSnapin Quest.ActiveRoles.ADManagement
Import-Csv "User1.csv" | % {
$userprinc = $_."Logon Username"  + "@domain.com"
    New-QADUser -Name $_.Name `
	-ParentContainer $_.container `
	-SamAccountName $_."Logon Username" `
	-userpassword "P@ssw0rd!" `
	-FirstName $_.First `
	-Initials $_.Initials `
	-LastName $_.LastName `
	-Displayname $_.name `
	-UserPrincipalName $userPrinc `
	-Department $_.Department `
	-Email $_.Email
}

Open in new window

0
techdriveAuthor Commented:
I reformatted the input file and the error above appeared even with the code you left. In a desperate attempt to get this completed I just removed lines 16 and 17. When I did it worked. I just incorporated the command get-qaduser -searchroot "pathofou" | set-qaduser  -UserMustChangePassword $true after I ran the script and it worked. However, I was really trying to get this to run together in the script.
0
SubsunCommented:
Can you post your input file?
0
techdriveAuthor Commented:
Sorry Subsun i found where I was in error. Your script works fine and as always many thanks. Whenever I see your name answering the question I post it always gets resolved, thanks again for your help sir.
0
SubsunCommented:
Glad you are able to fix the error. If you don’t need any further assistance then please don’t forget to close this thread.. :-)
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Powershell

From novice to tech pro — start learning today.