#Create Time Stamp $time = Get-Date ForEach ($server in (Import-CSV 'C:\output\servers.csv')){ #Test the port by checking to see if it is open and listening write-host 'Testing connectivity to SQL server on', $server.name $servername=$server.name $socket = new-object System.Net.Sockets.TcpClient($server.ip,$server.port) # $? variable means to check what happened in the last operation and it's success or failure if ($?) { $socket.close() $socket.dispose() Start-Sleep -s 1 } Else { $socket.close() $socket.dispose() Write-Host "Sending Email" #SMTP server name $smtpServer = "[redacted]" #Creating a Mail object $msg = new-object Net.Mail.MailMessage #Creating SMTP server object $smtp = new-object Net.Mail.SmtpClient($smtpServer) #Email structure $msg.From = "[redacted]" $msg.ReplyTo = "[redacted]" $msg.To.Add("[redacted]") $msg.subject = "SAP Connection Failed" $msg.body = $server,' Has failed SQL Connectivity' #Sending email $smtp.Send($msg) Write-Host $servername ,'s SQL connection has failed. Current time is: ', $time Start-Sleep -s 300 } }
This is the code I am working with.
The issue is when I check netstat these connections stay open for quite some time before closing. Is there any way to forcibly close the connection after it is finished?