Advertisement
Guest User

LQ##001

a guest
Apr 15th, 2015
440
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.41 KB | None | 0 0
  1. ##CONFIGURE SMTP SETTINGS
  2. $emailSmtpServer = "SERVER_PORT_HERE"
  3. $emailSmtpServerPort = "587"
  4. $emailSmtpUser = "USERNAME_HERE"
  5. $emailSmtpPass = "PASSWORD_HERE"
  6.  
  7. ##CONFIGURE EMAIL SETTINGS
  8. $emailFrom = "person@person.com"
  9. $emailTo = "person@person.com"
  10. $body = "BODY_TEXT_HERE"
  11. $subject = "SUBJECT_TEXT_HERE"
  12.  
  13. ##CONFIGURE SQL CONNECTION SETTINGS
  14. $sqlServer = "SERVER_NAME_HERE"
  15. $sqlDatabase = "DATABASE_NAME_HERE"
  16.  
  17. ##CONFIGURE SQL QUERY
  18. $sqlQuery = @"
  19. SQL_QUERY_HERE
  20. "@
  21.  
  22. ##CONFIGURE FOLDERPATH FOR SAVED FILES
  23. $folderPath = "C:\reportresults\"
  24.  
  25. ##CLEANUP FOLDER LOCATION
  26. Remove-Item $folderPath -recurse
  27.  
  28. ##CREATE FOLDER LOCATION
  29. New-Item $folderPath -type directory
  30.  
  31. ##CONNECT TO DATABASE AND EXECUTE COMMAND
  32. $sqlConnection = New-Object System.Data.SqlClient.SqlConnection
  33. $sqlConnection.ConnectionString = "Server="+$sqlServer+"; Database="+$sqlDatabase+"; Integrated Security=True;"
  34.  
  35. $sqlCommand = New-Object System.Data.SqlClient.SqlCommand
  36. $sqlCommand.CommandText = $sqlQuery
  37. $sqlCommand.Connection = $sqlConnection
  38.  
  39. $sqlAdapter = New-Object System.Data.SqlClient.SqlDataAdapter
  40. $sqlAdapter.SelectCommand = $sqlCommand
  41. $sqlDataSet = New-Object System.Data.DataSet
  42. $sqlAdapter.Fill($sqlDataSet)
  43. $sqlConnection.Close()
  44.  
  45. ##SAVE RESULTS TO FILES
  46. $filePath = $folderPath + "Report.csv"
  47. $sqlDataSet.Tables[0] | Export-Csv -NoTypeInformation $filePath
  48.  
  49. foreach ($Row in $sqlDataSet.Tables[1].Rows)
  50. {
  51. $val = $Row[1] | out-string
  52. $outPath = $folderPath + $row[0] + "_" + $row[2] + ".txt"
  53. $val | Out-File $outPath
  54. }
  55.  
  56. ##ZIP AND SAVE RESULTS TO FILE
  57. set-alias sz "C:\Program Files (x86)\7-Zip\7z.exe"
  58. sz a -t7z C:\reportresults\report.7z c:\reportresults\*
  59. $emailAttachment = "C:\reportresults\report.7z"
  60.  
  61. ##FINALIZE AND SEND EMAIL
  62. $emailMessage = New-Object System.Net.Mail.MailMessage( $emailFrom , $emailTo )
  63. $emailMessage.Subject = $subject
  64. $emailMessage.IsBodyHtml = $true
  65. $emailMessage.Body = $body
  66. $emailMessage.Attachments
  67. $attachment = New-Object System.Net.Mail.Attachment($emailattachment, 'application/x-7z-compressed')
  68. $emailMessage.Attachments.Add($attachment)
  69.  
  70. $SMTPClient = New-Object System.Net.Mail.SmtpClient( $emailSmtpServer , $emailSmtpServerPort )
  71. $SMTPClient.EnableSsl = $false
  72. $SMTPClient.Credentials = New-Object System.Net.NetworkCredential( $emailSmtpUser , $emailSmtpPass );
  73.  
  74. $SMTPClient.Send( $emailMessage )
  75.  
  76. ##CLEANUP FOLDER LOCATION
  77. Remove-Item $folderPath -recurse
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement