Advertisement
Werezwolf

Powershell Example [WIP]

Jun 19th, 2017
66
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. #Config
  2. Set-ExecutionPolicy RemoteSigned -Scope LocalMachine
  3. Set-StrictMode -Version latest
  4.  
  5. #Misc Variables
  6. #Define your Variables includeing what type they are! Unless its calling apon a function.
  7. $date = Get-Date
  8. $scriptName = [string]"Test"
  9. $dateformat = [string]$date.Year+$date.Month+$date.Day #Format for your Dates
  10. $timeformat = [string]$date.ToShortTimeString() #Format for your Time
  11. $filePath = [string]"\\$site-srv-sbs\D$\test\Logs\"
  12. $fileName = [string]"$dateformat`_$env:UserName.txt"
  13. $txtBody = [string]""
  14. $ipaddress = Test-Connection -ComputerName $env:ComputerName -Count 1  | Select IPV4Address
  15. # $username = [string]""
  16. # $password = [string]""
  17.  
  18. #Define New Arrays
  19. $log = New-Object System.Collections.ArrayList
  20.  
  21. #Run Once Events - Anything that throws an error if we run it a second time.
  22. New-EventLog -LogName Application -Source "script-"+$scriptName -EA SilentlyContinue
  23.  
  24. #Create PSCredential
  25. # $secureString = ConvertTo-SecureString $password -AsPlainText -Force
  26. # $pSCredentialEmail = New-Object System.Management.Automation.PSCredential ($username, $SecureString)
  27.  
  28. #Do Stuff
  29. $log.Add("`r`n We did Stuff Yay`r`n`r`n")
  30.  
  31. #Send an Email
  32. $errorNo = [int]0
  33. do  {
  34.     Try {Send-MailMessage -from $email[0] -to $email[1] -SmtpServer $email[2] -Credential $email[3] -Subject $email[4] -Priority High -DeliveryNotificationOption Never -body $email[5] -ErrorAction Stop}
  35.     Catch {
  36.         $errorNo ++
  37.         $log.Add("`r`n Try:$errorNo - $_.Exception `r`n`r`n")
  38.         break
  39.         }
  40.     Start-Sleep -s 60
  41.     }until ($errorNo -ge 5)
  42.  
  43. #Post Script & Exit
  44. foreach($string in $log){$txtBody = $txtBody+$string}
  45. out-file -filePath (Join-Path $filePath $fileName) -append -width 200 -inputObject $txtBody
  46. break
  47.  
  48. #References
  49. #Automatic Variables - https://ss64.com/ps/syntax-automatic-variables.html
  50. #Error Suppresion `- append -EA SilentlyContinue
  51. #Time and Date https://technet.microsoft.com/en-us/library/ff730960.aspx
  52. #SS64.com https://ss64.com/ps/
  53. #$ENV: http://windowsitpro.com/powershell/powershell-one-liner-getting-local-environment-variables
  54. #PSCredential object: https://blogs.msdn.microsoft.com/koteshb/2010/02/12/powershell-how-to-create-a-pscredential-object/
  55. #Arrays:  https://stackoverflow.com/questions/44490999/powershell-net-arrays-cannot-index-into-a-null-array/44500684#44500684
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement