在PowerShell中是否可以使IndexOf不区分大小写? [英] Is it possible to make IndexOf case-insensitive in PowerShell?

查看:118
本文介绍了在PowerShell中是否可以使IndexOf不区分大小写?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在终端服务器中由query session命令组成的数组中搜索INDEX时遇到问题.

I've got a problem searching an INDEX in an array made up by query sessions command in a terminal server.

这是有问题的脚本:

# Array of logged users in terminal servers 
$a=Get-RDUsersession -CollectionName "BLABLA" -ConnectionBroker BLABLA.BLA.BL

# Array of all users with two columns from active directory
$b=Get-ADUser -filter * -properties TelephoneNumber,SamAccountName

现在想象一下使用帐户名 TEST 而不是 test 登录终端服务器.

Now imagine logging in the terminal server using the account name TEST instead of test.

如果我这样做:

$c = $b[$b.SamAccountName.indexof("test")].TelephoneNumber

那我没有电话号码.

我认为是因为区分大小写,不是吗?如果在搜索命令中键入 TEST ,我会得到正确的数字.

I think that's because of the case sensitivity, isn't it? If I type TEST in the search command, I get the correct number.

是否有任何简单的方法来解决此问题并使索引的搜索不区分大小写?我已经读过关于使用方法 [StringComparison]"CurrentCultureIgnoreCase" 的信息,但是它似乎不适用于数组.

Is there any simple way to solve this problem and make the search of the index case-insensitive? I've read about using the method [StringComparison]"CurrentCultureIgnoreCase", but it seems not working with array.

谢谢.

推荐答案

由于$ b是Object []类型,因此您可能想要执行Where-Object.

Since $b is an Object[] type, then you would probably want to do a Where-Object.

$b | Where-Object -FilterScript {$_.Samaccountname -like '*Smith*'} | Select-Object -ExpandProperty 'telephoneNumber'

也就是说,如果将Powershell中的数组转换为[Collections.Generic.List [Object]]类型,则可以不区分大小写地对其进行索引.

That being said, an array in Powershell can be indexed case-insensitively if it is converted to a [Collections.Generic.List[Object]] type.

$b = [Collections.Generic.List[Object]]$b
$b.FindIndex( {$args[0].sAMAccountName -eq 'test'} )

请注意,在AD中提取每个用户对象并使用where-object或index匹配进行过滤可能非常慢.您可以改为根据需要Get-ADUser或使用过滤器仅提取$ a中返回的用户来提取所有ADuser.

Note that pulling every single user object in AD and filtering using where-object or index matching can be very slow. You can instead Get-ADUser as needed or pull all ADusers using a filter that pulls only the users returned in $a.

如果您坚持一次拉动所有ADUser的位置,请考虑一次遍历列表以进行哈希查找,以便轻松索引哈希值.

If you insist on having all ADUsers in one spot with one pull, consider looping over the list once to make a hash lookup so you can easily index the hash value.

#Create account lookup hash
$accountlookup = @{}
foreach ($element in $accounts) {
  $accountlookup[$element.SamAccountName] = $element 
}

希望有帮助!

这篇关于在PowerShell中是否可以使IndexOf不区分大小写?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆