0

I'm syncing up user attributes in AD with a provided CSV file. When I try to call my SyncUser function I get the following error:

SyncUser : Cannot process argument transformation on parameter 'user'. Cannot convert the "System.Object[]" value of type "System.Object[]" to type "Microsoft.ActiveDirectory.Management.ADUser".

$user.GetType() tells me the name is ADUser with a base type of ADAccount yet the error message acts as though I'm passing an object array. Powershell is still fairly new to me so maybe my function syntax is incorrect? I'm confused at this point.

$AD_Users = Get-ADUsers -SearchBase $adPath -LDAPFilter $filter
$UPNs = $AD_Users | ForEach-Object -Begin { $ht = @{} } _Process { $ht.add($_."userPrincipalname", $_) } -End { return $ht }


Import-Csv $file | ForEach-Object {
    
    $user = $UPNs[$_."userPrincipalName"]
    SyncUser($user, $_)
}


function  SyncUser{
    param([Microsoft.ActiveDirectory.Management.ADUser]$user, [System.Object]$c)

    # do stuff

}
Phaelax z
  • 1,814
  • 1
  • 7
  • 19

1 Answers1

1

You are not calling your function correctly. It should be:

    SyncUser $user $_

PowerShell does not take parameters in parenthesis like that for functions. The function thinks you are trying to pass an array to the first parameter, that array being $user,$_.

TheMadTechnician
  • 34,906
  • 3
  • 42
  • 56
  • Yea I just realized that myself. Drawback to staring at different languages so often, syntax looks correct but for the wrong language... – Phaelax z Feb 09 '22 at 23:08