Hi all,
I’ve been using the import-csv cmdlet for years, to automate various jobs, mostly in AD.
Recently, as in v3 upward of Powershell, I have noticed that when importing data through a CSV, the data now looks all messed up.
I am attempting to import a very basic CSV containing a single column list of usernames. The usernames are all correct, and actually pulled from another script I ran and exported from Poweshell. I then need to add those users to a specific security group in AD. Here’s the two methods I’ve attempted:
Example script:
$users = import-csv "csvfile.csv" $group = "securitygroupgoeshere" foreach ($user in $users) { add-adgroupmember -identity $group -Members $user }
As you can see, very simple - I can also simplify this to one line:
import-csv "csvfile.csv" | foreach-object {add-adgroupmember -identity groupname -members $_ }
However, this doesn’t work. On both methods, they error out on each line in the CSV with the following:
Add-ADGroupMember : Cannot bind parameter 'Members'. Cannot convert value "@{akpm860=kelm617}" to type "Microsoft.ActiveDirectory.Management.ADPrincipal". Error: "Cannot convert the "@{akpm860=kelm617}" value of type "System.Management.Automation.PSCustomObject" to type "Microsoft.ActiveDirectory.Management.ADPrincipal"." At G:\Scripts\data moves\add-groupmembers.ps1:4 char:45 + add-adgroupmember -identity $group -Members $user + ~~~~~ + CategoryInfo : InvalidArgument: [:] [Add-ADGroupMember], ParameterBindingException + FullyQualifiedErrorId : CannotConvertArgumentNoMessage,Microsoft.ActiveDirectory.Management.Commands.AddADGroupM ember
Anyone have any idea why it is completely messing up the username from the CSV? That’s not what the imported file looks like at all! It’s almost like it’s taking the first line in the CSV and using that as a fieldname, e.g.
“user1” is ignored
“user2” becomes "@{user1=user2}
This is very strange behaviour. Have they changed something in the way Powershell parses CSVs?
Thanks,
Greg