【发布时间】:2020-02-08 14:12:49
【问题描述】:
我正在尝试更改以下 Powershell 脚本,使其不会从列表中搜索特定 OU 中的任何帐户。
上报的AD账号为不具备以下属性的AD账号:
Phone Number
Mobile/Cellphone Number
Job Title
Email Address.
Company
City
office address
这是我目前的脚本:
$filter = "(Enabled -eq 'true') -and (mail -notlike '*') -and (company -notlike '*') -and "
$filter += "(l -notlike '*') -and (physicalDeliveryOfficeName -notlike '*') -and "
$filter += "(title -notlike '*') -and (telephoneNumber -notlike '*') -and (mobile -notlike '*')"
$properties = @('mail', 'physicalDeliveryOfficeName', 'Company', 'DisplayName', 'title', 'SamAccountName', 'CanonicalName', 'lastlogondate', 'mobile', 'telephoneNumber', 'l', 'Whencreated', 'DistinguishedName')
$domainDN = (Get-ADDomain).DistinguishedName
$excludeOUs = @(
'OU=Disabled Users,DC=GlobalCorp,DC=com'
'OU=GlobalCorp Testing,DC=GlobalCorp,DC=com'
'OU=Admin Accounts,OU=GlobalCorp Global,DC=GlobalCorp,DC=com'
'OU=Service Accounts,OU=GlobalCorp Global,DC=GlobalCorp,DC=com'
'OU=Shared Mailboxes,OU=GlobalCorp Global,DC=GlobalCorp,DC=com'
)
$reExcludeOUs = '(?:{0})$' -f ($excludeOUs -join '|')
Get-ADUser -Filter $filter -Properties $properties -SearchBase $domainDN |
Where-Object {
($_.DistinguishedName -notmatch $reExcludeOUs) -and
($_.SamAccountName -notmatch '^(SM_|Temp|HealthMailbox)|SVC|Test|admin|\$') -and
($_.DisplayName -notmatch 'Admin|Calendar|Room')
} |
Select-Object -Property `
DisplayName,
Company,
Title,
TelephoneNumber,
Mobile,
PhysicalDeliveryOfficeName,
SamAccountName,
Mail,
@{n = 'OU'; e = { $_.CanonicalName.Remove($_.CanonicalName.LastIndexOf($_.Name) - 1) } },
@{n = 'CN'; e = { Split-Path $_.CanonicalName -Parent } },
@{n = 'ParentContainer'; e = { $_.DistinguishedName -replace '^CN=.*?(?=CN|OU)' } },
LastLogondate,
WhenCreated |
Sort-Object OU |
ConvertTo-HTML |
Set-Variable HTMLBody
Send-MailMessage -SmtpServer SMTP.GlobalCo.com -From "$env:COMPUTERNAME@$env:userdnsdomain" -To Admin@MSP.com -Subject "AD User Incomplete report as at $((Get-Date).ToString('dd-MM-yyyy'))" -Body ($HTMLBody -join '`n') -BodyAsHTML
如何修改上面的 Where-Object 过滤器?
【问题讨论】:
-
-notcontains表示右侧的 item 不在左侧的 collection 中......但是您将它们颠倒了。 [grin] 试试-notin。 -
已经改成使用($_.DistinguishedName -notin "$excludeOUs"),结果还是一样?
-
您的变量
$excludeOUs导致 OU DN 以逗号结尾。只需用逗号分隔值即可创建数组,例如$excludeOUs = 'OU=Disabled Users,DC=GlobalCorp,DC=com', 'OU=GlobalCorp Testing,DC=GlobalCorp,DC=com', .. -
@SeniorSystemsEngineer - 您发布的代码仍然显示>>>
($_.DistinguishedName -notcontains $excludeOUs)咧嘴]
标签: powershell scripting active-directory powershell-4.0 windows-scripting