SCOM Powershell Script for SMTP Connection Monitoring

Fazc 0 Reputation points
2024-11-19T15:01:07.8+00:00

I need to create a Powershell script that simulates a connection to an SMTP server. The purpose of this is to check if there is a problem with our SMTP gateway server.

Operations Manager
Operations Manager
A family of System Center products that provide infrastructure monitoring, help ensure the predictable performance and availability of vital applications, and offer comprehensive monitoring for datacenters and cloud, both private and public.
1,498 questions
0 comments No comments
{count} votes

1 answer

Sort by: Most helpful
  1. XinGuo-MSFT 18,921 Reputation points
    2024-11-20T06:55:11.2766667+00:00

    Hi,

    Here's a simple PowerShell script that you can use to simulate a connection to an SMTP server and check its status:

    # Define SMTP server and port
    $smtpServer = "smtp.yourserver.com"
    $smtpPort = 25
    
    # Create a TCP client to connect to the SMTP server
    $tcpClient = New-Object System.Net.Sockets.TcpClient
    
    try {
        # Attempt to connect to the SMTP server
        $tcpClient.Connect($smtpServer, $smtpPort)
        if ($tcpClient.Connected) {
            Write-Output "Successfully connected to SMTP server $smtpServer on port $smtpPort."
        } else {
            Write-Output "Failed to connect to SMTP server $smtpServer on port $smtpPort."
        }
    } catch {
        Write-Output "Error: $_"
    } finally {
        # Close the TCP client connection
        $tcpClient.Close()
    }
    

    This script attempts to connect to the specified SMTP server and port. If the connection is successful, it will output a success message; otherwise, it will output a failure message or any error encountered.

    Feel free to modify the $smtpServer and $smtpPort variables to match your SMTP server's details. Let me know if you need any further assistance!

    0 comments No comments

Your answer

Answers can be marked as Accepted Answers by the question author, which helps users to know the answer solved the author's problem.