0

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.

0

1 Answer 1

3

Your data format isn't CSV, so I wouldn't recommend using *-Csv cmdlets for processing it. Simply output custom-formatted lines with Out-File:

$var | ForEach-Object { 
    if ($_ -like "adm*") {
        $_
    } else {
        $group = Get-ADGroup $_ -Properties samaccountname,description,info
        '{0} {1} {2}' -f $group.samaccountname, $group.description, $group.info
    }
} | Out-File 'C:\ahoy\supertest.txt'
Sign up to request clarification or add additional context in comments.

2 Comments

I really appreciate that, Ansgar! That works very well. Is there a way I could add a special character between the {0} {1} and {2} columns, so I could actually out-file to a csv, open in Excel, and then text to columns it out by the special character just so it's easier to read?
'{0} {1} {2}' is a format string, with the numbers in curly brackets being placeholders. You can use any character you like between the placeholders. Note, however, that you need to replace the outer single quotes with double quotes if you want to use escape sequences (e.g. `t for a tabulator).

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.