Net Adapter toggling/Route Table changes

by OCSecurity at 2012-12-27 06:19:02

I had a question regarding designing a script that could do the following:

-Enable a Network Adapter (I have seen the commands for this)
-Change the routing tables to direct external traffic across the newly enabled NIC while keeping internal traffic over the existing NIC

I normally redo these manually via the command prompt but I was hoping there was a way to do this via a script to make life a little easier. Is a PowerShell script the best option?

Any help you can provide would be greatly appreciated.
by Klaas at 2012-12-27 06:34:15
I used to do that with WMI: get-wmi -computername server1 -class win32_networkadapter and also win32_networkadapterconfiguration. The trick nowadays is to apply a filter that makes absolutely sure you select the right one, on my Win7 machine I have 19 adapters and I haven’t got any virtual machines installed. When you can isolate that one adapter you can set ip, dns, wins and default gateway. This is a part of an old script (PS v1 on Windows XP):
$obj = get-wmiobject -class Win32_NetworkAdapterConfiguration -computername $comp -ea silentlycontinue |`
where {$.IPAddress -match "192.168."}| where {$.IPEnabled -and -not $_.DHCPEnabled}
foreach ( $adapt in $obj ) {$adapt.setgateways("$GW")}
I believe the method for changing IP is .enablestatic(). You can find that by piping to | get-member.
routes I used to add by calling psexec (you better use invoke-command) and executing the route command on the target server.