Change all default gateways

This topic contains 6 replies, has 4 voices, and was last updated by Profile photo of Milos Ostojic Milos Ostojic 1 year, 7 months ago.

  • Author
    Posts
  • #31162
    Profile photo of 37mm
    37mm
    Participant

    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!

  • #31181
    Profile photo of Mark
    Mark
    Participant

    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.

  • #31197
    Profile photo of 37mm
    37mm
    Participant

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

  • #31201
    Profile photo of 37mm
    37mm
    Participant
    $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')
    
  • #31218
    Profile photo of Curtis Smith
    Curtis Smith
    Participant

    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.

  • #31222
    Profile photo of 37mm
    37mm
    Participant

    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!

  • #32322
    Profile photo of Milos Ostojic
    Milos Ostojic
    Participant

    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

You must be logged in to reply to this topic.