问题描述
如何获取包含小写字母的广告属性,例如:Wa,Co,ny,la
使用:
Get-ADUser -Properties State -Filter { (Enabled -eq $true) -and (State -cmatch '^[a-z]') } | Select SamAccountName,State | Out-GridView
错误是:
Get-ADUser : Error parsing query: ' (Enabled -eq $true) -and (State -cmatch '^[a-z]') ' Error Message: 'Operator Not supported: -cmatch' at position:
'34'.
At line:1 char:1
+ Get-ADUser -Properties State -Filter { (Enabled -eq $true) -and (Stat ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : ParserError: (:) [Get-ADUser],ADFilterParsingException
+ FullyQualifiedErrorId : ActiveDirectoryCmdlet:Microsoft.ActiveDirectory.Management.ADFilterParsingException,Microsoft.ActiveDirectory.Managemen
t.Commands.GetADUser
解决方法
该错误明确指出不支持运算符-cmatch。在这种情况下,过滤器由AD提供程序处理,因此不支持所有Powershell运算符。请与每个特定的PS提供商联系。这是您可以使用的列表。 https://adsecurity.org/?p=297
,除了Doug Maurer's helpful answer外,您不能在过滤器中使用-match
或-cmatch
比较运算符。
另外,过滤器实际上应该是字符串,而不是脚本块({..}
)。
在您的情况下,应在过滤Where-Object
属性后添加一个Enabled
子句以使用正则表达式:
Get-ADUser -Properties State -Filter "Enabled -eq 'True'" |
Where-Object { $_.State -cmatch '[a-z]' } |
Select SamAccountName,State | Out-GridView