2
votes

I am trying to fetch the AD group members information through power shell. But I am getting the messages as

Export-Csv : Cannot bind argument to parameter 'InputObject' because it is null.

Import-Module ActiveDirectory

$Path = Get-Location

$GroupName = Read-Host "Type the Group Name to check the members in it`t "

foreach ($group in $GroupName) {
    $users = @{}

    Get-ADUser -Filter '*' -Property Name, DisplayName | ForEach-Object {
        $users[$_.DistinguishedName] = $_
    }

    $MemberCount = (Get-ADGroup $group -Properties Member | Select-Object -Expand Member).Count

    Write-Host "`t Total Number of Users/Groups added on this Group : $MemberCount" -BackgroundColor DarkCyan

    $Info = Get-ADGroup $group -Properties Member |
            Select-Object -Expand Member |
            ForEach-Object { $users[$_] }

    $Info | Export-Csv -NoTypeInformation -Append -Force $Path\new.csv
1
Most likely $Info contains emtpy/null values. My guess would be that some of your group members are orphaned (account was deleted w/o removing group membership) or external references or someting like that. Change ForEach-Object { $users[$_] } to something like ForEach-Object { if ($users.ContainsKey($_)) {$users[$_]} else {Write-Host "Unknown DN: ${_}"} }.Ansgar Wiechers
Thanks Angar - edited the way u suggested. its worked. thanksKailas Kichu

1 Answers

0
votes

The error means that $Info contains empty/null values. The most likely reason for their presence is that the group has members that aren't returned by Get-ADUser.

You can avoid the issue by checking for the presence of the key in the $users hashtable:

$Info = Get-ADGroup $group -Properties Member |
        Select-Object -Expand Member |
        ForEach-Object { if ($users.ContainsKey($_) {$users[$_]} }

If you want to further investigate the missing distinguished names you could collect them like this:

$missing = Get-ADGroup $group -Properties Member |
           Select-Object -Expand Member |
           ForEach-Object { if (-not $users.ContainsKey($_) {$_} }