Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #Config
- Set-ExecutionPolicy RemoteSigned -Scope LocalMachine
- Set-StrictMode -Version latest
- #Misc Variables
- #Define your Variables includeing what type they are! Unless its calling apon a function.
- $date = Get-Date
- $scriptName = [string]"Test"
- $dateformat = [string]$date.Year+$date.Month+$date.Day #Format for your Dates
- $timeformat = [string]$date.ToShortTimeString() #Format for your Time
- $filePath = [string]"\\$site-srv-sbs\D$\test\Logs\"
- $fileName = [string]"$dateformat`_$env:UserName.txt"
- $txtBody = [string]""
- $ipaddress = Test-Connection -ComputerName $env:ComputerName -Count 1 | Select IPV4Address
- # $username = [string]""
- # $password = [string]""
- #Define New Arrays
- $log = New-Object System.Collections.ArrayList
- #Run Once Events - Anything that throws an error if we run it a second time.
- New-EventLog -LogName Application -Source "script-"+$scriptName -EA SilentlyContinue
- #Create PSCredential
- # $secureString = ConvertTo-SecureString $password -AsPlainText -Force
- # $pSCredentialEmail = New-Object System.Management.Automation.PSCredential ($username, $SecureString)
- #Do Stuff
- $log.Add("`r`n We did Stuff Yay`r`n`r`n")
- #Send an Email
- $errorNo = [int]0
- do {
- 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}
- Catch {
- $errorNo ++
- $log.Add("`r`n Try:$errorNo - $_.Exception `r`n`r`n")
- break
- }
- Start-Sleep -s 60
- }until ($errorNo -ge 5)
- #Post Script & Exit
- foreach($string in $log){$txtBody = $txtBody+$string}
- out-file -filePath (Join-Path $filePath $fileName) -append -width 200 -inputObject $txtBody
- break
- #References
- #Automatic Variables - https://ss64.com/ps/syntax-automatic-variables.html
- #Error Suppresion `- append -EA SilentlyContinue
- #Time and Date https://technet.microsoft.com/en-us/library/ff730960.aspx
- #SS64.com https://ss64.com/ps/
- #$ENV: http://windowsitpro.com/powershell/powershell-one-liner-getting-local-environment-variables
- #PSCredential object: https://blogs.msdn.microsoft.com/koteshb/2010/02/12/powershell-how-to-create-a-pscredential-object/
- #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