AD user creation powershell script

Hi

Been trying to write a powershell script to import new users into AD from a csv file. The code partially works in that it does create the new user in the correct ou, however it fails to enter any of the other data contained in the csv, like office and password. I also receive this error when running the script "Cannot index into a null array."

Any help on where im going wrong.


Add-PSSnapin Quest.ActiveRoles.ADManagement  

Function ProvisionInputCSV {
  Param ([string]$filename)
  $users = Import-CSV $filename
  foreach ($user in $users) {
    $userinfo = @{'givenName'=$user."First Name";
            'sn'= $user."Last Name";
            'title'= $user."Job Title";
            'department'= $user.Department;
            'displayName'= $user."First Name" + " " + $user."Last Name";
            'city'= $user.City;
            'password'= $user.Password;
            'samAccountName'= $user."Logon Name"
	'OU'= "OU=" + $user.department + ",OU=Departments,OU=Ashridge Users,DC=ashridge,DC=local";
           }
    Write-Output $userinfo
  }
}
	function CreateUser {
	param ($userinfo)
	New-QADUser –parentContainer $userinfo['OU'] `
		–samAccountName $userinfo['samAccountName'] `
		–FirstName $userinfo['givenName'] `
			–LastName $userinfo['sn'] `
–Name ($userinfo['givenName'] + ' ' + $userinfo['sn']) `				–displayName ($userinfo['givenName'] + ' ' + $userinfo['sn'])`
			–userPassword $userinfo['password'] `
			-WhatIf   | Enable-QADUser	
  }
  
   
Function Provision {
  PROCESS {
    CreateUser $_
    #CreateHomeFolder $_
    #AddToGroups $_
    #UpdateAttributes $_
  }
}   
                   
ProvisionInputCSV c:\test.csv | Provision

Open in new window

LVL 1
AshridgeTechServicesAsked:
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.

Chris DentPowerShell DeveloperCommented:
Hi there,

It'd take out the hash you're creating ($userinfo), it's a bit of an unnecessary step if all you're doing is reading the values back out of it.

Does this work?

Chris

Import-CSV "c:\test.csv" | %{
  $OU = "OU=$($_.Department),OU=Departments,OU=Ashridge Users,DC=ashridge,DC=local"
  $Name = "$($_.'First Name') $($_.'Last Name')"

  $User = New-QADUser -ParentContainer $OU -Name $Name -DisplayName $Name -SamAccountName $_."Logon Name" `
    -FirstName $_."First Name" -LastName $_."Last Name" -UserPassword $_.Password
  $User | Enable-QADUser
}

Open in new window

0
AshridgeTechServicesAuthor Commented:
That doesnt work at all, the user is created but no information at all is populated, login name is just random rubbish.

Thanks for the suggestion though
0
Chris DentPowerShell DeveloperCommented:

Can you show me a sample of your input file? Fabricated data is fine.

Chris
0
Simplify Active Directory Administration

Administration of Active Directory does not have to be hard.  Too often what should be a simple task is made more difficult than it needs to be.The solution?  Hyena from SystemTools Software.  With ease-of-use as well as powerful importing and bulk updating capabilities.

AshridgeTechServicesAuthor Commented:
here you go chris
test.csv
0
Chris DentPowerShell DeveloperCommented:

Well that's all perfectly normal.

Can you try this version? It fetches some of the values for New-QADUser as sub-expressions, just to see if it's doing strange things with the value types.

Out of interest, which version of PowerShell are you running? I do all testing under 2 which can occasionally lead to strange results.

Chris
Import-CSV "c:\test.csv" | %{
  $OU = "OU=$($_.Department),OU=Departments,OU=Ashridge Users,DC=ashridge,DC=local"
  $Name = "$($_.'First Name') $($_.'Last Name')"

  $User = New-QADUser -ParentContainer $OU -Name $Name -DisplayName $Name -SamAccountName $($_."Logon Name") `
    -FirstName $($_."First Name") -LastName $($_."Last Name") -UserPassword $($_.Password)
  $User | Enable-QADUser
}

Open in new window

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
AshridgeTechServicesAuthor Commented:
That one works, and yes im using 2 also.

One last question, how would i got about adding the home drive and profile creation into this script, as thats why the original was formatted in that way. as i had found on the net that was supposedly the best way. Any thoughts?
0
Chris DentPowerShell DeveloperCommented:
They're just folders aren't they?

I'd guess you want to create the folders and set permissions on them?

Chris
0
AshridgeTechServicesAuthor Commented:
yea, that was the idea. Id also like to be able to auto add them to the required groups.
0
Chris DentPowerShell DeveloperCommented:

Cool, that's no bother.

Chris
Function New-UserFolder($FolderPath, $Username)
{
  If (!(Test-Path $FolderPath))
  {
    # Create the folder, ignore the object it returns
    [Void](New-Item $FolderPath -Type Directory)

    # Create an access rule granting the user Modify rights on the created folder
    $AccessRule = New-Object Security.AccessControl.FileSystemAccessRule( `
      "$($Env:UserDomain)\$Username",
      @("Modify", "Synchronize"),
      @("ObjectInherit", "ContainerInherit"),
      "None",
      "Allow")

    # Get the current access control list
    $ACL = Get-ACL $FolderPath
    # Add the new entry
    $ACL.AddAccessRule($AccessRule)
    # Apply the ACL
    Set-ACL $FolderPath -AclObject $ACL
  }
}

Import-CSV "c:\test.csv" | %{
  $OU = "OU=$($_.Department),OU=Departments,OU=Ashridge Users,DC=ashridge,DC=local"
  $Name = "$($_.'First Name') $($_.'Last Name')"
  $Username = $_."Logon Name"

  $User = New-QADUser -ParentContainer $OU -Name $Name -DisplayName $Name -SamAccountName $Username `
    -FirstName $($_."First Name") -LastName $($_."Last Name") -UserPassword $($_.Password)
  $User | Enable-QADUser

  # Create the Profile folder
  $FolderPath = "\\ProfileServer\Share\$Username"
  # Create the folder and set Modify access
  New-UserFolder $FolderPath $Username

  # Create the Home folder
  $FolderPath = "\\HomeServer\Share\$Username"
  # Create the folder and set Modify access
  New-UserFolder $FolderPath $Username

  # Add the user to some groups
  Add-QADGroupMember "Group 1" -Member $Username
  Add-QADGroupMamber "Group 2" -Member $Username
  # etc
}

Open in new window

0
AshridgeTechServicesAuthor Commented:
Nice one cheers
0
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.