Advertisement
Guest User

Untitled

a guest
Mar 26th, 2019
63
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 1.07 KB | None | 0 0
  1. $FileSystemWatcher = New-Object System.IO.FileSystemWatcher
  2. $FileSystemWatcher.Path = "C:\temp"
  3.  
  4. Register-ObjectEvent -InputObject $FileSystemWatcher -EventName Created -Action {
  5.  
  6. $Object = "{0} was {1} at {2}" -f $Event.SourceEventArgs.FullPath,
  7. $Event.SourceEventArgs.ChangeType,
  8. $Event.TimeGenerated
  9. $Event.SourceEventArgs.FullPath
  10.  
  11.  
  12. ############################ Get the last file in the directory and pull its contents
  13. $filepath = "C:\temp"
  14. $filename = (get-content $filepath | sort CreationTime -Descending | select -last 1).Name
  15. $Join = Join-Path -path $filepath -ChildPath $filename
  16. $content = Get-Content -Raw $join | out-string
  17.  
  18. ############################ Send the message
  19. $EmailParams = @{
  20. credential = Import-CliXml -Path 'C:\temp\cred.xml' # this needs to be stored prior with "Get-Credential | Export-CliXml -Path C:\path\cred.xml"
  21. From = "email@gmail.com"
  22. To = "email@gmail.com"
  23. Subject = $Object
  24. Body = $content
  25. SMTPServer = "smtp.gmail.com"
  26. Port = "587"
  27. }
  28. Send-MailMessage @EmailParams -UseSsl
  29. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement