Powershell获得2级房产

时间:2014-07-31 22:27:15

标签: powershell properties

它是怎么回事flackoverstow,

我是PowerShell的新手,我似乎无法为源代码检索两个级别的属性(也就是说:我可以获得一个对象的属性,如$ foo.bar,但不是所述属性的属性,如$ foo.bar.soap)

我有以下代码:

param (
    [parameter(mandatory=$false)]$Output = ".\OrphanedGPTs.txt",
    [parameter(mandatory=$false)]$Domain = [System.DirectoryServices.ActiveDirectory.Domain]::GetCurrentDomain()
)

$strGCPath = "LDAP://" + $Domain.Name
$oCmd = New-Object -ComObject ADODB.command
$oConnection = New-Object -ComObject ADODB.connection
$oConnection.Provider = "ADsDSOObject"
$oConnection.Open("ADs Provider" )

$oCmD.ActiveConnection = $oConnection

$strADOQuery = "<"+ $strGCPath + ">;(objectCategory=group);distinguishedName;subtree"

$oCmd.CommandText = $strADOQuery

$TopLevel = $oCmd.Execute() #this works
$Fields = $oRecordSet.Fields #this works
$Name = $Fields.Name #this doesn't seem work
$TopLevel.MoveNext()

$TopLevel #this works
Write-Output "-----------------------"
$Fields #this works
Write-Output "-----------------------"
$Name #this returns nothing
Write-Output "-----------------------"
$TopLevel.Fields.Name #This also return nothing
Write-Output "-----------------------"

其中输出以下内容:

    Properties       : System.__ComObject
    AbsolutePosition : 2
    ActiveConnection : System.__ComObject
    BOF              : False
    Bookmark         : 1
    CacheSize        : 1
    CursorType       : 3
    EOF              : False
    Fields           : System.__ComObject
    LockType         : 1
    MaxRecords       : 0
    RecordCount      : 932
    Source           : <LDAP://company.com>;(objectCategory=group);distinguishedName;subtree
    AbsolutePage     : 1
    EditMode         : 0
    Filter           : 0
    PageCount        : 94
    PageSize         : 10
    Sort             : 
    Status           : 0
    State            : 1
    CursorLocation   : 2
    MarshalOptions   : 0
    DataSource       : System.__ComObject
    ActiveCommand    : System.__ComObject
    StayInSync       : True
    DataMember       : 
    Index            : 

    -----------------------
    Properties      : System.__ComObject
    ActualSize      : 234
    Attributes      : 32
    DefinedSize     : 4000
    Name            : distinguishedName
    Type            : 202
    Value           : CN=blank,OU=blank,OU=blank 
              Groups,DC=company,DC=com
    Precision       : 255
    NumericScale    : 255
    OriginalValue   : 
    UnderlyingValue : 
    DataFormat      : 
    Status          : 0

    -----------------------
    -----------------------
    -----------------------

如果代码像我想要的那样工作,那么底部的虚线之间应该有“distinguishedName”或打印的东西。

如何获得$ TopLevel.Fields.Name?

1 个答案:

答案 0 :(得分:1)

首先,我要指出,虽然您可以在COM对象上使用PowerShell,但通常有更好的替代方案,在这种情况下,它是ActiveDirectory模块:

Import-Module ActiveDirectory
Get-ADDomain

可以找到有关此模块的完整文档here。但是,让我们回答你的问题。通常,您的语法将起作用,但不适用于COM属性。例如,您可以这样做:

$TopLevel.Fields.Count    # returns 1 on my computer

但Name是一个COM属性,因此您需要使用Select-Object

$TopLevel.Fields | Select-Object *

Properties      : System.__ComObject
ActualSize      : 194
Attributes      : 32
DefinedSize     : 4000
Name            : distinguishedName
Type            : 202
Value           : CN=Exchange Organization Administrators,OU=Microsoft Exchange Security Groups,DC=XXXXXXXX,DC=XXX
Precision       : 255
NumericScale    : 255
OriginalValue   : 
UnderlyingValue : 
DataFormat      : 
Status          : 0

因此,要回答您的具体问题(如何获取$ TopLevel.Fields.Name),您可以这样做:

$name = $TopLevel.Fields | Select-Object -ExpandProperty Name