如何在 Powershell 中访问集合上的字符串索引器

发布于 2024-10-07 18:34:38 字数 522 浏览 0 评论 0原文

慢慢学习Powershell ...我正在编写一个脚本来查询第三方AD/AM数据库(ldap)。我想要的特定 LDAP 属性名称中包含连字符。

我可以不假思索地在 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.'
}

Slowly learning Powershell ... I'm working on a script to query a third party AD/AM database (ldap). The specific LDAP property name that I want has a hyphen in the name.

I can do this in c# without thinking about it, but I don't want to fire up Visual Studio just to do some simple scripting stuff that changes frequently.

$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.'
}

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

擦肩而过的背影 2024-10-14 18:34:39

您还可以通过变量指定属性名称:

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

You can also specify the property name via a variable:

$prop = 'some-property'
$result.$prop
无人接听 2024-10-14 18:34:39

您需要将大括号放在连字符的属性名称周围。这应该有效:

$item.{some-property}

You need to place curly braces around the hyphenated property name. This should work:

$item.{some-property}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文