Advertisement
AndrewZ

Restart Network Adapter

Sep 3rd, 2013
120
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. # Check that the network is rechable on Linux KVM machines. If not,
  2. # disable the NIC, wait 30 seconds, and enable the NIC again.
  3. # If the computer is online, send a notification email.
  4. # Created by Andrew Zbikowski. <andyzib@gmail.com>
  5. # Version: 2013-07-08_01
  6.  
  7. # Network adapter service name. Use get-WmiObject win32_networkadapter | fl to find this.
  8. # netkvm for RHEL KVM
  9. $serviceName = "netkvm"
  10. # E1G60 for VMware Fusion
  11. #$serviceName = "E1G60"
  12.  
  13. # Ping this computer/IP address to test connectivity.
  14. $pingHost = "10.1.1.1"
  15.  
  16. # Ping Count, number of times to ping $pingHost
  17. $pingCount = 4
  18.  
  19. #SMTP server name
  20. $smtpServer = "smtp.domain.com"
  21.  
  22. # Email From
  23. $emailFrom = "Robot B-9 <noreply@example.com>"
  24.  
  25. # Email To
  26. $emailTo = "alerts@example.com"
  27.  
  28. function sendMail{
  29.     $computer = gc env:computername
  30.    
  31.     #Creating a Mail object
  32.     $msg = new-object Net.Mail.MailMessage
  33.  
  34.     #Creating SMTP server object
  35.     $smtp = new-object Net.Mail.SmtpClient($smtpServer)
  36.  
  37.     #Email structure
  38.     $msg.From = $emailFrom
  39.     $msg.ReplyTo = $emailFrom
  40.     $msg.To.Add($emailTo)
  41.     $msg.subject = "Network Connection Restarted on $computer"
  42.     $msg.body = "Network connectivity was lost on $computer. NIC on $computer was restarted and connectivity has been restored."
  43.  
  44.     #Sending email
  45.     $smtp.Send($msg)
  46. }
  47.  
  48.  
  49. If (-Not(Test-Connection $pingHost -count 4 -quiet)) {
  50.     $Ethernet = get-WmiObject win32_networkadapter |where { $_.ServiceName -like "*$serviceName*"}
  51.     $Ethernet.Disable()
  52.     Start-Sleep -s 30
  53.     $Ethernet.Enable()
  54.     Start-Sleep -s 30
  55.     If (Test-Connection $pingHost -count 4 -quiet) {
  56.         sendMail
  57.     }
  58. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement