Solved

Help with PowerShell Script to import users into Active Directory

Posted on 2014-09-22
5
409 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 28

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 82

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 28

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

Office 365 Training for Admins - 7 Day Trial

Learn how to provision tenants, synchronize on-premise Active Directory, implement Single Sign-On, customize Office deployment, and protect your organization with eDiscovery and DLP policies.  Only from Platform Scholar.

Question has a verified solution.

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

Always backup Domain, SYSVOL etc.using processes according to Microsoft Best Practices. This is meant as a disaster recovery process for small environments that did not implement backup processes and did not run a secondary domain controller that ne…
A hard and fast method for reducing Active Directory Administrators members.
This tutorial will walk an individual through the process of configuring their Windows Server 2012 domain controller to synchronize its time with a trusted, external resource. Use Google, Bing, or other preferred search engine to locate trusted NTP …
There are cases when e.g. an IT administrator wants to have full access and view into selected mailboxes on Exchange server, directly from his own email account in Outlook or Outlook Web Access. This proves useful when for example administrator want…

630 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