1

Hey I have been given a CVS file with the data that I must use to populate some already existing empty users. So far I have been able to populate all the data fields I need such as givenName, description, postalCode etc. However I have been unable to populate the user direct reports field. I am new to PowerShell, please see the code below for any errors.

Import-Csv -Delimiter "," -Path C:\script\PerthUsersa.csv | Foreach { 
     $ADUser = Get-ADUser -Filter "userPrincipalName -eq '$($_.userPrincipalName)'"
    
    $DirectReport = Get-ADUser 

    if($ADUser) {
        Set-ADUser -Identity $ADUser -givenName $_.givenName -Surname $_.sn -DisplayName $_.displayName  -Description $_.description -Office $_.physicalDeliveryOfficeName -EmailAddress $_.mail -StreetAddress $_.streetAddress -City $_.l -State $_.st -PostalCode $_.postalCode -Title $_.title -Department $_.description  -MobilePhone $_.mobile -DirectReport $_.directReport
              
       } else {
         Write-Warning ("didnt get in " + $($_.userPrincipalName))
    }    
}

I expected this to also populate the direct report however I have been getting the following error instead:

Set-ADUser : A parameter cannot be found that matches parameter name 'DirectReports'.
At C:\script\perthUsers.ps1:5 char:343
+ ... hone $_.mobile -DirectReports $_.directReports
+                    ~~~~~~~~~~~~~~
    + CategoryInfo          : InvalidArgument: (:) [Set-ADUser], ParameterBindingException
    + FullyQualifiedErrorId : NamedParameterNotFound,Microsoft.ActiveDirectory.Management.Commands.SetADUser

Thanks in advance.

Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37
  • In your CSV, the `DirectReport` column what Values do you have? are them SamAccountNames, DistinguishedNames ? Do you have more than 1 value in each row ? – Santiago Squarzon Feb 27 '23 at 03:06

1 Answers1

0

The Reports attribute is not an attribute you can set manually, its managed by the system. What you can do instead is set the Manager attribute of the users in the DirectReport column of your CSV.

The -Manager attribute from Set-ADUser allows the following values:

  • A distinguished name
  • A GUID (objectGUID)
  • A security identifier (objectSid)
  • A SAM account name (sAMAccountName)

If your column does not have one of these values then you will need to query Active Directory.

Import-Csv -Delimiter "," -Path C:\script\PerthUsersa.csv | ForEach-Object {
    $ADUser = Get-ADUser -Filter "userPrincipalName -eq '$($_.userPrincipalName)'"

    if($ADUser) {
        $aduserSplat = @{
            givenName     = $_.givenName
            Surname       = $_.sn
            DisplayName   = $_.displayName
            Description   = $_.description
            Office        = $_.physicalDeliveryOfficeName
            EmailAddress  = $_.mail
            StreetAddress = $_.streetAddress
            City          = $_.l
            State         = $_.st
            PostalCode    = $_.postalCode
            Title         = $_.title
            Department    = $_.description
            MobilePhone   = $_.mobile
        }
        $ADUser | Set-ADUser @aduserSplat
        # here, if `$_.DirectReport` is not one of the mentioned values
        # you must query AD before
        $_.DirectReport | Set-ADUser -Manager $ADUser
    }
    else {
        Write-Warning ("Didnt get in " + $($_.userPrincipalName))
    }
}
Santiago Squarzon
  • 41,465
  • 5
  • 14
  • 37