Sep 18, 2024

Creating a .BAT File for Windows Server Restart Notification

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:

  1. @echo off: This line turns off the echoing of commands to the console, making the script run more cleanly.
  2. set recipient=your_email@example.com: Replace your_email@example.com with the actual email address you want to send the notification to.
  3. set subject=Windows Server Restart Notification: Set the subject line of the email.
  4. 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.
  5. 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:

  1. Create a new text file: Open a text editor like Notepad or WordPad.
  2. Paste the code: Copy and paste the code from above into the text file.
  3. Save the file: Save the file with a .bat extension, such as restart_notification.bat.
  4. 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:

Look

Technology Used by Successwful Businesses

Internet Speed Test