I have a variable, $var
, that holds Active Directory account names, and the AD groups that each account is a member of. The variable currently looks like this:
Username1 group1 group2 group3 Username2 group1 group2 Username3 group1 group2 group3 …
Now, I want to loop through $var
. If the entry is a username (all of these start with adm, so that's easy), append it to a CSV. If it's a group (-notlike "adm*"
), pull group samaccountname, description, and info, and append it to the same CSV. I have no problem exporting the account names to a CSV, and no problem separately exporting the group info to the CSV using the loop. I think there is an issue with exporting the group info to the CSV once an account name is in there; it seems to have an issue with the header or column name. Here is what I'm trying to use:
foreach ($z in $var) {
if ($z -like "adm*") {
$z | out-file c:\ahoy\supertest.csv -append
} elseif ($z -notlike "adm*") {
Get-ADGroup $z -Properties samaccountname,description,info |
select samaccountname,description,info |
Export-Csv c:\ahoy\supertest.csv -append -NoTypeInformation
}
}
Now, here is what I want the CSV to look like:
Username1 group1name group1description group1info group2name group2description group3info Username2 group1name group1description group1info group2name group2description group3info
Here is the error I get:
Export-Csv : Cannot append CSV content to the following file: c:\ahoy\supertest.csv. The appended object does not have a property that corresponds to the following column: adm007. To continue with mismatched properties, add the -Force parameter, and then retry the command. At line:6 char:103 + ... ription,info | Export-Csv c:\ahoy\supertest.csv -append -NoTypeInformation + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ + CategoryInfo : InvalidData: (adm007:String) [Export-Csv], InvalidOperationException + FullyQualifiedErrorId : CannotAppendCsvWithMismatchedPropertyNames,Microsoft.PowerShell.Commands.ExportCsvCommand
Yes, the first account name is adm007. Hope you guys can help me.