-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathlabadmin-config-gateway.ps1
57 lines (40 loc) · 1.34 KB
/
labadmin-config-gateway.ps1
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
#Requires -RunAsAdministrator
<#
.SYNOPSIS
Set gateway address (even if current addres has been get using dhcp)
.PARAMETER setGateway
Address to set as gateway (remove old gateway)
.PARAMETER resetGateway
Address to remove as gateway (and try renew DHCP addresses)
.NOTES
File Name: labadmin-config-gateway.ps1
Author : Leonardo Marco
#>
Param(
[Parameter(Mandatory=$true, ParameterSetName='set')]
[ipaddress]$setGateway,
[Parameter(Mandatory=$true, ParameterSetName='reset')]
[ipaddress]$resetGateway
)
# SETWATEGAY
if($setGateway) {
# Get current gw
$gwCurrent=(Find-NetRoute -RemoteIPAddress 8.8.8.8)[1].NextHop
# Set new gw
$wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'"
$wmi = $wmi | Where-Object { $_.DefaultIPGateway -eq $gwCurrent }
$wmi.SetGateways($setGateway, 1) | Out-null
# Show new config
ipconfig
# EXIT CODE
if($setGateway -eq (Get-NetIPConfiguration).IPv4DefaultGateway.NextHop) {
Remove-NetRoute -NextHop $gwCurrent -Confirm:$false -ErrorAction SilentlyContinue | Out-Null # Remove old gw
exit 0
} else {
Write-Output "WANING! Gateway address not chnaged!"; exit 1
}
# RESETGATEWAY
} elseif($resetGateWay) {
Remove-NetRoute -NextHop $resetGateway -Confirm:$false
ipconfig /renew
}