如何在 Powershell 中访问集合上的字符串索引器
慢慢学习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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
您还可以通过变量指定属性名称:
You can also specify the property name via a variable:
您需要将大括号放在连字符的属性名称周围。这应该有效:
You need to place curly braces around the hyphenated property name. This should work: