Solved

Help with PowerShell Script to import users into Active Directory

Posted on 2014-09-22
5
402 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
  • 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

Problems using Powershell and Active Directory?

Managing Active Directory does not always have to be complicated.  If you are spending more time trying instead of doing, then it's time to look at something else. For nearly 20 years, AD admins around the world have used one tool for day-to-day AD management: Hyena. Discover why

Question has a verified solution.

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

In this article, we will see the basic design consideration while designing a Multi-tenant web application in a simple manner. Though, many frameworks are available in the market to develop a multi - tenant application, but do they provide data, cod…
In this article, I am going to show you how to simulate a multi-site Lab environment on a single Hyper-V host. I use this method successfully in my own lab to simulate three fully routed global AD Sites on a Windows 10 Hyper-V host.
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…
Microsoft Active Directory, the widely used IT infrastructure, is known for its high risk of credential theft. The best way to test your Active Directory’s vulnerabilities to pass-the-ticket, pass-the-hash, privilege escalation, and malware attacks …

735 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