How to remove default gateway
-
Saturday, March 03, 2012 11:56 PM
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?
- Edited by Yan Li_Microsoft Contingent Staff, Moderator Monday, March 05, 2012 3:00 AM edit
All Replies
-
Sunday, March 04, 2012 1:09 AM
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
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
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 PMStarting 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
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
- get the current IP address and mask into variables
- delete it with the line above
- reset the address and mask
I just need to figure out step 1.
-
Sunday, March 04, 2012 5:11 PM
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 PMIf 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 PMThat 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 AMModerator
Hi,
Please refer to the below links for Remove-NetworkAdapterGateway and change default gateway on client:
http://www.vexasoft.com/cmdletlibrary/support/removenetworkadaptergateway.html
Hope this helps.
Best Regards,
Yan Li
If you are TechNet Subscription user and have any feedback on our support quality, please send your feedback here.
Yan Li
TechNet Community Support
- Marked As Answer by Yan Li_Microsoft Contingent Staff, Moderator Friday, March 09, 2012 1:25 AM
- Unmarked As Answer by Pat Richard [MVP]MVP Friday, March 09, 2012 3:26 AM
-
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
$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
- Marked As Answer by Pat Richard [MVP]MVP Saturday, March 10, 2012 1:54 PM
-
Saturday, March 10, 2012 1:54 PM
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!
- Edited by Pat Richard [MVP]MVP Saturday, March 10, 2012 1:55 PM
-
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

