Solved

Help with PowerShell Script to import users into Active Directory

Posted on 2014-09-22
5
391 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 26

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 78

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 26

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

Join & Write a Comment

Suggested Solutions

My last post dealt with using group policy preferences to set file associations, a very handy usage for a GPP. Today I am going to share another cool GPP trick, this may be a specific scenario but I run into these situations frequently in my activit…
Installing a printer using group policy preferences is not that hard let’s take a look at it. First lets open up your group policy console and edit the policy you want to add it to. I recommend creating a new policy for each printer makes it a l…
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 from a Windows Server 2008 domain controller to a Windows Server 2012 domain controlle…
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…

762 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

Need Help in Real-Time?

Connect with top rated Experts

17 Experts available now in Live!

Get 1:1 Help Now