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.

How to check and disable Transport agents in Exchange

Posted on

To get the list of Transport agents running on your Exchange server run the command:

Get-TransportAgent

And to disable a specific agent:

Disable-TransportAgent -identity “transport agent name”

How to find the owner of an alias in Exchange

Posted on

To find out who the owner is for an alias or email address in Exchange.  You should be able to run the following exchange powershell command:

get-recipient -results unlimited | where {$_.emailaddresses -match “emailaddresshere”} | select name,emailaddresses,recipienttype

Get status of a Public Folder replication

Posted on Updated on

Run the powershell command

Set-ExecutionPolicy Unrestricted

To run the report and get a list of data objects returned that can be further processed, just call the script with no parameters:

.\Get-PublicFolderReplicationReport.ps1

If you’d like to export the HTML report to a file, then use the -Filename parameter:

.\Get-PublicFolderReplicationReport.ps1 -Filename “PublicFolderReplicationReport.html”

If you’d like to send an email, use the -SendEmail switch:

.\Get-PublicFolderReplicationReport.ps1 -SendEmail -To me@mydomain.local -From PFReport@mydomain.local -SmtpServer smtp.mydomain.local -Subject “Public Folder Replication Report”

Note: When using the -SendEmail switch, the -To, -From and -SmtpServer parameters are mandatory.

If you want to limit to only scanning specified servers, use the -ComputerName parameter:

.\Get-PublicFolderReplicationReport.ps1 -ComputerName @(“EXCH01”, “EXCH02”)

If you want to limit to only scanning specific Public Folders, use the -FolderPath parameter:

.\Get-PublicFolderReplicationReport.ps1 -FolderPath @(“\MyFolder1”, “\MyFolder2”)

If you want to scan a specific Public Folder and all subfolders, use the -Recurse switch:

.\Get-PublicFolderReplicationReport.ps1 -FolderPath “\MyFolderPath” -Recurse

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