Advertisement
Guest User

Untitled

a guest
Sep 2nd, 2015
291
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.16 KB | None | 0 0
  1. $ErrorActionPreference = "Stop"
  2. $originalpath = "C:\watch\Orgin"
  3. $originalfile = "AAA_TestFile.docx"
  4. $witnesspath = "C:\watch\Witness"
  5. $witnessfile = $originalfile
  6.  
  7.  
  8.  
  9.  
  10.  
  11. ## SEND MAIL FUNCTION
  12. function sendMail($s, $to) {
  13. $smtpServer = "mail.nowhere.com"
  14. $smtpFrom = "alert@nowhere.com"
  15. $smtpTo = $to
  16.  
  17. $messageSubject = $s[0]
  18. $message = New-Object System.Net.Mail.MailMessage $smtpfrom, $smtpto
  19. $message.Subject = $messageSubject
  20. $message.IsBodyHTML = $false
  21. $message.Body = $s[1]
  22.  
  23. $smtp = New-Object Net.Mail.SmtpClient($smtpServer)
  24. $smtp.Send($message)
  25. }
  26.  
  27.  
  28.  
  29.  
  30.  
  31. ##Check original files
  32. try {
  33. $origin = Get-Content "$originalpath\$originalfile"
  34. $witness = Get-Content "$witnesspath\$witnessfile"
  35. }
  36. catch
  37. {
  38. ## Probably a file not found error
  39. $subject = "Error logged on $Witnesspath\$Witnessfile by $env:username on $env:computername"
  40. $body = "The original or witness file has not been found. Aborting monitor script."
  41. $email =@($subject,$body)
  42. sendMail -s $email -to "ben22@nowhere.com"
  43. Exit
  44. }
  45.  
  46.  
  47.  
  48.  
  49.  
  50. ## If files don't match, then Send messaged and quit
  51. if (Compare-Object $origin $witness){
  52. ## files don't match
  53. $subject = "Error logged on $witnesspath\$witnessfile by $env:username on $env:computername"
  54. $body = "The original file does not match the witness file. Aborting monitor script."
  55. $email =@($subject,$body)
  56. sendMail -s $email -to "ben22@nowhere.com"
  57. Exit
  58. }
  59.  
  60.  
  61.  
  62.  
  63.  
  64. ## CREATE WATCHER ON DIRECTORY
  65. $watcher = New-Object System.IO.FileSystemWatcher
  66. $watcher.Path = $witnesspath
  67. $watcher.IncludeSubdirectories = $false
  68. $watcher.EnableRaisingEvents = $false
  69. $watcher.NotifyFilter = [System.IO.NotifyFilters]::LastWrite -bor [System.IO.NotifyFilters]::FileName
  70.  
  71.  
  72.  
  73.  
  74.  
  75. ## Execute Watcher
  76. while($TRUE){
  77. $result = $watcher.WaitForChanged([System.IO.WatcherChangeTypes]::Changed `
  78. -bor [System.IO.WatcherChangeTypes]::Renamed `
  79. -bor [System.IO.WatcherChangeTypes]::Deleted `
  80. -bor [System.IO.WatcherChangeTypes]::Created, 1000);
  81.  
  82. if ($result.TimedOut){
  83. continue;
  84. }
  85.  
  86. if ($result.Name -eq $witnessfile) {
  87. ### Make sure the files do not match
  88. try {
  89. $origin = Get-Content "$originalpath\$originalfile"
  90. $witness = Get-Content "$witnesspath\$witnessfile"
  91. if (Compare-Object $origin $witness){
  92. ## files don't match
  93. $body = "Witness file $witnesspath\$witnessfile on $env:computername has been modified."
  94. }
  95. }
  96. catch {
  97. ## file deleted
  98. $body = "Witness file $witnesspath\$witnessfile on $env:computername has been deleted"
  99. }
  100. finally {
  101. ## scorched earth - disconnect all shares
  102. Stop-Service "LanmanServer" -force
  103. $subject = "EMERGENCY ON FILE SERVER -- $Witnesspath\$Witnessfile by $env:username on $env:computername"
  104. $email =@($subject,$body)
  105. sendMail -s $email -to "ben22@nowhere.com"
  106. sendMail -s $email -to "5555555555@txt.bell.ca"
  107. Exit
  108. }
  109.  
  110. }
  111.  
  112. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement