2010-12-14 50 views
1

慢慢学习Powershell ...我正在研究脚本以查询第三方AD/AM数据库(ldap)。我想要的特定LDAP属性名称中有一个连字符。如何访问Powershell中的集合上的字符串索引器

我可以在c#中做到这一点,而不用考虑它,但我不想启动Visual Studio只是为了做一些简单的经常变化的脚本。

$objSearcher = New-Object System.DirectoryServices.DirectorySearcher 
..... 
$results = $objSearcher.FindAll() 
foreach($result in $results) { 
    $item = $result.Properties 
    $item.some-property   # this fails because of '-' 
    $result['some-property'] # 'Unable to index into an object of type System.DirectoryServices.SearchResult.' 
} 

回答

0

您需要在带连字符的属性名称周围放置花括号。这应该工作:

$item.{some-property} 
1

您也可通过变量指定属性名:

$prop = 'some-property' 
$result.$prop 
+1

$ result.'some属性”应该工作以及.. – stej 2010-12-15 01:03:45

+0

是的,这也可以。 – 2010-12-15 01:29:44

相关问题