如果您想过滤包含第一个字母w并且后跟“x”数字的名称,那么如何过滤它?
例如:
w34593 w44569a we5552 w01123 w85532
我一直在用
开始$userIDs = Get-Aduser -filter {name -like "w3*" -or name -like "w4*"} -Properties * | Select name, SurName, DisplayName
问题是从w1,w2,w3,w0开始的时间很长,我不想过滤掉每一个或代码都是愚蠢的。
有什么建议吗?每个名字通常是6到7个字符长,所以有一个范围长度可以帮助我吗?我试过.length但它似乎不适用于我的powershell版本
更新
$userIDs = Get-Aduser -filter {(name -gt "w30000*" -and name -lt "w99999*") -or (name -gt "we0000*" -and name -lt "we9999*") `
-or (name -gt "cr0000*" -and name -lt "cr9999*") -or (name -gt "ac0000**" -and name -lt "ac9999*")`
-or (name -gt "cm0000*" -and name -lt "cm9999*") -or (name -gt "do0000*" -and name -lt "do9999*") `
-or (name -gt "ec0000*" -and name -lt "ec9999*") -or (name -gt "ev0000*" -and name -lt "ev9999*") `
-or (name -gt "fm0000*" -and name -lt "fm9999*") -or (name -gt "ia0000*" -and name -lt "ia9999*") `
-or (name -gt "in0000*" -and name -lt "in9999*") -or (name -gt "le0000*" -and name -lt "le9999*") `
-or (name -gt "md0000*" -and name -lt "md9999*") -or (name -gt "mk0000*" -and name -lt "mk9999*") `
-or (name -gt "np0000*" -and name -lt "np9999*") -or (name -gt "pb0000*" -and name -lt "pb9999*") `
-or (name -gt "ps0000*" -and name -lt "ps9999*") -or (name -gt "re0000*" -and name -lt "re9999*") `
-or (name -gt "sf0000*" -and name -lt "sf9999*") -or (name -gt "so0000*" -and name -lt "so9999*") `
-or (name -gt "tr0000*" -and name -lt "tr9999*") -or (name -gt "wn0000*" -and name -lt "wn9999*") } `
-Properties * | Select name, SurName, DisplayName, enabled
ERROR:
Get-ADUser : Error parsing query: '(name -gt "w30000*" -and name -lt "w99999*")
-or (name -gt "we0000*" -and name -lt "we9999*") `
-or (name -gt "cr0000*" -and name -lt "cr9999*") -or (name -gt "ac
0000**" -and name -lt "ac9999*")`
-or (name -gt "cm0000*" -and name -lt "cm9999*") -or (name -gt "do
0000*" -and name -lt "do9999*") `
-or (name -gt "ec0000*" -and name -lt "ec9999*") -or (name -gt "ev
0000*" -and name -lt "ev9999*") `
-or (name -gt "fm0000*" -and name -lt "fm9999*") -or (name -gt "ia
0000*" -and name -lt "ia9999*") `
-or (name -gt "in0000*" -and name -lt "in9999*") -or (name -gt "le
0000*" -and name -lt "le9999*") `
-or (name -gt "md0000*" -and name -lt "md9999*") -or (name -gt "mk
0000*" -and name -lt "mk9999*") `
-or (name -gt "np0000*" -and name -lt "np9999*") -or (name -gt "pb
0000*" -and name -lt "pb9999*") `
-or (name -gt "ps0000*" -and name -lt "ps9999*") -or (name -gt "re
0000*" -and name -lt "re9999*") `
-or (name -gt "sf0000*" -and name -lt "sf9999*") -or (name -gt "so
0000*" -and name -lt "so9999*") `
-or (name -gt "tr0000*" -and name -lt "tr9999*") -or (name -gt "wn
0000*" -and name -lt "wn9999*") ' Error Message: 'Operator Not supported: ' at
position: '95'.
At J:\DATA\IR\ITSecurity\Intern\AlyssaC 2014\Part-time 2014-2015 task\AD\AD cle
an-up\Daily Script\AD-User.MonitorOwnership.ps1:3 char:22
+ $userIDs = Get-Aduser <<<< -filter {(name -gt "w30000*" -and name -lt "w9999
9*") -or (name -gt "we0000*" -and name -lt "we9999*") `
+ CategoryInfo : ParserError: (:) [Get-ADUser], ADFilterParsingEx
ception
+ FullyQualifiedErrorId : Error parsing query: '(name -gt "w30000*" -and n
ame -lt "w99999*") -or (name -gt "we0000*" -and name -lt "we9999*") `
-or (name -gt "cr0000*" -and name -lt "cr9999*") -or (name -g
t "ac0000**" -and name -lt "ac9999*")`
-or (name -gt "cm0000*" -and name -lt "cm9999*") -or (name -g
t "do0000*" -and name -lt "do9999*") `
-or (name -gt "ec0000*" -and name -lt "ec9999*") -or (name -g
t "ev0000*" -and name -lt "ev9999*") `
-or (name -gt "fm0000*" -and name -lt "fm9999*") -or (name -g
t "ia0000*" -and name -lt "ia9999*") `
-or (name -gt "in0000*" -and name -lt "in9999*") -or (name -g
t "le0000*" -and name -lt "le9999*") `
-or (name -gt "md0000*" -and name -lt "md9999*") -or (name -g
t "mk0000*" -and name -lt "mk9999*") `
-or (name -gt "np0000*" -and name -lt "np9999*") -or (name -g
t "pb0000*" -and name -lt "pb9999*") `
-or (name -gt "ps0000*" -and name -lt "ps9999*") -or (name -g
t "re0000*" -and name -lt "re9999*") `
-or (name -gt "sf0000*" -and name -lt "sf9999*") -or (name -g
t "so0000*" -and name -lt "so9999*") `
-or (name -gt "tr0000*" -and name -lt "tr9999*") -or (name -g
t "wn0000*" -and name -lt "wn9999*") ' Error Message: 'Operator Not suppor
ted: ' at position: '95'.,Microsoft.ActiveDirectory.Management.Commands.Ge
tADUser
更新2:这次我单独做了每个人,他们工作了吗?我不明白为什么我的主过滤器不起作用,除非它们被单独过滤
其中一些:
$wIDs = Get-Aduser -filter {(name -gt "w30000*" -and name -lt "w99999*")} -Properties * | Select name, SurName, DisplayName, enabled
$weIDs = Get-Aduser -filter {(name -gt "we0000*" -and name -lt "we9999*")} -Properties * | Select name, SurName, DisplayName, enabled
foreach ($user in $wIDs) {
if ($user.DisplayName -like "*-DISABLED"){
#$groups = get-Adgroup -properties managedby -filter {managedby -eq $user.name}
write-host "User ID: " $user.name
write-host "Last Name: " $user.SurName
write-host "Display Name: " $user.DisplayName
write-host $break1 "Next User:" $break1
}#end of if statement
}#end foreach
foreach ($user in $weIDs) {
if ($user.DisplayName -like "*-DISABLED"){
#$groups = get-Adgroup -properties managedby -filter {managedby -eq $user.name}
write-host "User ID: " $user.name
write-host "Last Name: " $user.SurName
write-host "Display Name: " $user.DisplayName
#write-host "Managed Groups: " $groups
write-host $break1 "Next User:" $break1
}#end of if statement
}#end foreach
这些打印正确,无错误????
答案 0 :(得分:2)
这对你有用吗?
Get-Aduser -filter {name -gt "w3000" -and name -lt "w9999"}
编辑:
鉴于新要求:
$prefixes = "cr","cm","ec","fm","in","md","np","ps","sf","tr","ac","do","ev","ia","le","mk","pb","re","so","wn","we"
#Create filter for searches
$DNFilters = $prefixes -replace '^','(Name=' -replace '$','*)'
$Filter = "(|$DNFilters)"
Get-ADUser -LDAPFilter $Filter -Properties Enabled |
Where-Object {$_.name -like '??[0-9][0-9][0-9][0-9]'}
这将允许您使用早期过滤仅返回名称与前缀匹配的帐户。后期过滤器(Where-Object)
将过滤掉那些不具有4位数的过滤器。
编辑: 这一行:
$DNFilters = $prefixes -replace '^','(Name=' -replace '$','*)'
获取每个前缀,并预先绑定字符串'(Name='
,然后将字符串'*)'
附加到其中。结果是,例如,前缀&#39; cr&#39;将成为'(Name=cr*)'
。这是以&#39; cr&#39;。
然后这一行:
$Filter = "(|$DNFilters)"
将这些内容包裹在&#39;(&#39;和&#39;)&#39; 。 &#39; |&#39;在LDAP过滤器中意味着&#39; OR&#39;生成的过滤器如下所示:
(|(Name=cr*) (Name=cm*) (Name=ec*) (Name=fm*) (Name=in*) (Name=md*) (Name=np*) (Name=ps*) (Name=sf*) (Name=tr*) (Name=ac*) (Name=do*) (Name=ev*) (Name=ia*) (Name=le*) (Name=mk*) (Name=pb*) (Name=re*) (Name=so*) (Name=wn*) (Name=we*))
所有单独的LDAP过滤器都组合在一起成为一个过滤器。
在这一行:
Where-Object {$_.name -like '??[0-9][0-9][0-9][0-9]'}
??
表示&#34;任意两个字符&#34;。在通配符匹配(-like
)中,?
表示&#34;任何单个字符&#34;。
答案 1 :(得分:2)
出于性能原因,首选使用-Filter
,因为Get-ADUser
不会返回所有用户对象。但是,在这种情况下,我认为由于条件标准的数量,基于事实的正则表达式可能更适合这一点。因此,除非你有10个1000的用户,否则这应该可以正常工作。此外,您正在撤消所有用户属性,但之后只取4个。这本身就是一个性能损失。唯一不违约的是Enabled
,所以我们一定会接受。
$prefixes = "cr","cm","ec","fm","in","md","np","ps","sf","tr","ac","do","ev","ia","le","mk","pb","re","so","wn","we","w[3-9]"
$regex = "^($($prefixes -Join "|"))\d*"
$results = Get-ADUser -Filter * -Properties Enabled,DisplayName |
Where-Object{$_.Name -match $regex} |
Select Name,SurName,DisplayName,Enabled
$results
基本上构建一个正则表达式匹配字符串,其中名称必须以两个字符组中的一个开头*(w30000标准与另一个不同,这就是为什么最后你看到w[3-9]
表示“w” “后跟一个”3到9“的数字,后跟一些数字。该字符串是根据$prefixes
数组构建的,您可以根据需要添加和删除。
这可能不符合您的确切标准,但可以根据匹配的内容轻松更改。非常确定-lt
,-gt
等不支持通配符。认为它将它们视为字面星号字符。 -like
和-notlike
支持通配符。
是否存在具有不同2个字符前缀的帐户?如果不是这样,可以用一个非常简单的正则表达式来完成,例如^\w{2}\d+
,它是2个字符,后跟至少一个数字。