0

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
    }
}
kardon
  • 35
  • 1
  • 6
  • Are you working on the same Domain as the Domain you're pointing your query? Any specific reason why GC port is hardcoded on your `$domain` variable? – Santiago Squarzon Apr 12 '21 at 15:52

1 Answers1

2

As Santiago implied this looks like an issue of cross domain or forest communication. And/or you are passing a user from one domain and attempting to set it in another.

See here, and a link here

An aside, a few pointers; You don't need to filter for samAccountName when you already have the samAccountName, the query can be something like:

$setUsers =
Get-ADUser $samAccountName -Server $domain |
ForEach-Object{
    Set-ADUser $_ -Server tstdc01 -Company "New Company" -PassThru    
}

Also note: you should avoid += I don't know how big a job this is, but it can degrade performance as it causes the creation of a new array and a copy of the existing array. Repeated many times that can cause big performance issues. There are a number of ways to address that, but the preferred approach is to let PowerShell gather the results for you. So above assign the $setUsers variable to the output of the loop. Add -PassThru to output the object that was set.

Update: With the point regarding += demonstrated, @SantiagoSquarzon pointed out we don't even need the loop anymore:

$setUsers =
Get-ADUser $samAccountName -Server $domain |
Set-ADUser -Server tstdc01 -Company "New Company" -PassThru
Steven
  • 6,817
  • 1
  • 14
  • 14
  • 1
    Yup, agreed. This also should work `Get-ADUser $samAccountName -Server $domain | Set-ADUser -Server tstdc01 -Company "New Company" -PassThru` – Santiago Squarzon Apr 12 '21 at 17:30
  • 2
    @SantiagoSquarzon Duh why didn't I see that... I guess sometimes where you start affects where you finish. Let me update and credit you... – Steven Apr 12 '21 at 17:51
  • 2
    That's fine you don't need to credit me, your answers are always pretty accurate :) – Santiago Squarzon Apr 12 '21 at 18:01