Set-AdUser a referral was returned from the server

kardon 1 Reputation point
2021-04-12T15:10:56.723+00:00

I'm trying to set a new company value for users from a CSV.

I read that you get the error when not setting the server in my Set-AdUser command.

At line:18 char:18
+ ...    $query | Set-ADUser -Server tstdc01 -Company "New Company"
+                 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
 + CategoryInfo          : ResourceUnavailable: (CN=testUser\, H...DC=domain,DC=local:ADUser) [Set-ADUser], ADReferralException
 + FullyQualifiedErrorId : ActiveDirectoryServer:8235,Microsoft.ActiveDirectory.Management.Commands.SetADUser

--------------------------------------------------------------------------------------------------------

Import-Module ActiveDirectory
$users = Import-Csv -Path "pathToCsv.csv" -Encoding UTF8 -Delimiter ";"
$setUsers = @()
$domain = "domain.local:3268"
foreach ($user in $users) {
 $samAccountName = $user.samAccountName
 $query = Get-ADUser -Filter {Samaccountname -eq $samAccountName} -Server $domain
 if ($query) {
     $query | Set-ADUser -server tstdc01-Company "New Company"
     $setUsers += $query
 }
}
Active Directory
Active Directory
A set of directory-based technologies included in Windows Server.
5,847 questions
Windows Server PowerShell
Windows Server PowerShell
Windows Server: A family of Microsoft server operating systems that support enterprise-level management, data storage, applications, and communications.PowerShell: A family of Microsoft task automation and configuration management frameworks consisting of a command-line shell and associated scripting language.
5,362 questions
0 comments No comments
{count} votes

2 answers

Sort by: Most helpful
  1. Andreas Baumgarten 96,266 Reputation points MVP
    2021-04-12T18:30:52.513+00:00

    Hi @kardon ,

    please try this:

    Import-Module ActiveDirectory  
     $users = Import-Csv -Path "users.csv" -Encoding UTF8 -Delimiter ";"  
     $setUsers = @()  
     $domain = "TEST.LOCAL"  
     foreach ($user in $users) {  
      $samAccountName = $user.samAccountName  
      $query = Get-ADUser -Filter {Samaccountname -eq $samAccountName} -Server $domain  
      if ($query) {  
         $query | Set-ADUser -server $domain -Company "New Company"  
         $setUsers += $query  
      }  
     }  
    

    Here in my environment it's working. Only modification I did:
    I removed the port in $domain = "domain.local:3268"

    Get-ADUser accepts for -server:

    Domain name values:
    Fully qualified domain name (FQDN)
    NetBIOS name

    Directory server values:
    Fully qualified directory server name
    NetBIOS name
    Fully qualified directory server name and port

    Source: https://learn.microsoft.com/en-us/powershell/module/activedirectory/get-aduser?view=windowsserver2019-ps
    For the domain FQDN no port allowed
    For domain controller FQDN a port is ok

    I changed the -server value here ``$query | Set-ADUser -server tstdc01 -Company "New Company" `


    (If the reply was helpful please don't forget to upvote and/or accept as answer, thank you)

    Regards
    Andreas Baumgarten

    0 comments No comments

  2. Ian Xue (Shanghai Wicresoft Co., Ltd.) 29,571 Reputation points Microsoft Vendor
    2021-04-13T07:20:11.89+00:00

    Hi,

    I ran your script on my server but it worked well. What about ADSI?

    $user = [ADSI]"LDAP://cn=testuser,ou=testou,dc=domain,dc=local"  
    $user.company = "New Company"  
    $user.setinfo()  
    

    Best Regards,
    Ian Xue

    ============================================

    If the Answer is helpful, please click "Accept Answer" and upvote it.
    Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.

    0 comments No comments