如何枚举 System._comobject 的所有属性 [英] How to enumerate all of the Properties of a System._comobject

查看:54
本文介绍了如何枚举 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)}

这按预期工作,但如果它是域组,我需要了解有关对象的更多信息,例如域.所以我想列出我可以使用 InvokeMember 查询的所有属性,但我找不到好的解决方案.

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.

使用 Get-Member 等正常方法不起作用,因为这是一个 system._comobject.以下是 Comobject 上 Get_member 的输出:

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() 

我也尝试过查看是否可以找到 ac# 解决方案并找到此链接:C#中System.Type如何返回System.__COMObject的类型

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#

然而,当我尝试在comobect上运行GetProperties时,我编写的程序没有得到任何PropertyDescription.如果需要,我也可以发布 c# 代码,但我觉得这有点离题,因为我只发布了 Powershell 标签.

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.

推荐答案

我在这里写了一个 ComObject Wrapper:https://stackoverflow.com/a/30477959/1701026

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

使用此包装器,您可以轻松地 grep 属性,例如 NameADsPath,如下所示:

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"}

要列出 $Member 的所有属性:

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 *

输出:

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                  :

这篇关于如何枚举 System._comobject 的所有属性的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆