Configure Alerts for disk I/O errors using t-sql

 Use this script to create alerts for disk I/O errors.

Before running the script replace the <OperatorName> parameter with the actual operator that you would like to alert.

/*
PARAMETERS:
<OperatorName,sysname,Alerts> - Name of the Operator/Alias to alert.
@notification_method 1 - Bitmap of notification types/options: 1 = email,
2 = pager, 4 = netsend
*/
 
USE msdb
GO
 
EXEC msdb. dbo.sp_add_alert @name = N'823 - Read/Write Failure',
    @message_id = 823,
    @severity = 0,
    @enabled = 1,
    @delay_between_responses = 0,
    @include_event_description_in = 1
GO
EXEC msdb .dbo. sp_add_notification
        @alert_name=N'823 - Read/Write Failure' ,
        @operator_name =N'<OperatorName>' ,
        @notification_method = 1;   -- 1 for email
 
EXEC msdb. dbo.sp_add_alert @name = N'824 - Page Error',
    @message_id = 824,
    @severity = 0,
    @enabled = 1,
    @delay_between_responses = 0,
    @include_event_description_in = 1
GO
EXEC msdb .dbo. sp_add_notification
        @alert_name=N'824 - Page Error' ,
        @operator_name =N'<OperatorName>' ,
        @notification_method = 1;   -- 1 for email
 
EXEC msdb. dbo.sp_add_alert @name = N'825 - Read-Retry Required',
    @message_id = 825,
    @severity = 0,
    @enabled = 1,
    @delay_between_responses = 0,
    @include_event_description_in = 1
GO
EXEC msdb .dbo. sp_add_notification
        @alert_name=N'825 - Read-Retry Required' ,
        @operator_name =N'<OperatorName>' ,
        @notification_method = 1;   -- 1 for email

Get CPU utilization for a host using PowerShell Script

 The code snippet below shows how to get CPU utilization for a host.

Get-WmiObject -Class win32_Processor -ComputerName <ip address> | select DeviceID,Name,Status,LoadPercentage | Sort-Object LoadPercentage -Descending

Note: You have to update ip address or hostname to get output.

Send email with attachments using PowerShell script

Being able to send emails through script is really useful, particularly for system administrators who often have to administer their servers remotely. It also comes in handy for various other purposes. The code below demonstrates how to send email with attachments using PowerShell.

$smtpServer = "<your smtp mail server address>"
$msg = new-object Net.Mail.MailMessage
$smtp = New-Object Net.Mail.SmtpClient($smtpServer)
 
$msg.From = "from@xyz.com"
$msg.To.Add("to@xyz.com")
$msg.Subject = "Your email subject line"
$msg.Body = "Your email body"
 
# Attachments
$file1 = ".\f1.txt"
$file2 = ".\f2.txt"
 
foreach ($file in ($file1, $file2)){
    $att = New-Object Net.Mail.Attachment($file)
    $msg.Attachments.Add($att)
}
 
$smtp.Send($msg)