Here's a .BAT file that will send an email notification to a specified address when a Windows Server is restarted:
Code snippet
@echo off
set recipient=your_email@example.com
set subject=Windows Server Restart Notification
if "%computername%"=="your_server_name" (
echo "Your server (%computername%) has been restarted." | mail -s "%subject%" %recipient%
)
Explanation:
@echo off
: This line turns off the echoing of commands to the console, making the script run more cleanly.set recipient=your_email@example.com
: Replace your_email@example.com
with the actual email address you want to send the notification to.set subject=Windows Server Restart Notification
: Set the subject line of the email.if "%computername%"=="your_server_name"
: This checks if the current computer name matches the specified server name. If it does, the email will be sent. Replace your_server_name
with the actual name of your server.echo "Your server (%computername%) has been restarted." | mail -s "%subject%" %recipient%
: This line sends the email. It echoes the message "Your server (%computername%) has been restarted." and pipes it to the mail
command. The -s
option specifies the subject, and %recipient%
is the recipient's email address.
To use this script:
- Create a new text file: Open a text editor like Notepad or WordPad.
- Paste the code: Copy and paste the code from above into the text file.
- Save the file: Save the file with a
.bat
extension, such as restart_notification.bat
. - Schedule the script: Use Task Scheduler to schedule the script to run at regular intervals (e.g., daily, weekly).
Note:
- You'll need to have a mail server configured on your network or use a third-party email service for the
mail
command to work. - For more advanced email notifications, you can consider using PowerShell scripts or dedicated email notification tools.
By following these steps, you can create a simple and effective .BAT file to notify you of Windows Server restarts.
0 comments:
Post a Comment