简体   繁体   English

适用于即将到期的AD用户的Powershell脚本

[英]Powershell script for Soon-to-expire AD users

So basically, what I have here is a script that will scan a CSV that it imports, and for every entry in the spreadsheet, except for people in the RANDOM.DOMAIN, it will find the managers email address and send an automated email to the manager telling them user XYZ is about to expire soon, and they need to do something about it. 因此,基本上,我在这里拥有的脚本将扫描它导入的CSV,并且对于电子表格中的每个条目,除了RANDOM.DOMAIN中的人员外,它将找到经理的电子邮件地址,并将自动电子邮件发送至经理告诉他们XYZ用户即将过期,因此他们需要为此做些事情。

If the managers email is unavailable for some reason, then it defaults to sending the email to me. 如果经理的电子邮件由于某些原因而无法使用,则默认为将电子邮件发送给我。 This script works well. 该脚本运行良好。

The problem I am running into is, I want to make it so only one email is sent to each manager, despite multiple users (or entries) from the spreadsheet, list them as the manager. 我遇到的问题是,我希望做到这一点,因此尽管电子表格中有多个用户(或条目),但仅向每位经理发送一封电子邮件,将他们列为经理。

Ie if Joe Bloggs has a manager Aaron T and Jane Doe has the manager Aaron T, then Aaron T will get two emails, one email for each user. 例如,如果Joe Bloggs的经理Aaron T和Jane Doe的经理Aaron T,则Aaron T将收到两封电子邮件,每位用户一封电子邮件。

MY QUESTION: 我的问题:

Is there an easy way to only get it to send one email per manager, even if that manager has multiple users reporting to them that are about to expire? 有没有一种简单的方法可以让每个经理仅发送一封电子邮件,即使该经理有多个要向他们报告的用户即将到期?

$datai = Import-Csv "Soon-to-expire User Accounts22.csv" | select 'Display Name',Manager,'Domain Name','Account Expiry Time'
Connect-QADService -Service another.DC | Out-Null
$expiringUsers = @{}



foreach ($i in $datai) {
        $dn = $i.'Display Name'
        $dn1 = $i.'Domain Name'
        $man = $i.'Manager'
        $aet = $i.'Account Expiry Time'
        $subject = "Account about to expire: $dn"



$getmail = get-qaduser "$man" -LdapFilter '(mail=*)' | select mail
$emailAD = $getmail.mail

if ($man -eq "-" -or $man -like 'CN=*' -or $getmail -eq $null -or $man -eq "") {
$man = "Aaron T"
$getmail = get-qaduser "$man" -LdapFilter '(mail=*)' | select mail
$emailAD = $getmail.mail
}



if ($expiringUsers.Contains($emailAD)) {
  $expiringUsers[$emailAD]["dn"] += $dn += "`n"
  $expiringUsers[$emailAD]["aet"] += $aet += "`n"
  $expiringUsers[$emailAD]["man"] += $man += "`n"
} else {
  $expiringUsers[$emailAD] = @{
    #"dn1" = $dn1
    #"aet" = $aet
   #"man" = $man
  # "dn"  = @( $dn )
     }
  }
}


$expiringUsers | fc #as suggested


foreach ($emailAD in $expiringUsers.Keys) {
$dn  = $expiringUsers[$emailAD]["dn"]
$dn1 = $expiringUsers[$emailAD]["dn1"]
$man = $expiringUsers[$emailAD]["man"]
$aet = $expiringUsers[$emailAD]["aet"]
$subject = "Account/s About to Expire!"
$content = @"
Hi,
$dn `n
$dn1 `n
$man `n
$aet `n

$emailAD `n
Technology Services
 "@
 Send-MailMessage -from "aaron@website.com" `
-To $emailAD `
-Subject $subject `
-Body $content `
-Priority high `
-smtpServer "relay.server"


#using this as a test instead of sending mass emais all the time
Write-Host $content
}

UPDATED with the new script as requested.... still having issues. 根据要求使用新脚本进行了更新。...仍然存在问题。

Is there an easy way to only get it to send one email per manager, even if that manager has multiple users reporting to them that are about to expire? 有没有一种简单的方法可以让每个经理仅发送一封电子邮件,即使该经理有多个要向他们报告的用户即将到期?

For this you need to defer e-mail processing. 为此,您需要推迟电子邮件处理。 Collect the users in a hashtable, eg by manager e-mail address: 将用户收集在哈希表中,例如通过经理电子邮件地址:

...

$expiringUsers = @{}

foreach ($i in $datai) {
  If ($i.'Domain Name' -notmatch "RANDOM.DOMAIN") {
    ...
    if ($expiringUsers.Contains($emailAD)) {
      $expiringUsers[$emailAD]["dn"] += $dn
    } else {
      $expiringUsers[$emailAD] = @{
        "dn1" = $dn1
        "aet" = $aet
        "man" = $man
        "dn"  = @( $dn )
      }
    }
  }
}

and move the actual e-mail processing outside the loop: 并将实际的电子邮件处理移到循环外:

foreach ($emailAD in $expiringUsers.Keys) {
  $dn1 = $expiringUsers[$emailAD]["dn1"]
  $man = $expiringUsers[$emailAD]["man"]
  $aet = $expiringUsers[$emailAD]["aet"]
  $subject = "Account about to expire: $($expiringUsers[$emailAD]["dn"])"
  $content = @"
Hi,
...
Technology Services
"@
  Send-MailMessage -from "Test Script - Powershell <email@test.com>" `
    -To "$emailAD" `
    -Subject $subject `
    -Body $content `
    -Priority high `
    -smtpServer servername
  Write-Host "Mail Sent to $man"
}

Note that for simplicity reasons the above code only records the expiry date of the first user. 请注意,为简单起见,以上代码仅记录了第一个用户的到期日期。 If you want the expiry date of each user recorded separately, you'll have to take additonal steps, eg 如果要分别记录每个用户的到期日期,则必须采取其他步骤,例如

$expiringUsers[$emailAD]["expiry"] += @{
  "name" = $dn;
  "date" = $aet;
}

instead of 代替

$expiringUsers[$emailAD]["dn"] += $dn

So I finally decided to revisit this script, after many, many months. 因此,我终于决定在许多月后重新审阅此脚本。 I'm get a little better at PowerShell and while I'm sure this isn't the most effective way to do it, this is something that works for me. 我在PowerShell方面有了一些进步,虽然我确定这不是最有效的方法,但是这对我来说很有用。

I've also changed the input method; 我也改变了输入法。 it pulls the information directly from AD, instead of using a CSV file that used to be generated from an application called 'AD Manager Plus' (Hate it). 它直接从AD提取信息,而不是使用以前从称为“ AD Manager Plus”的应用程序生成的CSV文件(讨厌它)。

Remember, using Quest CMDlets here because we don't have a 2008 environment. 请记住,在这里使用Quest CMDlet,因为我们没有2008环境。 (so using Get-QADUser instead of Get-ADuser) (因此使用Get-QADUser而不是Get-ADuser)

FYI, I have only posted the code here which sorts out the data into separate tables - you can decide how you want to utilize those results. 仅供参考,我只在此处发布了将数据整理到单独表中的代码-您可以决定如何利用这些结果。 For our environment, I have it build an nice HTML table and body, then send it to the appropriate manager to deal with. 对于我们的环境,我让它构建了一个不错的HTML表和主体,然后将其发送给适当的管理器进行处理。

#user data input
$data = get-qaduser -SizeLimit 0 -includedproperties accountexpires | where {$_.AccountExpires -ne $null -and $_.AccountExpires -le ((Get-Date).AddDays(45)) }

#get a list of managers, unique.
$uniqueMan = $data | select Manager -Unique

#foreach manager from $uniqueman
Foreach ($manager in $uniqueman) {
    #create the array variable / clear it out for the next manager.
    $myarray = @()
            #foreach User found in in $data query
            Foreach ($user in $data) {

            #Search the $user's query for people with the same manager as that of the $uniqueman list.
            If ($user.Manager -eq $manager.Manager) {

                    #do what with the result.
                    #add the person to an array
                    $myarray += New-Object psobject -Property @{
                        Name = $user.'Name'
                        UserName = $user.'SAMAccountName'
                        AccountExpires = $user.'AccountExpires'
                        Manager = $user.Manager
                        }


            }


    #for testing, to output the results to an HTML file.
    #$myarray | ConvertTo-Html | Out-File ("C:\test\" + $manager.Manager + ".html")

        }


}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM