PBIT
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.cs v
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,Mic rosoft.Act iveDirecto ry.Managem ent.Comman ds.SetADUs er
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.cs
foreach($user in $users)
{
$fielduser = $user.SamAccountName
$fielddate = $user.HireDate -as [datetime]
echo "$fielduser,$fielddate"
Get-ADUser -Filter "SamAccountName -eq '$($user.SamAccountName)'"
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,Mic
SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
hiredate is not normally in the schema you have to add it.
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.cs v
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:PSOb ject) [Set-ADUser], ParameterBin dingException
+ FullyQualifiedErrorId : InputObjectNotBound,Micros oft.Active Directory. Management .Commands. SetADUser
Import-Module ActiveDirectory
$users = import-csv -path d:\scripts\hiredatetest.cs
foreach($user in $users)
{
$fielduser = $user.SamAccountName
$fielddate = $user.HireDate -as [datetime]
echo "$fielduser,$fielddate"
Get-ADUser -Filter "SamAccountName -eq '$($user.SamAccountName)'"
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:PSOb
+ FullyQualifiedErrorId : InputObjectNotBound,Micros
ASKER CERTIFIED SOLUTION
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
ASKER
Got it. Thanks! That worked!