【问题标题】:How to enumerate all of the Properties of a System._comobject如何枚举 System.__comobject 的所有属性
【发布时间】:2017-09-09 02:21:59
【问题描述】:

我有以下代码来生成机器上本地管理员组的成员列表:

    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 查询的所有属性,但我找不到好的解决方案。

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

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

我也尝试过看看是否可以找到 c# 解决方案并能够找到此链接:How return the type of a System.__COMObject in System.Type in C#

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

【问题讨论】:

  • 什么版本的 PowerShell/Windows?在最近的版本中,这只是Get-LocalGroupMember -Group "Administrators"(要获取远程计算机的列表,请使用Invoke-Command -ComputerName $server 远程命令),这样就省去了使用 WinNT 提供程序的所有麻烦。
  • 使用[ADSI] 加速器的本地用户或域用户是不会得到的。就我个人而言,当我在一个盒子上有管理员时,我使用 WMI 来获取域,但这并不能真正解决您在 COMObject 上枚举属性的问题。

标签: powershell


【解决方案1】:

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

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

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

列出$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                  :

【讨论】:

  • 我上面的代码已经可以获取属性值了。你的方法也有效。我的解决方案和您的解决方案遇到的问题是您已经必须知道 comobject 必须查询哪些属性。我想要找到的是一种查询 comobject 以列出我可以查询的所有属性的方法
  • 对不起,我错过了。要列出成员的所有属性,基本上是在 ADSI 成员对象上执行 Select -Property *。 (我已相应地将其添加到我的答案中)
猜你喜欢
  • 2011-06-15
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2017-06-03
  • 1970-01-01
  • 2019-04-08
  • 1970-01-01
相关资源
最近更新 更多