Need help , updating data in AD from a Csv file

Hi, Need help , updating data in AD from a Csv file
Thank you.

Hi Denis, welcome to the forum :wave:

What sort of help do you need?

Please post the script you’re having problems with, together with any errors, and we’ll be happy to take a look at it for you and provide assistance.

When posting the script and errors, please make sure to use the </> button and paste the information where shown. This makes it easier for us to read, and to copy and paste your code. TIA.

Import-Module ActiveDirectory
$Users = Import-Csv -Path C:\Users\denbut\Desktop\darb.csv
$users = Get-ADUser -Filter * -SearchBase “LDAP://OU=_users/staff,DC=ism,DC=lt”
Get-Content -Path C:\Users\denbut\Desktop\test.csv | ft
foreach
($dataRecord in $datasource){
$cn=$dataRecord.FirstName + " " + $dataRecord.LastName
$sAMAccountname=$dataRecord.FirstName + “.” + $dataRecord.LastName
$givenName=$dataRecord.FirstName

$sn=$dataRecord.LastName

$sAMAccountName=$sAMAccountName.ToLower()

$displayName=$sn +", " + $givenName
$userPrincipalName=$sAMAccountName + “@ism.lt
$physicalDeliveryOfficeName=$dataRecord.office

$objuser.Put(“sAMAccountName,$sAMAccountName”)
$objuser.Put("userPrincipalName”,$userPrincipalName)
$objuser.Put(“displayname”, $displayName)
$objuser.Put(“givenNane”, $givenName)
$objuser.Put("sn”,$sn)
$objuser.Put("physicalDeliveryOfficeName”,$physicalDeliveryOfficeName)
$objuser.Put(“telephoneNumber”,$telephoneNumber)
$objuser.SetInfo()
}
problem
Get-ADUser : Directory object not found
At C:\Users\denis\Documents\Untitled11.ps1:3 char:10

  • $users = Get-ADUser -Filter * -SearchBase "LDAP://OU=_users/staff,DC= …
  •      ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : ObjectNotFound: (:slight_smile: [Get-ADUser], ADIdentityNotFoundException
    • FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADIdentityNotFoundException,Microsoft.ActiveDirectory.Management.Commands.GetADUser

First, as previously requested: please use the </> button to format the code in your post. You can edit your post, you do not need to create a new one to reformat it.

Your script not only contains errors but appears to be incomplete.

$Users is being set to the contents of the darb.csv, then over written with the results from Get-ADUser on the next line.

$datasource is not defined in the script you’ve posted.

$objuser is not defined in the script you’ve posted. But there’s no need to do it that way anyway when you have the Set-ADUser cmdlet.

The error you’re getting for the Get-ADUser command is due to the way you’ve defined the -SearchBase parameter. You should not include the LDAP:// part in the string, it’s just the DN of the OU.

Can these line be successful?

$objuser. Put("sAMAccountName,$sAMAccountName")
$objuser. Put("userPrincipalName", $userPrincipalName)
$objuser. Put("displayname", $displayName)
$objuser. Įdėti ("givenNane", $givenName)
$objuser. Put("sn", $sn)
$objuser. Put("physicalDeliveryOfficeName", $physicalDeliveryOfficeName)
$objuser. Įdėti ("phoneNumber", $telephoneNumber)
$objuser. SetInfo()

And I did’t really understand about the variable $datasource ?
Now the code looks like this …

$Users = Import-Csv -Path C:\Users\denbut\Desktop\darb.csv
Get-Content -Path C:\Users\denbut\Desktop\darb.csv 
$objIO=[ADSI]"LDAP://OU=ism.lt/_users/staff/staff,DC=ism,DC=lt"
Get-ADUser -Filter * -SearchBase "ou=_users/staff,dc=ism,dc=lt"

foreach($dataRecord in $datasource){
$cn=$dataRecord.FirstName + " " + $dataRecord.LastName
$sAMAccountname=$dataRecord.FirstName + “.” + $dataRecord.LastName
$givenName=$dataRecord.FirstName
$sn=$dataRecord.LastName
$sAMAccountName=$sAMAccountName.ToLower()
$displayName=$sn +", " + $givenName
$userPrincipalName=$sAMAccountName + “@ism.lt”
$objuser=$objOU.Create("user","CN="+$cn)
$physicalDeliveryOfficeName=$dataRecord.office

}

#$objuser.Put("sAMAccountName,$sAMAccountName")
#$objuser.Put("userPrincipalName”,$userPrincipalName)
#$objuser.Put(“displayname”, $displayName)
#$objuser.Put(“givenNane”, $givenName)
#$objuser.Put("sn”,$sn)
#$objuser.Put("physicalDeliveryOfficeName”,$physicalDeliveryOfficeName)
#$objuser.Put("telephoneNumber",$telephoneNumber)
#$objuser.SetInfo()

No, that won’t do what you want either.

You don’t need to use the Put() and SetInfo() methods.
Have a look at the help file for Set-ADUser it gives examples of how to use it.

Well you have a loop, that says foreach($dataRecord in $datasource) but there are no records in $datasource because you don’t assign it any value in your code. I would expect to see something like this in your script:

$datasource = Import-Csv C:\Temp\userdata.csv