Advertisement
Guest User

Powershell Script - GatewaySwapper

a guest
Mar 2nd, 2016
493
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. ##########################################
  2. # Default Gateway Changer
  3. # Version 1.0
  4. # Written by github.com/phlatlinebeta
  5. ##########################################
  6. # How to Use:
  7. # First change the global variables below
  8. # Run the script as an admin. The script will try once to ping $PingTest, if it fails to ping
  9. # that address then it will change the default gateway address on all computers that have a
  10. # DHCP leases and a computer object in AD. The script will also change the Default gateway on
  11. # the host computer running the script and change the router (option 003) on your DHCP server
  12. # scope settings.
  13. # I suggest setting the script to run as a scheduled task every 10 minutes.
  14. # PS. This script needs to run as ADMIN to apply changes to DHCP Scope settings
  15. ##########################################
  16. #Global Variables that you need to change
  17. $DHCPServer = "MyDHCPServer"  # Name of your DHCP Server
  18. $DHCPScopeID = "192.168.1.0"  # Name of your DHCP Scope
  19. $BackupDefaultGateway = "192.168.1.2"
  20. $StaticServers = @("Server1","Server2","PrintServer1")  # Use this to add static IP addresses (windows) that you also want their gateway changed, example "Server1,"Server2","Server3"
  21. $MyIPSubnet = "192.168.1."  # This is needed to find which NIC has an assigned Default Gateway
  22. $PingTest = "8.8.8.8"  # What you will ping to check your internet connection (8.8.8.8 is google's DNS server)
  23. $EmailTo = "helpdesk@mycompany.com"  # What email address you want alerted
  24. $EmailFrom = "madeupgmailaccount@gmail.com"  # email settings
  25. $SMTPServer = "smtp.gmail.com"  # email settings
  26. $SMTPPort = 587  # email settings
  27. $SMTPUsername = "madeupgmailaccount@gmail.com"  # email settings
  28. $SMTPPassword = "PASSWORD4madeupgmailaccount"  # email settings
  29. ##########################################
  30.  
  31.  
  32.  
  33. Function ListADComputerDHCPLeases()
  34. {
  35. # Function Instruction: This function will generate a list of DHCP leases that have a matching Computer Object in Active Directory
  36. # this function returns an array of those computers -jml2016
  37. # Example usage: $MyArray = ListADComputerDHCPLeases
  38.  
  39.     #$DHCPServer = ""  #Declared this globally instead of inside the function
  40.     #$DHCPScopeID = ""  #Declared this globally instead of inside the function
  41.    
  42.     # Load the Microsoft Active Directory Module
  43.     Import-Module ActiveDirectory
  44.     #Get a list of all DHCP leases(fully qualified domain names)
  45.     $DHCPComputers = Get-DhcpServerv4Lease -Computername $DHCPServer -ScopeId $DHCPScopeID | ForEach-Object {$_.HostName}
  46.     # Get a list of all computer names(fully qualified domain names)
  47.     $ADComputers = Get-ADComputer -Filter * | ForEach-Object {$_.DNSHostName}
  48.     # $myarray will be used to load all computers that are both in AD and have DHCP leases
  49.     $myarray = @()
  50.     # Parse all the DHCP computers and check each one against the computers found in AD
  51.     ForEach ($value in $DHCPComputers)
  52.     {
  53.         if ($value.length -ne 0)  # skip DHCP leases that have no DNS name
  54.         {
  55.             if ($ADComputers -contains $value)
  56.             {
  57.                 #Write-Host "$value is found in AD"
  58.                 $myarray+=$value
  59.             }
  60.             else
  61.             {
  62.                 #Write-Host "$value not found in AD " $value.length -BackgroundColor DarkMagenta
  63.             }
  64.         }
  65.     }
  66.     return $myarray
  67. }
  68.  
  69. Function ChangeDefaultGateway ($NewDefaultGateway, $ComputerName)
  70. {
  71.     #Function Instructions: This fucntion will change the default gateway on computer $ComputerName to $NewDefaultGateway
  72.     #function requires variable $MyIPSubnet to be defined as "XXX.XXX.XXX."
  73.  
  74.    
  75.     #$MyIPSubnet = ""  #Declared this globally instead of inside the function
  76.     $Returnvalue=""  #Used to return what NIC/IP had their gateway changed from what to what
  77.     $NICs = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -ComputerName "$ComputerName" -Filter “IPEnabled=TRUE”
  78.     foreach($NIC in $NICs)
  79.     {
  80.         if($NIC.DefaultIPGateway -match $MyIPSubnet) # fetch the NIC that already has a gateway address
  81.         {
  82.             $Returnvalue = "Changing Gateway for " + $ComputerName + " on " + $NIC.IPAddress[0] + " " + $NIC.Description + " from " + $NIC.DefaultIPGateway[0] + " to " + $NewDefaultGateway + "`r`r"
  83.             write-host $Returnvalue -BackgroundColor DarkCyan #DEBUG
  84.             $null = $NIC.SetGateways($NewDefaultGateway)  # Changes the gateway on $NIC
  85.         }
  86.     }
  87.     return $Returnvalue
  88. }
  89.  
  90. Function PingIt ($Server)
  91. {
  92.     #Funciton Instructions: Pings Server, returns 0 if ping fails, 1 if pings succeeds
  93.  
  94.     if(!(Test-Connection -Cn $Server -Count 1 -quiet))
  95.     {
  96.         return 0
  97.     }
  98.     ELSE
  99.     {
  100.         return 1
  101.     }
  102. }
  103.  
  104. Function SendEmail($EmailSubject, $EmailBody, $EmailAttachment)
  105. {
  106.     #Function Instructions: Sends an email using variables declared at the top of the script
  107.     # Functions needs to be passed $EmailSubject and $EmailBody, $EmailAttachment is optional
  108.  
  109.     $SMTPMessage = New-Object System.Net.Mail.MailMessage($EmailFrom,$EmailTo,$EmailSubject,$EmailBody)
  110.     if (!($EmailAttachment -eq $NULL))
  111.     {
  112.         $attachment = New-Object System.Net.Mail.Attachment($EmailAttachment)
  113.         $SMTPMessage.Attachments.Add($EmailAttachment)
  114.     }
  115.     $SMTPClient = New-Object Net.Mail.SmtpClient($SmtpServer, $SMTPPort)
  116.     $SMTPClient.EnableSsl = $true
  117.     $SMTPClient.Credentials = New-Object System.Net.NetworkCredential($SMTPUsername, $SMTPPassword);
  118.     $SMTPClient.Send($SMTPMessage)
  119. }
  120.  
  121.  
  122.  
  123. ##########################################
  124. # Main
  125. ##########################################
  126. If (PingIT $PingTest)
  127. {
  128.     #Ping Success - Internet Working
  129.     Write-Host "Connection working"
  130.    
  131. }
  132. Else
  133. {
  134.     #Ping Fail - Internet Offline - Switch Gateways/ISPs
  135.     CLS
  136.     Write-Host "Internet Connection offline. Changing Gateways...(this may take a min)" -BackgroundColor Red -ForegroundColor Black
  137.     $Results = @()
  138.     $ThisComputerName = $env:computername
  139.     $Results += ChangeDefaultGateway $BackupDefaultGateway $ThisComputerName  #Change default gateway on host server running this script
  140.    
  141.     # \/ Change the DHCP Server's Scope Option to use the new default gateway as option 003 Router \/
  142.     Set-DhcpServerv4OptionValue -ComputerName $DHCPServer -ScopeId $DHCPScopeID -Router $BackupDefaultGateway
  143.     $Results += "Changed option 003 Router on DHCP Server:" + $DHCPServer + " ScopeID:" + $DHCPScopeID + " to " + $BackupDefaultGateway + "`r`r"
  144.  
  145.     $Computers = ListADComputerDHCPLeases  #Fills array with all DHCP leases that are computer objects in AD
  146.     ForEach ($value in $StaticServers)
  147.     {
  148.         $Results += ChangeDefaultGateway $BackupDefaultGateway $value  # Change each static server's default gateway
  149.     }
  150.     ForEach ($value in $Computers)
  151.     {
  152.         $Results += ChangeDefaultGateway $BackupDefaultGateway $value   #Change each computer's default gateway
  153.     }
  154.     SendEmail "Error:Internet Outage" $Results
  155.  
  156. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement