0

I'm not so good at PowerShell. I have this script which worked before that I've have used to update the manager attribute in Active Directory, however, now after just replacing the file its no longer working

$Users = Import-CSV C:\Documents\Managers.csv
ForEach ($User in $Users) {
   $user= Get-ADUser -Filter "displayname -eq '$($Users.EmployeeFullName)'"|select -ExpandProperty samaccountname
   $manager=Get-ADUser -Filter "displayname -eq '$($Users.'Line Manager Fullname')'"|select -ExpandProperty DistinguishedName

Set-ADUser -Identity $user -Replace @{manager=$manager}
}

when running the script it returns:

Set-ADUser : Cannot validate argument on parameter 'Identity'. The argument is null. Provide a valid value for the argument, and then try running the command again.
At line:6 char:22
+ Set-ADUser -Identity $user -Replace @{manager=$manager}
+                      ~~~~~
    + CategoryInfo          : InvalidData: (:) [Set-ADUser], ParentContainsErrorRecordException
    + FullyQualifiedErrorId : ParameterArgumentValidationError,Microsoft.ActiveDirectory.Management.Commands.SetADUser

the "EmployeeFullName" and "Line Manager FullName" format in the .csv is set as i.e. joe bloggs, see below CSV file example.

EmployeeFullname   Line Manager Fullname 
----------------   ---------------------
Kieran Rhodes      Tracey Barley    
Lewis Pontin       Tracey Barley
Lizzy Wilks        Rodney Bennett

I also noticed if I remove and try to retype "$Users.EmployeeFullName" it no longer picks up the "EmployeeFullName" from the csv file.

Anyone know what I'm doing wrong here please?

6
  • Inside the loop you are referencing $Users when it should be your iteration variable, $User. The way you have it the properties are unrolling and its probably messing up the Get-ADUser command resulting in a null value being used in Set-ADUser's -Identity argument. Commented Feb 24, 2021 at 1:17
  • so like below? I tried that but returns "the search filter cannot be recognised" $Users = Import-CSV C:\Documents\Managers.csv ForEach ($User in $Users) { $user= Get-ADUser -Filter "displayname -eq '$($User.EmployeeFullName)'"|select -ExpandProperty samaccountname $manager=Get-ADUser -Filter "displayname -eq '$($User.'Line Manager Fullname')'"|select -ExpandProperty DistinguishedName Set-ADUser -Identity $user -Replace @{manager=$manager} } Commented Feb 24, 2021 at 1:24
  • Sorry don't want to read in comment format, but I posted an answer below to illustrate. It would also help if I had a sample of the csv content. I may be able to optimize further. Commented Feb 24, 2021 at 1:30
  • thanks, below is the format of the csv file e.g. EmployeeFullname Line Manager FullName Kieran Rhodes Tracey Barley Lizzy Wilks Rodney Bennett so i.e. 2 rows 1 for "EmployeeFullname" and 1 for "Line Manager FullName" Commented Feb 24, 2021 at 1:37
  • I reworded the question, but I think the samples below are already sufficient. I was just surprised at the column names. At any rate, drop another comment if anything. Commented Feb 24, 2021 at 1:48

1 Answer 1

2

Below would be an example:

$Users = Import-CSV C:\Documents\Managers.csv
ForEach ($User in $Users) {
   $user    = Get-ADUser -Filter "displayname -eq '$($User.EmployeeFullName)'"
   $manager = (Get-ADUser -Filter "displayname -eq '$($User.'Line Manager Fullname')'").DistinguishedName

Set-ADUser -Identity $User -Replace @{manager=$manager}
}

Note: you don't need to dig down to the samAccountName property because Set-ADUser will take the full fidelity object for the -Identity argument. You also don't need to use Select... -ExpandProperty you can just parenthetically dot reference the distinguishedName property.

Also you can use the instancing capability in the AD cmdlets:

$Users = Import-CSV C:\Documents\Managers.csv
ForEach ( $User in $Users )
{
   $User    = Get-ADUser -Filter "displayname -eq '$($User.EmployeeFullName)'" -Properties Manager
   $Manager = (Get-ADUser -Filter "displayname -eq '$($User.'Line Manager Fullname')'").DistinguishedName

   $User.Manager = $Manager
   Set-ADUser -Instance $User 
}

In this case you call back the Manager property set it with a simple assignment statement than give the $User variable as the argument to the -Instance parameter of Set-ADUser

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.