Answered How to remove default gateway

  • Saturday, March 03, 2012 11:56 PM
     
      Has Code

    I have servers with multiple NICs. I need to delete the default IP gateway on all but one NIC.

    I can find/target the NIC I want to disable the GW on using something like:

    $nic = Get-WmiObject win32_networkadapterconfiguration`

    -filter "description = 'Intel(R) 82577LM Gigabit Network Connection'"

    I can get the GW using

    $nic.defaultipgateway

    but

    $nic.defaultipgateway.remove

    doesn't seem to work, yet doesn't return an error.

    $nic.defaultipgateway | gm

    does show remove in the list. What am I missing?


All Replies

  • Sunday, March 04, 2012 1:09 AM
     
      Has Code

    Based on the msdn article for that class: http://msdn.microsoft.com/en-us/library/windows/desktop/aa394217%28v=vs.85%29.aspx, it looks like the defaultipgateway is a ReadOnly property, so a remove would not work. Perhaps  using the SetGateways() method or SetPropertyValue() method to accomplish this would work better.

    $nic.SetPropertyValue('DefaultIPGateway',$Null)
    
    $nic.SetGateways($Null)



    Boe Prox

    Please remember to mark the best solution as the answer using Mark as Answer. If you find a solution to be helpful, please use Vote as Helpful.

    Looking for a script? Check out the Script Repository
    Need a script written for you? Submit a request at the Script Request Page

  • Sunday, March 04, 2012 5:18 AM
     
      Has Code

    No I need to do a set or write? If I use

    $nic.SetPropertyValue('DefaultIPGateway',$Null)

    the look at $nic, the DefaultIpGateway is empty.

    But if I look via ipconfig, or the properties of the NIC, it still has a value.

  • Sunday, March 04, 2012 3:55 PM
     
      Has Code

    Or possibly

    $nic | invoke-wmimethod -name setgateways -argumentlist $null


    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

  • Sunday, March 04, 2012 4:16 PM
     
     

    That doesn't seem to do anything for me.

    Boe's suggestions clear it from a WMI perspective, but not from a GUI or IPCONFIG perspective. A reboot doesn't seem to help.

  • Sunday, March 04, 2012 4:19 PM
     
     
    Starting to wonder if this might be easier done via netsh.

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

  • Sunday, March 04, 2012 4:55 PM
     
      Has Code

    I looked into that. If you do a

    netsh int ipv4 del address "name of NIC" gateway=all

    Then it wipes the address, mask, and gateway. I can follow that up with setting the address again, but I haven't figured out how to get the address to begin with. My theory is

    1. get the current IP address and mask into variables
    2. delete it with the line above
    3. reset the address and mask

    I just need to figure out step 1.

  • Sunday, March 04, 2012 5:11 PM
     
      Has Code
    show ipaddresses "name of NIC" level=normal
    You can parse the IP addresses out of that with a regex and assign them to a variable.

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

  • Sunday, March 04, 2012 5:16 PM
     
     
    If you give it the address= parameter and a null argument list, does it leave the existing address entries alone?

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

  • Sunday, March 04, 2012 6:21 PM
     
     

    No. It deletes it. And the line you suggest won't work either, as I need the subnet mask as well. I essentially have to reset the address and mask and leave the GW blank.

    This isn't worth it.

  • Sunday, March 04, 2012 8:08 PM
     
     
    That seems like a bug.  According to the help, removing the address is a separate operation from removing the gateway.

    [string](0..33|%{[char][int](46+("686552495351636652556262185355647068516270555358646562655775 0645570").substring(($_*2),2))})-replace " "

  • Monday, March 05, 2012 3:09 AM
    Moderator
     
     

    Hi,

    Please refer to the below links for Remove-NetworkAdapterGateway and change default gateway on client:

    http://www.vexasoft.com/cmdletlibrary/support/removenetworkadaptergateway.html

    http://everythingsysadmin.wordpress.com/2010/07/29/change-default-gateway-on-clients-with-static-ips/

    Hope this helps.

    Best Regards,

    Yan Li

     

    TechNet Subscriber Support

    If you are TechNet Subscription user and have any feedback on our support quality, please send your feedback here.


    Yan Li

    TechNet Community Support

  • Friday, March 09, 2012 3:27 AM
     
     

    That's not an answer. I can't go installing a third party library on HUNDREDS of servers.

    If this can't be done via PowerShell, I'll have someone manually do it on each server.

  • Saturday, March 10, 2012 6:15 AM
     
     Answered

    $nics=Get-WmiObject win32_networkadapterconfiguration | ?{$_.IPEnabled -eq "True"}
    foreach ($nic in $nics)
    {
    $ip=$nic.ipaddress[0]
    $nic.enabledhcp()
    $nic.enablestatic($ip,"255.255.255.0")
    }

    replace 255.255.255.0 with your subnet mask

    it will change your NIC with static IP to DHCP enable and than will set it to static IP without gateway.

    Thanks

    Serkan



    Get-Mailbox.org

  • Saturday, March 10, 2012 1:54 PM
     
      Has Code

    Okay, that seems to mostly work. If you run it, the field gets cleared if you look at the NIC properties using the GUI. But IPConfig still shows the gateway till the server is rebooted. That's not an issue since I have to reboot because of other code in my script.

    Thanks! It was the $Nic.IpAddress[0] that was blocking me before. I couldn't figure that out for some reason.

    I created a function:

    function Remove-NicGateway	{
    	param(
    		[string]$NicName,
    		[string]$NicMask = "255.255.255.192"
    	)
    	$Adapter = Get-WmiObject -Class Win32_NetworkAdapter -Filter "NetConnectionID='$NicName'"
    	$Nic = Get-WmiObject -Class Win32_NetworkAdapterConfiguration -Filter "Index=$($Adapter.Index)"
    	$NicIP = $Nic.IpAddress[0]	
    	$Nic.EnableDhcp() | Out-Null
    	$Nic.EnableStatic($NicIp,$NicMask)
    }

    If I get some time, I'm going to see about catching the subnet mask before setting it to DHCP so that I don't have to specify. Not really an issue now since all of the servers and NICs I need to touch have the same mask.

    Thanks!


  • Saturday, March 10, 2012 3:18 PM
     
     

    $NicMask = $Nic.IpSubnet[0]

    seems to work.....

  • Wednesday, November 28, 2012 10:51 AM
     
     

    route delete 0.0.0.0

    also does the trick.

     

    Jan


    Jan Z