Windows Powershell

How to send email alerts via Task Scheduler in Server 2012 R2

Posted on Updated on

In Server 2008 R2 task scheduler, the send email feature is available as an option.  And I have used this feature a lot mostly for my phone system servers because I get real time alerts whenever there is a potential issues on switches and servers.

When 2012R2 came out, the send mail feature was not longer available.

I am making this post to help others out there with the same issues and hopefully you will find this post useful to you.

Here are the procedures:

1.  Create a powershell script to send emails, the entries for Attachment, SMTPPort, and UseSSL are optional unless required.

##############################################################################

$From = “alert@email.com”

$To = “user@email.com”

$Cc = “boss@email.com”

$Attachment = “C:\temp\Some random file.txt”

$Subject = “Subject here”

$Body = “Body of the email here”

$SMTPServer = “smtprelay.your.org”

$SMTPPort = “52”

Send-MailMessage -From $From -to $To -Cc $Cc -Subject $Subject -Body $Body -SmtpServer $SMTPServer#-port $SMTPPort -UseSsl #-Credential (Get-Credential) # -Attachments $Attachment

##############################################################################

2.  Next is to create your scheduled task, and under Actions, add the powershell script to send the email.

 

taskscheduler

 

This is very simple yet very useful to anyone that wants to setup a scheduled task that also sends email alerts.  Although you can have SCOM send those alerts, it is still useful if you can do it directly from the server.

Please do not forget to leave me a comment if you see any corrections, or if you have any suggestions.  Or better, if you like this post, please feel free to let me know.  It won’t cost you anything.

Advertisements

Powershell script to remove white-spaces on a CSV file and convert the time entry to 24-hour format

Posted on Updated on

#Here is a script to remove white-spaces on a CSV file and convert the time entry to 24-hour format.

$fileInput = <location and filename of the input file>

$fileOutput = <location and filename of the output file>

$delimeter = ‘,’

$header1 = ‘#Phone,Original Record,Client ID,Sub-Campaign,Device Attempted,Device Locale,Date Time,Last State,Last State Seconds,Call Seconds,Call Result,Best Contact’

# check if input file exist

if (!(test-path $fileinput)) {

write-host “File not found” exit

}

# check if output file already exist, delete file if true

if (test-path $fileoutput) {

Remove-Item $fileoutput

}

# import and build CSV in memory

#note single word IDs work better, I.E phone otherwise they need to be named with quotes

$header = “Phone”,”Original Record”,”Client ID”,”Sub-Campaign”,”Device Attempted”,”Device Locale”,”Date Time”,”Last State”,”Last State Seconds”,”Call Seconds”,”Call Result”,”Best Contact”

$csv = import-csv -path $fileInput -header $header

$csvTest = @();

# Display function for military time

# Input String

# Output String

function DisplayMilitaryDateTime( $InputDateString ){

[System.DateTime]$convertedDate = [System.DateTime]::Parse( $InputDateString );

#5/16/2014 13:00:02

return $convertedDate.ToString(“MM/dd/yyyy HH:mm:ss”);

}

Add-Content -Path $fileOutput -Value $header1

# all the clean and conversion of data would be here

foreach($line in $csv){

$outLine = “”;

# cleanup data and convert it

$outLine += $line.Phone.Trim(” “) + $delimeter ;

$outLine += $line.”Original Record”.Trim(” “)+ $delimeter;

$outLine += $line.”Client ID”.Trim(” “)+ $delimeter;

$outLine += $line.”Sub-Campaign”.Trim(” “)+ $delimeter;

$outLine += $line.”Device Attempted”.Trim(” “)+ $delimeter;

$outLine += $line.”Device Locale”.Trim(” “)+ $delimeter;

#note the function call

$outLine += DisplayMilitaryDateTime( $line.”Date Time”.Trim(” “) ) + $delimeter;

#this is added so another ‘,’ is added

$outLine += $delimeter; $outLine += $line.”Last State”.Trim(” “) + $delimeter;

$outLine += $line.”Last State Seconds”.Trim(” “) + $delimeter;

$outLine += $line.”Call Seconds”.Trim(” “) + $delimeter;

$outLine += $line.”Call Result”.Trim(” “) + $delimeter;

$outLine += $line.”Best Contact”.Trim(” “);

 

# manually build the output line and append it to the file

Add-Content -Path $fileOutput -Value $outLine }

 

#finally export it

# $csv | Export-Csv -Path $fileOutput -Delimiter ‘,’ -Encoding UTF8 -NoTypeInformation

 

#delete input file after successful conversion

Remove-Item $fileinput