Viewing 2 reply threads
  • Author
    Posts
    • #337499
      PNN
      Participant
      Member Points: 329
      Rank: 2

      Dear IT pros

      I am trying to write a script that will check the ping status of various servers and check if ping is unsuccessful, restart the network service/adapter and send a notification to via email. Any help will be appreciated. Thanks

    • #337894
      Michael Pietroforte
      Keymaster
      Member Points: 42,263
      Author of the year 2018
      Rank: 4

      How about this:

      while($(ping 8.8.8.8) -like "*Reply*"){
      "Server replied"
      }
      "Server unreachable"
      Restart-NetAdapter Ethernet0
      Send-MailMessage -From "me@yourdomain.com" -To "admin@yourdomain.com" -Subject "server down" -SmtpServer "smtp.yourdomain.com"
      • #345245
        PNN
        Participant
        Member Points: 329
        Rank: 2

        Thanks Michael. Sorry I couldn’t reply sooner. Proceeded on leave from work and got caught up doing other stuff. I have tested the script on powershell but whether a server is down or up, am getting ‘server replied’ thus not really working. Also, the server replied message keeps running continuously until you break the script. It should just give one response if up and if down reset the network adapter if ping results are ‘timeouts’ and not ‘destination server unreachable’ since if the server is unreachable it means it is powered off.

        • #345264
          Michael Pietroforte
          Keymaster
          Member Points: 42,263
          Author of the year 2018
          Rank: 4

          I have tested the script and it works. You can try it by disconnecting the machine where the script runs  from the network.

          Of course, you need an infinite loop if you want to test the availability of a server. The server is running all day, right?

          And the script does give you a response if the server has an internet connection. If the connection is lost, it leaves the loop, gives you a corresponding notice, restarts the network adapter and then sends an email to the admin.

          However, from your last message, it is unclear what you are trying to accomplish. Do you want to test a remote server or the server where the script runs? If your problem is that a remote server is down because the network adapter is malfunctioning, you obviously can’t fix that problem remotely. How can you reach the server if it is no longer connected?

          So the little script I wrote has to run on the server with the network adapter problem. It can ping any machine in your network and once the ping fails, it will restart the local network adapter.

        • #346720
          PNN
          Participant
          Member Points: 329
          Rank: 2

          Thanks, Michael. I think I did not explain clearly what I am trying to accomplish but your solution solves part of my problem. I can run this script on each of my servers. I was thinking of running the script from a central server that can reach all the others say domain controller and reset adapters of these servers (in a CSV list) in case of timeouts only and then send a notification.
          Our servers are connected to the local domain and from time to time, they lose identity and show unidentified network or public network instead of domain network affecting applications that require domain authentication. We normally just reset the adapter, or enable/disable it or restart network services/network location awareness service to revert it back to the domain network. All these servers also have access to the internet. Also to note is that most of the servers are VMs running on VMWare.

          What I want to accomplish is to automate this process of resetting the adapter/restarting network services when the server loses domain identity since it happens so randomly. Then the email notification is to inform me of the action. We use office365 as our exchange server so the email notification can only happen if there is a successful internet connection.  It is a bit frustrating to get the operations team inform you of a downtime and you were not aware.

          I hope my explanation is clearer now. You can also suggest a better solution not necessarily using this type of script. I thought if I can get a script to automate the reset if there is a timeout.

          Looking forward to your response

        • #348218
          Michael Pietroforte
          Keymaster
          Member Points: 42,263
          Author of the year 2018
          Rank: 4

          First of all, if the network type is set to Public on a domain member, it usually means that the authentication failed with a domain controller. This is serious issue for a server. You want to find the cause of this problem. Resetting network adapters is not the solution to the problem as this can also cause all kinds of problems. Check the event log for errors to find out what causes the problem.

          Second, if the remote machine is not authenticated in the domain, you can’t access it anymore through PowerShell remoting. Thus, resetting the network adapters remotely from a central location is problematic. I guess a workaround could be to configure the machines as workgroup computers for PowerShell remoting. But this is is not good for security and I don’t recommend it. Thus, I recommend running the script on the problematic servers. If you want to manage the scripts from a central location, you can work with remote jobs. This allows you to view the result at your local console.

          Third, I wouldn’t use ping for this purpose. I would rather read the network type directly:

          while((Get-NetConnectionProfile).NetworkCategory -like "*Domain*"){
          "Network type is DomainAuthenticated"
          Sleep 5
          }
          "Network problem"
          Restart-NetAdapter Ethernet0
          Send-MailMessage -From "me@yourdomain.com" -To "admin@yourdomain.com" -Subject "server down" -SmtpServer "smtp.yourdomain.com"

          You can test the script by temporarily removing the machine from the network.

        • #348240
          PNN
          Participant
          Member Points: 329
          Rank: 2

          Yea, there is an issue with domain authentication but I have checked the logs, DNS entries and can’t find what causes the issue. The good thing is that this issue occurs frequently in servers in a particular VM host meaning it can be isolated and further troubleshooting done. I would appreciate whatever tips to diagnose and fix this persistent issue.

          I need the servers to be in the domain for security purposes. Am not so familiar with remote jobs but I will check your link and research on it more. How about the issue of sending email? We are using office365 which means internet connection is vital for success.

        • #348960
          Michael Pietroforte
          Keymaster
          Member Points: 42,263
          Author of the year 2018
          Rank: 4

          If the problem only exists on a particular host, it might be a hardware issue. I would move one of the servers to another host and see if the problems reoccurs. If it doesn’t, I would replace the network adapter on the problematic host.

          To send an email via the Office 365 SMTP server you can use this code:

          $user='yourName@yourDomain.com'
          $creds=Get-Credential $user
          Send-MailMessage -To 'admin@yourDomain.com' -Subject "$env:computername down" -Body 'You really need to fix this issue soon' -UseSsl -Port 587 -SmtpServer 'smtp.office365.com' -From $user -Credential $creds
        • #349663
          PNN
          Participant
          Member Points: 329
          Rank: 2

          Thanks Michael. Yes I will get to the root of this problem and revert here. Thanks for all your help

      • #345251
        PNN
        Participant
        Member Points: 329
        Rank: 2

        Additionally, I have a list of servers that need to be routinely checked for uptime say daily. I am thinking of having a csv file from which the script can check for each server and give a status for all in an email

    • #340455
      Karim Buzdar
      Moderator
      Member Points: 8,068
      Rank: 3

      Hi PNN,

      Does the solution help?

      Thank you,

      • #345248
        PNN
        Participant
        Member Points: 329
        Rank: 2

        Hi Karim

        The solution is not working as it should. See my reply to Michael.

Viewing 2 reply threads
  • You must be logged in to reply to this topic.
© 4sysops 2006 - 2023

CONTACT US

Please ask IT administration questions in the forums. Any other messages are welcome.

Sending

Log in with your credentials

or    

Forgot your details?

Create Account