active directory - Powershell script for Soon-to-expire AD users -


so basically, have here script scan csv imports, , every entry in spreadsheet, except people in random.domain, find managers email address , send automated email manager telling them user xyz expire soon, , need it.

if managers email unavailable reason, defaults sending email me. script works well.

the problem running is, want make 1 email sent each manager, despite multiple users (or entries) spreadsheet, list them manager.

i.e. if joe bloggs has manager aaron t , jane doe has manager aaron t, aaron t 2 emails, 1 email each user.

my question:

is there easy way send 1 email per manager, if manager has multiple users reporting them 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 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 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 test instead of sending mass emais time write-host $content } 

updated new script requested.... still having issues.

is there easy way send 1 email per manager, if manager has multiple users reporting them expire?

for need defer e-mail processing. collect users in hashtable, e.g. 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 actual e-mail processing outside loop:

foreach ($emailad in $expiringusers.keys) {   $dn1 = $expiringusers[$emailad]["dn1"]   $man = $expiringusers[$emailad]["man"]   $aet = $expiringusers[$emailad]["aet"]   $subject = "account 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 $man" } 

note simplicity reasons above code records expiry date of first user. if want expiry date of each user recorded separately, you'll have take additonal steps, e.g.

$expiringusers[$emailad]["expiry"] += @{   "name" = $dn;   "date" = $aet; } 

instead of

$expiringusers[$emailad]["dn"] += $dn 

Comments

Popular posts from this blog

c# - DetailsView in ASP.Net - How to add another column on the side/add a control in each row? -

javascript - firefox memory leak -

Trying to import CSV file to a SQL Server database using asp.net and c# - can't find what I'm missing -