[Webinar] Streamline your web hosting managementRegister Today

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

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

0
AshridgeTechServices
Asked:
AshridgeTechServices
  • 5
  • 5
1 Solution
 
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
Free tool for managing users' photos in Office 365

Easily upload multiple users’ photos to Office 365. Manage them with an intuitive GUI and use handy built-in cropping and resizing options. Link photos with users based on Azure AD attributes. Free tool!

 
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
 
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

Featured Post

Easily manage email signatures in Office 365

Managing email signatures in Office 365 can be a challenging task if you don't have the right tool. CodeTwo Email Signatures for Office 365 will help you implement a unified email signature look, no matter what email client is used by users. Test it for free!

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