📜 ⬆️ ⬇️

PowerShell to automatically switch network settings

Some time ago, my provider had a streak of glitches associated with re-positioning the cable. Now everything is fine, but then a couple of times a week the Internet was cut off and the support worker every time demanded that I connect the computer directly and not through the router. That's for a quick change in network settings, I just made two small functions to_direct and to_router , which I bring to your attention. They are not written in the best way - so these are just pieces of code that work and that may be useful to you, and not a sample of how to write scripts.



 function to_direct () {
     $ NICs = Get-WMIObject Win32_NetworkAdapterConfiguration |  ? {$ _. IPEnabled -eq “TRUE”}
     Foreach ($ NIC in $ NICs) {
       $ NIC.EnableStatic ("64.38.232.180", “255.255.255.0")
       $ NIC.SetGateways ("64.38.232.180")
       $ DNSServers = “216.7.89.63", ”64.38.232.180]"
       $ NIC.SetDNSServerSearchOrder ($ DNSServers)
       $ NIC.SetDynamicDNSRegistration (”FALSE”)
     }
 }
 function to_router () {

   $ NICs = Get-WMIObject Win32_NetworkAdapterConfiguration |  ? {$ _. IPEnabled -eq “TRUE”}
	 Foreach ($ NIC in $ NICs) {
  		 $ NIC.EnableStatic ("192.168.1.3", “255.255.255.0")
         $ NIC.SetGateways ("192.168.1.1")
         $ DNSServers = “192.168.1.1", "192.168.1.2"
         $ NIC.SetDNSServerSearchOrder ($ DNSServers)
         $ NIC.SetDynamicDNSRegistration (”FALSE”)
     }
 }

')

Source: https://habr.com/ru/post/59712/


All Articles