PowerShell to automatically switch network settings

    Some time ago, my provider had a strip of glitches associated with cable re-routing. Now everything is fine, but then a couple of times a week the Internet was cut off and the support worker each time required me to connect the computer directly and not through the router. So, to quickly change the 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 an example 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”)
        }
    }
    

    Also popular now: