在我们的组织中,有些用户被设置为将电子邮件发送到组织外部的域(邮件用户)。因此,他们的主要电子邮件地址与我们的标准 FQDN 不同。
我想要做的是提取 AD 中所有启用的用户,并将具有我们的主要 FQDN 的用户排序到最后一个列表,将具有其他任何用户排序到另一个列表。
我已经能够让以下内容生成单个列表,但我无法让它按电子邮件类型将其分类为两个列表。
$users = Get-ADUser -Filter {Enabled -eq $true} -SearchBase "Removed" -Properties mail, givenName, initials, sn, title, extensionAttribute3, extensionAttribute4, physicalDeliveryOfficeName, department, extensionAttribute1, employeeid, EmployeeType, whenCreated | where-object {$_.EmployeeType -ne $null -AND $_.mail -ne $null} |
foreach {
new-object psobject -Property @{
Email = $_.mail
firstName = $_.givenName
lastName = $_.sn
middleInitial = $_.initials
jobTitle = $_.title
Department = $_.department
UserID = $_.employeeid
}
} | Select Email,firstName,lastName,middleInitial,jobTitle,Department,UserID
$goodEmail = @()
$badEmail = @()
foreach ($user in $users) {
if($users.'Email' -ne "*@example.com") {
$goodEmail += $user
}
else {
$badEmail += $user
}
}
$goodEmail | Export-CSV $csvOriginal -NoTypeInformation -Append
$badEmail | Export-CSV $badEmails -NoTypeInformation -Append
答案1
我会简化脚本
- 把需要属性放入一个数组中(有是一些未使用)
- 使用
[PSCustomObject]
for 输出(默认保持顺序) - 使用 Where-Object 区分好/坏电子邮件
## Q:\Test\2019\06\12\SU_1447905.ps1
$Props = ('mail','givenName','initials','sn','title','physicalDeliveryOfficeName',
'extensionAttribute1','extensionAttribute3','extensionAttribute4',
'department','employeeid','EmployeeType','whenCreated')
$users = Get-ADUser -Filter {Enabled -eq $true} -SearchBase "Removed" -Properties $Props |
Where-object {$_.EmployeeType -ne $null -AND $_.mail -ne $null} |
ForEach-Object {
[PSCustomObject]@{
Email = $_.mail
firstName = $_.givenName
lastName = $_.sn
middleInitial = $_.initials
jobTitle = $_.title
Department = $_.department
UserID = $_.employeeid
}
}
$goodEmail = $users | Where-Object Email -like '*@Example.com'
$badEmail = $users | Where-Object Email -notlike '*@Example.com'
$goodEmail | Export-CSV $csvOriginal -NoTypeInformation -Append
$badEmail | Export-CSV $badEmails -NoTypeInformation -Append
$csvOriginal
并且$badEmails
是在这个脚本之外定义的。