Home > Mobile >  PowerShell get-ADUser missing some information
PowerShell get-ADUser missing some information

Time:12-28

I'm writing a function that will grab information from an AD group and will export it into a csv file. I'm trying to grab the name and description of the users but the description is missing for a couple of the users in the output CSV. Here's my code:

function Get-UserInfo{
param(
    [Microsoft.ActiveDirectory.Management.ADGroup]$DomainName
    )
Get-ADGroupMember -Identity $DomainName |  #get the group of users specified
ForEach-Object {  #For each object grabbed
$test = Get-ADUser -Identity $_ -Properties * | Select-Object Description,Name #grab the Name   Description from each user
[pscustomobject] @{       #once data is grabbed send to custom object
Name = $test.Name
Description = $test.Description
}
} | Export-Csv -NoTypeInformation C:\Users\admin\Desktop\Test.csv    #Export information into CSV
} 

Whats confusing is if I run

Get-ADUser -Identity "User" -Properties *

in the console for one of the users without a description their description shows up. I think it could be an issue with the way I'm pipelining but I'm not sure. Anyone have any advice?

CodePudding user response:

I'm assuming that with parameter $DomainName you actually mean $GroupName.

When you only need two properties returned, do not use -Properties * because when asking for ALL properties, you are wasting time and memory.

Get-ADGroupMember can return objects of type computer, group or user, and because you only want users, you should filter the result.

Try

# get all user objects that are member of the group and collect the data in variable $result
$result = Get-ADGroupMember -Identity $GroupName |    # get the group of users specified
    Where-Object { $_.objectClass -eq 'user' } |      # filter users only (skip groups and computer objects
    ForEach-Object {
        # Get-ADUser by default returns these properties"
        # DistinguishedName, Enabled, GivenName, Name, ObjectClass, ObjectGUID, SamAccountName, SID, Surname, UserPrincipalName
        Get-ADUser -Identity $_.DistinguishedName -Properties Description | 
        # select and output the wanted properties as object. 
        # this will be collected in variable $result
        Select-Object Name, Description           
    } 

# now you can display on screen
$result | Format-Table -AutoSize

# and export to CSV
$result | Export-Csv -Path 'C:\Users\admin\Desktop\Test.csv' -NoTypeInformation

CodePudding user response:

Give this a try, first of all, AD Groups can have members other than user objects, you need to filter them. I would also recommend you to not hard code paths (Export-Csv) inside of functions.

function Get-UserInfo{
param(
    [parameter(ValueFromPipeline, Mandatory)]
    [Microsoft.ActiveDirectory.Management.ADGroup]
    $DomainName
)
    
    @(Get-ADGroupMember -Identity $DomainName).Where({
        $_.ObjectClass -eq 'user' # => Groups can have other object types
    }) | Get-ADUser -Properties Description | # => Takes DN from pipeline
    Select-Object Description, Name |
    Export-Csv -NoTypeInformation C:\Users\admin\Desktop\Test.csv
}

Instead of having Export-Csv inside the function you could remove it then you could do something like this with your function:

Get-ADGroup 'somegroup' | Get-UserInfo | Export-Csv path/to/csv.csv
  • Related