Mark H
Mark H

Reputation: 31

How to enumerate all of the Properties of a System._comobject

I have the following code to generate a list of members of the local administrators group on a machine:

    param([string]$server)
    $localGroupArray =@() 

    $groupName = 'Administrators'
    $Group = [ADSI]"WinNT://$Server/$groupName,group" 
    $Members = @($Group.psbase.Invoke("Members"))
    [Array]$class = $members | Foreach {$_.GetType().InvokeMember("Class", 'GetProperty', $null, $_, $null)}
    [Array]$MemberNames = $Members |  ForEach-Object {$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)}

This works as expected but I need to know more information about the object such as domain if it is a domain group. So I would like to list all of properties that I can query using InvokeMember but I been unable to find a good solution.

Normal ways such as using Get-Member do not work since this is a system._comobject. The following is the output of Get_member on the Comobject:

   Name                      MemberType Definition
   ----                      ---------- ----------
   CreateObjRef              Method     System.Runtime.Remoting.ObjRef                      CreateObjRef(type requestedType)
    Equals                    Method     bool Equals(System.Object obj)
    GetHashCode               Method     int GetHashCode()
    GetLifetimeService        Method     System.Object GetLifetimeService()
    GetType                   Method     type GetType()
    InitializeLifetimeService Method     System.ObjectInitializeLifetimeService()
    ToString                  Method     string ToString() 

I have also tried seeing if I could find a c# solution and was able to find this link: How return the type of a System.__COMObject in System.Type in C#

However, the program I wrote did not get any PropertyDescription when I try to run GetProperties on the comobect. I can also post the c# code if needed but I felt it was a little off topic since I am posting only with the Powershell tag.

Upvotes: 1

Views: 11276

Answers (1)

iRon
iRon

Reputation: 23663

I wrote a ComObject Wrapper here: https://stackoverflow.com/a/30477959/1701026

With this wrapper you can easily grep properties like Name and ADsPath as follows:

([ADSI]"WinNT://$Server/$groupName,group").psbase.Invoke("Members") | ForEach {ComInvoke $_ "ADsPath"}

To list all the properties of a $Member:

$Member = ([ADSI]"WinNT://./$groupName,group").psbase.Invoke("Members") | Select -First 1
[ADSI](ComInvoke $Member ADsPath) | Select -Property *

Output:

UserFlags                  : {66049}
MaxStorage                 : {-1}
PasswordAge                : {37774360}
PasswordExpired            : {0}
LoginHours                 : {255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255 255}
FullName                   : {}
Description                : {Built-in account for administering the computer/domain}
BadPasswordAttempts        : {0}
LastLogin                  : {7/19/2017 9:40:10 AM}
HomeDirectory              : {}
LoginScript                : {}
Profile                    : {}
HomeDirDrive               : {}
Parameters                 : {}
PrimaryGroupID             : {513}
Name                       : {Administrator}
MinPasswordLength          : {7}
MaxPasswordAge             : {3628800}
MinPasswordAge             : {86400}
PasswordHistoryLength      : {24}
AutoUnlockInterval         : {1800}
LockoutObservationInterval : {1800}
MaxBadPasswordsAllowed     : {0}
objectSid                  : {1 5 0 0 0 0 0 5 21 0 0 0 164 27 234 42 18 243 14 131 177 3 198 122 244 1 0 0}
AuthenticationType         : Secure
Children                   : {}
Guid                       : {D83F1060-1E71-11CF-B1F3-0123456789AB}
ObjectSecurity             :
NativeGuid                 : {D83F1060-1E71-11CF-B1F3-0123456789AB}
NativeObject               : System.__ComObject
Parent                     : WinNT://DOMAIN
Password                   :
Path                       : WinNT://DOMAIN/Administrator
Properties                 : {UserFlags, MaxStorage, PasswordAge, PasswordExpired...}
SchemaClassName            : User
SchemaEntry                : System.DirectoryServices.DirectoryEntry
UsePropertyCache           : True
Username                   :
Options                    :
Site                       :
Container                  :

Upvotes: 1

Related Questions