Link to home
Start Free TrialLog in
Avatar of PBIT
PBITFlag for United States of America

asked on

Help with PowerShell Script to import users into Active Directory

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
SOLUTION
Avatar of Dan McFadden
Dan McFadden
Flag of United States of America image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
hiredate is not normally in the schema you have to add it.
Avatar of PBIT

ASKER

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
ASKER CERTIFIED SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Avatar of PBIT

ASKER

Got it.  Thanks! That worked!