IsraelTorres

ProcessingMail.ps1

Mar 14th, 2012
178
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # original pastebin link: https://pastebin.com/w1F15Ptf
  2. # related father link: http://www.reddit.com/r/PowerShell/comments/qvk0d/powershell_fork/
  3. #Hash tables to store results of previous MX checks
  4. $positiveMX = @{}
  5. $negativeMX = @{}
  6.  
  7. #Adding this just so you have some visual for how far along you are.  It will slow it down slightly, so if you're
  8. #purely worried about speed (or not running it interactively), remove this and the 4 $count lines in the loop
  9. $count = 0
  10.  
  11. #Prep the CSV with headers
  12. "domain,address" | set-content c:\temp\log.txt
  13.  
  14. #Read the file.  Using pipelining to avoid storing the entire thing in memory
  15. get-content "C:\temp\email.txt" | foreach {
  16.     $count++
  17.     if ($count%500 -eq 0) {
  18.         write-host $count -foregroundcolor black -backgroundcolor white
  19.     }
  20.  
  21.  
  22.     $domain= $_.split("@")[1]
  23.  
  24.     #If the MX record passed the test previously, go ahead and pass it through for writing
  25.     if ($positiveMX.ContainsKey($domain)) {
  26.         "$domain,$_"
  27.     #Else, check to see if we found it before but disliked it, and if so do nothing
  28.     } elseif ($negativeMX.ContainsKey($domain)) {
  29.     #Else, haven't seen it before, need to do an MX Lookup
  30.     } else {
  31.         #Note that I changed this from '*yahoo.com' to '*yahoo*', since I tried using yahoo.com to test, and that ended with yahoodns.net.
  32.         #If you know for sure that you want *yahoo.com, change it back
  33.         $mx = nslookup -type=mx $domain | where {$_ -like '*yahoo*'}
  34.  
  35.         #Found it, adding it to the positive list and passing through for writing
  36.         if($mx){
  37.             "$domain,$_"
  38.             $positiveMX.Add($domain,"")
  39.         #Didn't find it, adding it to the bad list
  40.         } else {
  41.             $negativeMX.Add($domain,"")
  42.         }
  43.     }
  44. } | add-content c:\temp\log.txt
Add Comment
Please, Sign In to add comment