Author Posts

October 22, 2015 at 6:58 am

I need to change the default gateways on about 50 workstations. I feel like I should be able to do this in power shell. Not really sure where to start.

$domainHost = Get-ADComputer -Filter * -Properties * | select CN

foreach ($item in $domainHost)
{
?????????????????????
}

I am postive it can be done. Not sure how to accomplish this. Can't use DHCP to do this as we are locked in to statics for RDP. Any input is greatly appreciated, Cheers!

October 22, 2015 at 8:35 am

Try the following code
$domainHost = Get-ADComputer -Filter * -Properties CN | select CN
foreach ($item in $domainHost)
{
$n2 = Get-WMIObject win32_NetworkAdapterConfiguration -computer $Item.CN -filter "IPEnabled = True and DHCPEnabled = False" | where-object {$_.defaultIPGateway -eq 'EXISTING GW IP"}
$n2 | ForEach-Object { write-output "$($item.CN) $($_.IPaddress) $($_.IPSubnet) $($_.defaultIPGateWay)" }
#$n2.SetGateways("NEW GW IP")
}

Replace "EXISTING GW IP" and "NEW GW IP" with the proper IP addresses.

Also the operational line is commented out. I recommend you leave it commented and do a dry run first. You also may want to test it on one or two workstations prior to full rollout.

October 22, 2015 at 12:29 pm

Ok, it runs fine but the gateway stays the same.

October 22, 2015 at 12:37 pm

$n2 = Get-WMIObject win32_NetworkAdapterConfiguration -ComputerName $env:COMPUTERNAME -filter "IPEnabled = True and DHCPEnabled = False" | where-object {$_.defaultIPGateway -eq 'old Gateway'}
$n2 | ForEach { write-output "$($item.CN) $($_.IPaddress) $($_.IPSubnet) $($_.defaultIPGateWay)" }
$n2.SetGateways('MY GATEWAY')

October 22, 2015 at 7:14 pm

I have tested the code Mark suggested, and it works quite well against a Window 7 machine. There was one small type-o and I made a few adjustments to output a PSCustomObject rather than a string and a couple other tweeks, but the base code is essentially the same.

The only place where I ran into issues was when changing to a different default gateway that caused my workstation to no have network communication with the remote machine. In that situation, the WMI setgateways method does on get the completion message back because communications have dropped. WMI will sit there for a while waiting for the response before timing out.

$oldgateway = "EX. 1.1.1.1"
$newgateway = "EX. 1.1.1.2"

$domainHost = Get-ADComputer -Filter * -Properties CN | select CN
foreach ($item in $domainHost)
{
    $n2 = Get-WMIObject win32_NetworkAdapterConfiguration -computer $item.CN -filter "IPEnabled = True and DHCPEnabled = False" | where-object {$_.defaultIPGateway -eq $oldgateway}
    $n2 | ForEach-Object { [PSCustomObject]@{ ComputerName = $item.CN; IPAddress = $_.IPaddress; SubnetMask = $_.IPSubnet; Gateway = $_.defaultIPGateWay } }
#    $n2.SetGateways($newgateway)
}

Again as Mark did, I have comment out the SetGateways to prevent accidentally crippling your environment, you will have to uncomment that if you are sure you want it to process.

October 23, 2015 at 4:55 am

This worked awesome guys, Really saved me alot of time this morning. Powershell is the number one reason for my weight gain, lol , never have to get up!

November 27, 2015 at 11:30 am

Has anybody been using CIM commands instead of WMI? I've been struggling with type of DefaultIPGateway argument.
Working on a project where changing gateways precedes configuring network switch routes (done by network department). So, no connection for a while. Been working with InDisconnectedSession parameter of Invoke-Command and Remove-NetRoute ; New-NetRoute. The best way I found to avoid being stuck forever with remoting.
Still devastated by not being able to resolve mystery of setgateways cim method.
BR