【问题标题】:Find Active Accounts No Longer Employed查找不再受雇的活跃账户
【发布时间】:2020-12-30 00:27:52
【问题描述】:

我想为员工离职时没有通知 IT 创建一个支持。我们每月收到一份 csv 格式的活跃员工名册,包括employeeID 字段。我计划每天要求这个,他们可以通过他们的 HRIS 系统提供。我想最终通过任务调度程序自动化一个副本,从 HR 将文件放置到我们运行脚本的位置 - 然后启动下面的脚本以针对复制的 csv 运行。现在我只需要帮助来正确设置 Powershell 脚本。

我想做的是:

  • 在 AD 中搜索具有员工 ID 的员工(不返回空白)
  • 导入包含一列员工 ID 的 csv
  • 根据 AD 结果中的 csv 执行搜索
  • 对于 AD 中存在但 csv 中不存在的任何员工 ID,请将电子邮件地址发送到地址“用户 $_.Name 不是员工

EmployeeID 是我们最可靠的字段,因为 HR 没有 SamAccountNames 列表,人们结婚了,姓名和电子邮件地址也会发生变化。我确实想要自动化禁用帐户的过程,因为这将启用一种机制,让 HR 中的流氓行为者禁用每个人的帐户。

我的脚本全错了,但这是我开始的思考过程:

# Return employees with an employeeID field populated - we're not concerned with service accounts, consultants, etc
#
$adusers = Get-ADUser -searchbase "OU=MyOU,DC=MyCompany,DC=COM" -Filter {employeeID -like "*" -and enabled -eq $true} -Properties employeeID
#
# Import active roster
#
$csv = Import-Csv C:\temp\activeroster-test.csv

    foreach($emp in $csv)
    {
    $csvID = $csv.employeeID
    $csvName = $csv.Name
                    
    if($adusers.EmployeeID -notlike $csvID)
      {
            echo '**not found in roster**'
            echo $ADusers.Name
       }
    } 

我没有进入电子邮件通知部分,因为我似乎连这个都不知道。它只是将我名册中的人恢复到名册中的人数。它是倒退的。救命!

编辑 - 通过电子邮件通知更新:

# Return employees with an employeeID field populated - we're not concerned with service accounts, consultants, etc
$adUsers = Get-ADUser -searchbase "OU=MyOU,DC=Example,DC=COM" -Filter {employeeID -like "*" -and enabled -eq $true} -Properties employeeID

# Email Server info
$SmtpServer = "emailserver.example.com"
$NotificationEmailAddress = "myemail@example.com"

#
# Import active roster
#
$csv = Import-Csv C:\temp\activeroster.csv

foreach ($emp in $adUsers) {
    $csvIDList = $csv.EmployeeID
    if ($emp.EmployeeID -notin $csvIDList) {
    $Body = "The following users are still enabled in Active Directory however not found in the active employee roster " + ($($emp.Name) | out-string)
    Send-MailMessage -From $NotificationEmailAddress -To $NotificationEmailAddress -Subject "Active Accounts Not In Employee Roster" -Priority High -dno onFailure -SmtpServer $SmtpServer -Body $Body
    }
} 

我为每个用户收到一封电子邮件。值得庆幸的是,在我的测试中,我正在做一个小的 OU 和花名册的一个样本子集。呵!有什么建议吗?我想我可能需要创建另一个包含所有结果的变量,是吗?

【问题讨论】:

    标签: powershell active-directory


    【解决方案1】:

    您的脚本并非全错,实际上您已经很接近了。您正在打印 AD 查询中所有返回的用户,每次迭代都作为受影响的用户,而您只是想返回您正在迭代的用户。在与 ID 数组进行比较时,您可以在 HR csv 的列表中使用 -notin

    # Return employees with an employeeID field populated - we're not concerned with service accounts, consultants, etc
    #
    $adUsers = Get-ADUser -searchbase "OU=MyOU,DC=MyCompany,DC=COM" -Filter {employeeID -like "*" -and enabled -eq $true} -Properties employeeID
    #
    # Import active roster
    #
    $csv = Import-Csv C:\temp\activeroster-test.csv
    # Email Server info
    $smtpServer = "emailserver.example.com"
    $notificationEmailAddress = "myemail@example.com"
    
    [System.Collections.ArrayList]$listOfMissing = @()
    foreach ($emp in $adUsers) {
        $csvIDList = $csv.EmployeeID
        if ($emp.EmployeeID -notin $csvIDList) {
            Write-Output "**Employee $($emp.Name) not found in roster**"
            $listOfMissing.Add($emp.Name)
        }
    }
    
    if ($listOfMissing) {
        $subject = "Active Accounts Not In Employee Roster"
        $body = @"
        The following users are still enabled in Active Directory however not found in the active employee roster:
    
        $($listOfMissing | Out-String)
    "@
    
        $emailParameters = @{
            From       = $notificationEmailAddress
            To         = $notificationEmailAddress
            Subject    = $subject
            Priority   = 'High'
            Dno        = 'onFailure'
            SmtpServer = $smtpServer
            Body       = $body
        }
    
        Send-MailMessage @emailParameters
    
    } else {
        Write-Output 'Email not sent, no users found'
    }
    

    我还可能建议使用另一个脚本对该 csv 进行一些定期自动更新,或者集成到 HR 系统,通常是 Workday 或类似的东西,尽管我知道在我们的职位上我们有时没有这种访问权限。 :)

    邮箱可以check out this SO post.

    【讨论】:

    • 谢谢。我更新了我的帖子,因为我现在正在尝试电子邮件通知。每个结果我都会收到一封电子邮件。你也可以看看吗?非常感谢!
    • 当然,您要做的是建立一个列表,然后将找到的每个用户附加到列表中,然后在您的电子邮件正文中使用该列表。我对我可能会如何做进行了编辑。我添加的“emailParameters”只是将参数传递给函数的更好方法,称为“splatting”
    • 那个脚本更新也很漂亮。这种通知格式更可取,并且允许我按计划为我们现在手动运行的十几个脚本启用通知。非常感谢!
    猜你喜欢
    • 1970-01-01
    • 2014-11-02
    • 1970-01-01
    • 2014-02-16
    • 2022-08-16
    • 1970-01-01
    • 2017-07-27
    • 2013-11-12
    • 1970-01-01
    相关资源
    最近更新 更多