A collection of Powershell Scripts
A simple script to send emails through a remote SMTP server
Used to test an outgoing mail server and send a test message… or just send any email.
$EmailTo = ""
$EmailFrom = ""
$Subject = "Test"
$Body = "Test Body"
$SMTPServer = ""
$SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$Subject,$Body)
$SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, 587)
$SMTPClient.EnableSsl = $true
$SMTPClient.Credentials = New-Object System.Net.NetworkCredential("[user]", "[password]");
$SMTPClient.Send($SMTPMessage)
Create a scheduled task from Powershell
Create a recurring task
This will run every day at 5am. The $taskExists conditional is so that it can be overwritten if it already existed with the new settings.
$userName = "${env:computername}\Administrator"
$taskName = "RefreshSQLData"
$taskExists = Get-ScheduledTask | Where-Object {$_.TaskName -like $taskName }
echo $userName
if($taskExists) {
Unregister-ScheduledTask -TaskName $taskName -Confirm:$False
}
$action = New-ScheduledTaskAction -Execute "n:\eaccountsRestApi\eMigrateToSql64.exe"
$trigger = New-ScheduledTaskTrigger -Daily -At 5am
$settings = New-ScheduledTaskSettingsSet -Hidden -AllowStartIfOnBatteries -DontStopIfGoingOnBatteries -StartWhenAvailable -RunOnlyIfNetworkAvailable
$principal = New-ScheduledTaskPrincipal -UserId ((Get-CimInstance –ClassName Win32_ComputerSystem | Select-Object -expand Name) + "\Administrator")
$task = New-ScheduledTask -Action $action -Trigger $trigger -Settings $settings -Principal $principal
Register-ScheduledTask $taskName -InputObject $task
Run it for one day at 5am every 30 minutes
$trigger = New-ScheduledTaskTrigger -Once -At 5am -RepetitionDuration (New-TimeSpan -Days 1) -RepetitionInterval (New-TimeSpan -Minutes 30)
Register it as a user/password
Depending on the privilege of the user running the script, password may not be needed.
$task = New-ScheduledTask -Action $action -Trigger $trigger -Settings $settings
Register-ScheduledTask $taskName -InputObject $task -User $username -Password $password