Advertisement
bdill

PowerShell_template

Feb 20th, 2020 (edited)
215
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Desc: PowerShell template
  2. # Auth: Brian Dill 2021-01-26
  3. # License: Shoutout-ware.  If you find this script useful give me a shout out on Twitter @bdill
  4. # URL: https://pastebin.com/2srZrGqX
  5. # Other useful stuff: https://pastebin.com/u/bdill
  6. # Upd: 2022-05-04 bdill
  7.  
  8. Clear-Host
  9. $Script:error.clear()
  10. #-------------------------------------------------------------------------------
  11. # CONFIG PARAMS
  12. #-------------------------------------------------------------------------------
  13. $Server        = "Server01"
  14. $Database      = "MyDatabase"
  15. $ScriptName    = "My_script.ps1"
  16. $LocalPath     = "D:\tmp\"
  17. $RemotePath    = "\\MyServer\MyShare\MyFolder"
  18. $SessionLogContainer = ""  #Holding tank for all session LogLine data
  19. $CurDate       = Get-Date -Format yyyy-MM-dd_hh.mm.ss
  20. $StartTime     = Get-Date
  21. $sbExec        = [System.Text.StringBuilder]::New()
  22. #-------------------------------------------------------------------------------
  23. # Functions
  24. #-------------------------------------------------------------------------------
  25. function LogLine {
  26.     param ([string]$s )
  27.     $d2 = Get-Date -format "yyyy-MM-dd HH:mm:ss";
  28.     $s = "$d2 - $s";
  29.     Write-Host "$s";
  30.     Echo "$s" | Out-File $LogFile -append;
  31.     $script:SessionLogContainer += $s + "`n"
  32. }
  33. #-------------------------------------------------------------------------------
  34. # INLINE CODE
  35. #-------------------------------------------------------------------------------
  36.  
  37. # inline code here
  38.  
  39.  
  40. #-------------------------------------------------------------------------------
  41. # Email results
  42. $smtp = "mail.MyCompany.com"  
  43. $from = "Server01 <ITStaff@MyCompany.com>"  
  44. $to = "user@MyCompany.com"  
  45. $subject = "[PSOps]: SUCCESS - $ScriptName completed $CurDate"
  46. If ($Script:error) {
  47.     Logline "The following error(s) were received:"
  48.     Logline $Script:error
  49.     #$Script:error | Out-File $LogFile -append
  50.     $subject = "[PSOps]: ERROR - $ScriptName failed $CurDate"
  51. }
  52. #send-MailMessage -SmtpServer $smtp -From $from -To $to -Subject $subject -Body "$SessionLogContainer"
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement