Hi All,
I want to check whether telnet connection to a SMTP server on port 25 is working or not from a machine. Below is the script i’ve written to do it.
Now when i run the telnet command on a machine manually it is saying “421 4.3.2 Service not available… Connection to host lost.” so telnet connection is not working from that machine. that is fine.
but if i run the below script on the same machine where telnet failed with above said error message, it is saying “Success”. Not sure what is wrong with my script, please help
$Socket = New-Object Net.Sockets.TcpClient
# Suppress error messages
$ErrorActionPreference = ‘SilentlyContinue’
# Try to connect
$Socket.Connect(“124.140.118.209”, “25”)
# Make error messages visible again
$ErrorActionPreference = ‘Continue’
# Determine if we are connected.
if ($Socket.Connected) {
Write-Output “Success”
$Socket.Close()
}
else {
Write-Output “Failed”
}
# Apparently resetting the variable between iterations is necessary.
$Socket = $null