无法识别PowerShell中项目的属性

在试图回答这个问题时,我碰到了一些曾经困扰我的东西,而我一直没能find答案。

以下脚本块将列出本地pipe理员组的所有成员的名称。

$group = [ADSI]"WinNT://./Administrators" @($group.Invoke("Members")) | foreach {$_.GetType().InvokeMember("Name", 'GetProperty', $null, $_, $null)} 

但是它只会列出名称,而没有其他属性。

我相当确定,我可以提取其他Members属性,但我不明白我将如何去确定这些其他属性是什么。

我不一定需要知道该项目的附加属性,这更多的是如何去寻找它们的问题。

(我很抱歉,如果这有点含糊,我非常自我教导,而且我很清楚,我可能会吠叫错误的树和/或经常犯下可怕的错误。)

请在这里查看可用的属性:

http://msdn.microsoft.com/en-us/library/aa705950(v=VS.85).aspx

和你的问题类似的例子:

http://social.technet.microsoft.com/Forums/windowsserver/en-US/b4d51781-e304-45b1-a7b1-c21b62263540/adsi-local-group-enum-from-fancy-powershell-to-simple-foreach-重写?论坛= winserverpowershell

由于您已经获得了该组的成员名称的列表,为了获取成员的详细信息,我将再次查询,除了针对个人用户而不是组。

 PS C:\> $group = [ADSI]"WinNT://./administrators" PS C:\> $members = $group.Invoke("Members") | % {$_.GetType().InvokeMember("name", 'GetProperty', $null, $_, $null) } PS C:\> $membersObjects = @() ; $members | % { $membersObjects += [ADSI]"WinNT://./$_" } PS C:\> $membersObjects | gm TypeName: System.DirectoryServices.DirectoryEntry Name MemberType Definition ---- ---------- ---------- ConvertDNWithBinaryToString CodeMethod static string ConvertDNWithBinaryToString(psobject deInstance, psobject dnWithBinaryInstance) ConvertLargeIntegerToInt64 CodeMethod static long ConvertLargeIntegerToInt64(psobject deInstance, psobject largeIntegerInstance) AutoUnlockInterval Property System.DirectoryServices.PropertyValueCollection AutoUnlockInterval {get;set;} BadPasswordAttempts Property System.DirectoryServices.PropertyValueCollection BadPasswordAttempts {get;set;} Description Property System.DirectoryServices.PropertyValueCollection Description {get;set;} FullName Property System.DirectoryServices.PropertyValueCollection FullName {get;set;} HomeDirDrive Property System.DirectoryServices.PropertyValueCollection HomeDirDrive {get;set;} HomeDirectory Property System.DirectoryServices.PropertyValueCollection HomeDirectory {get;set;} LastLogin Property System.DirectoryServices.PropertyValueCollection LastLogin {get;set;} LockoutObservationInterval Property System.DirectoryServices.PropertyValueCollection LockoutObservationInterval {get;set;} LoginHours Property System.DirectoryServices.PropertyValueCollection LoginHours {get;set;} LoginScript Property System.DirectoryServices.PropertyValueCollection LoginScript {get;set;} MaxBadPasswordsAllowed Property System.DirectoryServices.PropertyValueCollection MaxBadPasswordsAllowed {get;set;} MaxPasswordAge Property System.DirectoryServices.PropertyValueCollection MaxPasswordAge {get;set;} MaxStorage Property System.DirectoryServices.PropertyValueCollection MaxStorage {get;set;} MinPasswordAge Property System.DirectoryServices.PropertyValueCollection MinPasswordAge {get;set;} MinPasswordLength Property System.DirectoryServices.PropertyValueCollection MinPasswordLength {get;set;} Name Property System.DirectoryServices.PropertyValueCollection Name {get;set;} objectSid Property System.DirectoryServices.PropertyValueCollection objectSid {get;set;} Parameters Property System.DirectoryServices.PropertyValueCollection Parameters {get;set;} PasswordAge Property System.DirectoryServices.PropertyValueCollection PasswordAge {get;set;} PasswordExpired Property System.DirectoryServices.PropertyValueCollection PasswordExpired {get;set;} PasswordHistoryLength Property System.DirectoryServices.PropertyValueCollection PasswordHistoryLength {get;set;} PrimaryGroupID Property System.DirectoryServices.PropertyValueCollection PrimaryGroupID {get;set;} Profile Property System.DirectoryServices.PropertyValueCollection Profile {get;set;} UserFlags Property System.DirectoryServices.PropertyValueCollection UserFlags {get;set;} 

问题在于你正在处理一个COM对象,而这些对象似乎并没有提供一种在PowerShell中显示所有对象的方法。

你也可以看看类似的问题在不同的(C#)线程在这里: https : //stackoverflow.com/questions/10615019/get-property-names-via-reflection-of-an-com-object

扩展@雅各布的想法。 枚举组的成员时,只返回string对象,而不是AD用户对象。 因此,唯一可用的属性是string属性(即长度等)。 您需要再次使用名称作为-identity参数来查询AD以检索用户属性。

在AD你可以做这样的事情:

 $(get-adgroup "administrators" -Properties members).members|foreach {get-aduser -identity $_} 

我不能说代码的WinNT