Solved

Help with PowerShell Script to import users into Active Directory

Posted on 2014-09-22
5
399 Views
Last Modified: 2014-09-23
Hello,

I have this script below where it tries to take what is in a CSV file and replace an custom attributed named HireDate with a date for each user in AD.  Below is the script, but I get an error.  Can you help with the error?

Import-Module ActiveDirectory            
$users = import-csv -path d:\scripts\hiredatetest.csv
foreach($user in $users)
{    
$fielduser = $user.SamAccountName
$fielddate = $user.HireDate -as [datetime]
echo "$fielduser,$fielddate"
Get-ADUser -Filter "SamAccountName -eq '$($user.SamAccountName)'" -Properties * -SearchBase "DC=domain,DC=com" |
Set-ADUser -Replace @{HireDate = $fielddate}  
}

Here is the CSV:

SAMAccountName,HireDate
bjones,3/17/2000


Here is the error:

Set-ADUser : Cannot bind parameter 'Replace' to the target. Exception setting "Replace": "Object reference not set to
an instance of an object."
At line:7 char:21
+ Set-ADUser -Replace @{HireDate = $fielddate}
+                     ~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : WriteError: (:) [Set-ADUser], ParameterBindingException
    + FullyQualifiedErrorId : ParameterBindingFailed,Microsoft.ActiveDirectory.Management.Commands.SetADUser
0
Comment
Question by:PBIT
  • 2
  • 2
5 Comments
 
LVL 27

Assisted Solution

by:Dan McFadden
Dan McFadden earned 500 total points
ID: 40338786
I think the issue is in the fact that you are piping all properties to the Set-ADUser command.  Also, since you have the SamAccountName already why do a Get-ADUser?

To use Set-ADUser to replace a value in a custom attribute field, try this:

Set-ADUser $fielduser  -Replace @{HireDate = $fielddate}

Set-ADUser command reference:  http://technet.microsoft.com/en-us/library/ee617215.aspx

Dan
0
 
LVL 80

Expert Comment

by:David Johnson, CD, MVP
ID: 40339296
hiredate is not normally in the schema you have to add it.
0
 

Author Comment

by:PBIT
ID: 40339524
OK, I have changed my script to the following, but get an error.  Thanks for you help in getting this going.

Import-Module ActiveDirectory            
$users = import-csv -path d:\scripts\hiredatetest.csv
foreach($user in $users)
{    
$fielduser = $user.SamAccountName
$fielddate = $user.HireDate -as [datetime]
echo "$fielduser,$fielddate"
Get-ADUser -Filter "SamAccountName -eq '$($user.SamAccountName)'" -Properties * -SearchBase "DC=domain, DC=com" |
Set-ADUser $fielduser  -Replace @{HireDate = $fielddate}
}

Get a different error now:

Set-ADUser : The input object cannot be bound to any parameters for the command either because the command does not
take pipeline input or the input and its properties do not match any of the parameters that take pipeline input.
At line:7 char:1
+ Set-ADUser $fielduser  -Replace @{HireDate = $fielddate}
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (CN=jones\, bon...,DC=pbtf,DC=com:PSObject) [Set-ADUser], ParameterBin   dingException
    + FullyQualifiedErrorId : InputObjectNotBound,Microsoft.ActiveDirectory.Management.Commands.SetADUser
0
 
LVL 27

Accepted Solution

by:
Dan McFadden earned 500 total points
ID: 40339552
Remove the Get-ADUser command and only use the Set-ADUser command as stated above.
0
 

Author Comment

by:PBIT
ID: 40339605
Got it.  Thanks! That worked!
0

Featured Post

Free Tool: Postgres Monitoring System

A PHP and Perl based system to collect and display usage statistics from PostgreSQL databases.

One of a set of tools we are providing to everyone as a way of saying thank you for being a part of the community.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Suggested Solutions

While rebooting windows server 2003 server , it's showing "active directory rebuilding indices please wait" at startup. It took a little while for this process to complete and once we logged on not all the services were started so another reboot is …
In-place Upgrading Dirsync to Azure AD Connect
This tutorial will walk an individual through the process of transferring the five major, necessary Active Directory Roles, commonly referred to as the FSMO roles to another domain controller. Log onto the new domain controller with a user account t…
This video shows how to use Hyena, from SystemTools Software, to bulk import 100 user accounts from an external text file. View in 1080p for best video quality.

856 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question