我有下面的代码,它为我提供了所有启用帐户的用户,并且描述不像"共享帐户"服务帐户";或";资源帐户";。
Get-ADUser -Filter {(SamAccountName -notlike "nam-svc*") -and (SamAccountName -notlike "nam_svc*") -and (enabled -eq $true) -and (description -notlike "Shared Account*") -and (Description -notlike "service account*") -and (description -notlike "Resource Account*") } -Properties memberof
如何简化我的代码,使其不那么杂乱?
-and
运算符为您提供了跨换行符的自由延续,因此您可以像这样缩进它:
Get-ADUser -Filter {
(enabled -eq $true) -and
(SamAccountName -notlike "nam-svc*") -and
(SamAccountName -notlike "nam_svc*") -and
(description -notlike "Shared Account*") -and
(Description -notlike "service account*") -and
(description -notlike "Resource Account*") } -Properties memberof
如果你有很多额外的参数参数想要传递给Get-ADUser
,我建议结合splatting:
$ADUserParams = @{
Filter = {
(enabled -eq $true) -and
(SamAccountName -notlike "nam-svc*") -and
(SamAccountName -notlike "nam_svc*") -and
(description -notlike "Shared Account*") -and
(Description -notlike "service account*") -and
(description -notlike "Resource Account*")
}
Properties = 'memberof'
SearchBase = "OU=target,DC=domain,DC=tld"
SearchScope = 'subtree'
Server = 'some-specific-DC.domain.tld'
}
Get-ADUser @ADUserParams