0

I have a script from a previously answered question, but don't have enough reputation to comment. I tried to run that script and came across this error message:

Export-CSV : Cannot append CSV content to the following file: C:\users.csv. The appended object does not have a property that corresponds to the following column: User;Group. To continue with mismatched properties, add the -Force parameter, and then retry the command.

How can I debug this script to resolve this issue?

Function Get-ADGroupsRecursive{
Param([String[]]$Groups)
Begin{
    $Results = @()
}
Process{
    ForEach($Group in $Groups){
        $Results+=$Group
        ForEach($Object in (Get-ADGroupMember $Group|?{$_.objectClass -eq "Group"})){
            $Results += Get-ADGroupsRecursive $Object
        }
    }
}
End{
    $Results | Select -Unique
}}

import-module activedirectory

$users = get-aduser -Filter {Name -Like "*"} -Searchbase "OU=Sample Accounts,DC=domain,DC=com" -Properties MemberOf | Where-Object { $_.Enabled -eq 'True' } 

$targetFile = "C:\users.csv"
rm $targetFile
Add-Content $targetFile "User;Group"


foreach ($user in $users)
{
    $Groups = $User.MemberOf
    $Groups += $Groups | %{Get-ADGroupsRecursive $_}
$Groups | %{New-Object PSObject -Property @{User=$User;Group=$_}}|Export-CSV $targetfile -notype -append
}
Brian Lee
  • 39
  • 7
  • 5
    Remove this line: `Add-Content $targetFile "User;Group"` and let PowerShell handle the CSV headers. – Mark Wragg Jul 07 '17 at 18:45
  • Export-csv tries to match the line you manually added to $targetFile what doesn't work as default -Delimiter is a comma and values are double quoted. So it interprets the contents as one property `User;Group` –  Jul 07 '17 at 18:46
  • @MarkWragg The script now works, but I end up with this error message: `Get-AdGroupMember exceeding limit`. Is there a workaround for this limit? – Brian Lee Jul 07 '17 at 19:12
  • What have you tried? I suggest you search for a solution first. – Mark Wragg Jul 07 '17 at 19:21
  • @markwragg I've run into a different issue now where the script will run (without the error message), and the powershell terminal freezes. The script outputs a .csv file that does not contain the complete population of users, and their group membership. – Brian Lee Jul 10 '17 at 22:43
  • 1
    I believe that I've solved my own question. This script does not execute properly on an OU containing nested OUs. Thus, it seems like the best approach is to edit the script's `searchbase` before executing on nested OUs. – Brian Lee Jul 10 '17 at 22:54

1 Answers1

0

try this function

function Get-InChainGroups
{
    param (
        [parameter(mandatory = $true)]
        $user,
        $domain)

    $user1 = (get-aduser -filter { name -eq $user } -server $domain).distinguishedname
    Write-verbose "checking $user"
    $ldap = "(&(objectcategory=group)(groupType:1.2.840.113556.1.4.803:=2147483648)(member:1.2.840.113556.1.4.1941:=$user1))"

    try { Get-ADobject -LDAPFilter $ldap -server $domain | select  @{ n = 'Identity'; e = { $user } }, Name, @{ n = 'DN'; e = { $_.distinguishedname } } | ft -a }
    catch { "Exception occurred" }


}
gsky
  • 111
  • 5
  • Recommend not using aliases (`select`, `ft`) or formatting cmdlets (`ft`) in a function. Aliases can be non-standard, and formatting is not necessary (let the user decide that). – Bill_Stewart Jul 07 '17 at 22:24
  • sure thing - it was copied from my old module I forgot to remove this – gsky Jul 07 '17 at 22:27