BaconBurner
BaconBurner

Reputation: 62

How to enumerate available properties of member

In the code below there are a number of properties of "member" I've added "description" property successfully but I can't find out if the account is enabled. I've tried "status" or "enabled" or "disabled" all to no avail. I realize it's a member of a group of an ADSI call but, I really need to know if the account is enabled or not.

Thanks in advance!

Full script available at https://github.com/JDogHerman/Powershell_Scripts/blob/master/get-localgroupmembers.ps1

Process {
    ForEach($Computer in $ComputerName) {
        Write-host "Working on $Computer"
        If(!(Test-Connection -ComputerName $Computer -Count 1 -Quiet)) {
            Write-Verbose "$Computer is offline. Proceeding with next computer"
            Add-Content -Path $OutputFile -Value "$Computer,$LocalGroupName,Offline"
            Continue
        } else {
            Write-Verbose "Working on $computer"
            try {
                $group = [ADSI]"WinNT://$Computer/$LocalGroupName"
                $members = @($group.Invoke("Members"))
                Write-Verbose "Successfully queries the members of $computer"
                if(!$members) {
                    Add-Content -Path $OutputFile -Value "$Computer,$LocalGroupName,NoMembersFound"
                    Write-Verbose "No members found in the group"
                    continue
                }
            }        
            catch {
                Write-Verbose "Failed to query the members of $computer"
                Add-Content -Path $OutputFile -Value "$Computer,,FailedToQuery"
                Continue
            }
            foreach($member in $members) {
                try {
                    $MemberName = $member.GetType().Invokemember("Name","GetProperty",$null,$member,$null)
                    $MemberType = $member.GetType().Invokemember("Class","GetProperty",$null,$member,$null)
                    $MemberPath = $member.GetType().Invokemember("ADSPath","GetProperty",$null,$member,$null)
                    $MemberDomain = $null

Upvotes: 2

Views: 386

Answers (1)

Santiago Squarzon
Santiago Squarzon

Reputation: 60045

Based on this answer, you can change this part of your code:

foreach($member in $members) {
    try {
        $MemberName = $member.GetType().Invokemember("Name","GetProperty",$null,$member,$null)
        $MemberType = $member.GetType().Invokemember("Class","GetProperty",$null,$member,$null)
        ....

For this:

$members.ForEach([adsi]).ForEach({
    $enabled = switch ($_.class) {
        User    { ('Enabled', 'Disabled')[[bool]($_.UserFlags.Value -band 2)] }
        Default { 'Not Applicable'}
    }

    [pscustomobject]@{
        Name    = $_.Name.Value
        Class   = $_.Class
        ADSPath = $_.ADSPath
        Enabled = $enabled
    }
})

You can add a try / catch logic if you believe it's needed. As also stated in comments, the built-in cmdlet Get-LocalUser already does this for you.

Upvotes: 1

Related Questions