gpt4 book ai didi

powershell - 您将如何创建一个脚本来比较两个系统用户的电子邮件地址

转载 作者:行者123 更新时间:2023-12-02 23:18:03 24 4
gpt4 key购买 nike

我正在尝试将1个系统中的用户列表与一组Active Directory成员进行比较。

我以相反的顺序工作,但这是错误的!

CSV是一个人的名字和他们的电子邮件地址的列表(列:“电子邮件”)

#Script to compare Program Users to AD Group

$Date = Get-Date -f MM-dd-yyyy
$ProgramList = Import-CSV -Path "C:\Scripts\Program_users_list_$Date.csv" | select -ExpandProperty "Email"| Where-Object {$_}
$ADGroupMembers = Get-ADGroupMember -Identity "Program Users" | select SamAccountName

$ADEmail = @()
$Found = 0
$NotFound = 0
$Missing = @()

ForEach ($member in $ADGroupMembers){

$ADEmail += Get-ADuser -Identity $($member.samaccountname) -Properties emailaddress | select emailaddress

}

ForEach ($i in $ProgramList){

If ($i -contains $ADEmail){
#Write-Host "I found " $i
$Found = $Found + 1
}
Else {
#Write-Host "I did not find " $i
$NotFound = $NotFound + 1
$Missing += $i
}

}
Write-Host "I found $found Group Members, but I cannot find: $Notfound in the Program List"
$Missing

Write-Host "Program Count: "$ProgramList.count

Write-Host "Group Count: "$ADEmail.count

当我运行脚本时,$ ProgramList的每个成员都会放入$ NotFound中。

我的If语句一定是错误的,但是我不确定我缺少什么...

我认为If陈述是:
如果在Active Directory组中找到了ProgramList CSV的这1个电子邮件地址,请将其放入$ Found变量中。
如果不是,请将其放入$ NotFound变量。

当我查看$ ADEmail变量中的内容时,我看到列标题“emailaddress”。我不知道这是否重要,但除此之外,我还能看到70个电子邮件地址的列表。

程序CSV = 75电子邮件地址。

广告组= 70个电子邮件地址

然后,我希望脚本告诉我未找到的内容-$ Missing。

最佳答案

您可以将所有对帐单组合在一起,以获取已找到和未找到的电子邮件地址,如下所示:

假设:$ ProgramList包含电子邮件地址列表。

$ADEmail = (Get-ADGroupMember 'Program Users' | Get-ADUser -Properties EmailAddress).EmailAddress

# Check if $ProgramList contains and not contains the list you generated above.
$Found = $ADEmail | ? { $ProgramList -contains $_ } # Intersection of two lists.
$NotFound = $ADEmail | ? {$ProgramList -notcontains $_ }

# You can print the count or the entire lists of email addresses ($Found and $NotFound)
Write-Output "Found $($Found.Count) Email addresses"
Write-Output "Not Found $($NotFound.Count) Email Addresses"

编辑

另一种方法是
$Found = $ProgramList | ? { $ADEmail -contains $_ } # Intersection of two lists.
$NotFound = $ProgramList | ? {$ADEmail -notcontains $_ }

关于powershell - 您将如何创建一个脚本来比较两个系统用户的电子邮件地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/59755440/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com