Share via

script problem

Momo 21 Reputation points
2022-06-02T18:39:40.257+00:00

Hello,

I'm trying to update AD users from a CSV file, it doesn't work.

Error message : Set-ADUser: Could not find object with identity 'toto' under: 'DC=xxx,DC=local'.

my script :
Import-Module ActiveDirectory

$update = Import-Csv -Delimiter ";" -Path 'C:\ListeUsersGAL V2.csv' -Encoding Default

$Users = Get-ADUser -SearchBase "OU=Users,OU=xxx,DC=xxx,DC=local" -Filter * | select -Property Name | select -ExpandProperty Name

ForEach ($Changement in $update) {

$log = $Changement.DisplayName

$name = $Changement.Name
$displayname = $Changement.DisplayName

$title = $Changement.Title
$officenumber = $Changement.telephoneNumber
$department = $Changement.Department
$office = $Changement.physicalDeliveryOfficeName

Set-ADUser $log -Title $title -Department $department -OfficePhone $officenumber }

}

my csv file :
DisplayName;Title;telephoneNumber;physicalDeliveryOfficeName;Department;
toto;director;0000;Paris;IT

Any idea

Thanks for help

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

Answer accepted by question author
  1. Rich Matheisen 48,116 Reputation points
    2022-06-05T21:31:02.897+00:00

    There's no "telephoneNumber" key in the $props hash. Make sure you're running the correct code.

    This will check for empty values. It will also distinguish between a missing user and multiple users from the Get-ADUser. In addition, it won't try to update a user if no properties are present in the CSV (other than the display name -- the script does NOT check to see if the displayname column in the CSV is empty!).

    Import-Csv -Delimiter ";" -Path 'C:\ListeUsersGAL V2.csv' -Encoding Default |
        ForEach-Object {
            $u = Get-ADUser -SearchBase "OU=Users,OU=xxx,DC=xxx,DC=local" -Filter "displayname -eq '$($_.DisplayName)'"
            if (-not $u){
                # just some very basic exception handling -- you should probably do something more usefu
                Write-Host "$($_.DisplayName) was not found"
            }
            elseif ($u -is [array]) {
                # just some very basic exception handling -- you should probably do something more useful
                Write-Host "There were $($u.count) users found to have the same displayname: '$($_.DisplayName)"
            }
            else {
                $props = @{
                    Identity    = $u.distinguishedName
                }
                $update = $false  # don't try to update user if no updates are available
                if ($_.Title.Trim().Length -gt 1){$props['title'] = $_.Title; $update = $true}
                if ($_.telephoneNumber.Trim().length -gt 1){$props['officephone'] = $_.telephoneNumber; $update = $true}
                if ($_.Department.Trim().Length -gt 1){$props['department']  = $_.Department; $update = $true}
                if ($_.physicalDeliveryOfficeName.Trim().length -gt 1){$props['office'] = $_.physicalDeliveryOfficeName; $update = $true}
                Try {
                    if ($update){
                        Set-ADUser @props -ErrorAction STOP
                    }
                }
                Catch {
                    # just some very basic exception handling -- you should probably do something more useful
                    Write-Host "Failed to update user with samAccountName '$($u.samaccountname)'"
                    Write-Host $_
                }
            }
        }
    

3 additional answers

Sort by: Most helpful
  1. Momo 21 Reputation points
    2022-06-03T13:12:37.077+00:00

    Hello,

    the error came from the extra $ at the end of the script

    but the script does not work, it can't find any user


  2. Momo 21 Reputation points
    2022-06-03T09:43:23.217+00:00

    thank you for your Answer
    I tested the script I encounter the following error

    Failed to update user with samAccountName 'toto'
    Set-ADUser : Impossible de trouver un paramètre correspondant au nom « officenumber ».
    Au caractère Ligne:20 : 29

    • Set-ADUser @props -ErrorAction STOP
    • ~~~~~~
    • CategoryInfo : InvalidArgument : (:) [Set-ADUser], ParameterBindingException
    • FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.SetADUser

    Any idea ?

    0 comments No comments

  3. Rich Matheisen 48,116 Reputation points
    2022-06-02T19:26:48.933+00:00

    Try this (NOT TESTED! It would be a good idea to add "-WhatIf:$true" to the Set-ADUser until you're satisfied this won't introduce errors into the AD):

    Import-Csv -Delimiter ";" -Path 'C:\ListeUsersGAL V2.csv' -Encoding Default |
        ForEach-Object{
            $u = Get-ADUser -SearchBase "OU=Users,OU=xxx,DC=xxx,DC=local" -Filter "displayname -eq '$($_.DisplayName)'"
            if (-not $u -OR $u -is [array]){
                # just some very basic exception handling -- you should probably do something more useful
                Write-Host "Either $($_.DisplayName) was not found OR more than one user was found to have the same displayname"
            }
            else{
                $props = @{
                    Identity = $u.distinguishedName
                    title = $_.Title
                    officephone = $_.telephoneNumber
                    department = $_.Department
                    office = $_.physicalDeliveryOfficeName
    
                }
                Try{
                    Set-ADUser @props -ErrorAction STOP
                }
                Catch{
                    # just some very basic exception handling -- you should probably do something more useful
                    Write-Host "Failed to update user with samAccountName '$($u.samaccountname)'"
                    $_
                }
            }
        }
    

    Also -- using the DisplayName to identify AD Users is a poor practice. That property is NOT guaranteed to be unique!

    EDIT: Corrected line 12, changed 'officenumber' to 'officephone'.

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.