Untitled
user_8942811
plain_text
a year ago
1.1 kB
9
Indexable
# Địa chỉ IP của gateway
$gatewayIP = "192.168.120.1"
#tên của cổng cần change khi lỗi mạng
$name = "Local Area Connection"
# Kiểm tra kết nối tới gateway
if (!(Test-Connection -ComputerName $gatewayIP -Count 2 -Quiet)) {
# Nếu không có kết nối, đổi sang IP động
Write-Output "lost connection switch to dhcp."
$adapter = Get-NetAdapter | Where-Object { $_.Name -eq $name }
$interface = $adapter | Get-NetIPInterface -AddressFamily IPv4
if( $interface.Dhcp -eq "Disabled" ) {
# remote gateway
$interface | Remove-NetRoute -Confirm:$false -NextHop $gatewayIP -AddressFamily IPv4
}
Set-DnsClientServerAddress -InterfaceAlias $adapter.InterfaceAlias -ServerAddresses ("1.1.1.1","1.0.0.1")
Set-NetIPInterface -InterfaceAlias $adapter.InterfaceAlias -Dhcp Enabled
#disable interface
Disable-NetAdapter -Name $name -Confirm:$false
#enable interface
Enable-NetAdapter -Name $name -Confirm:$false
} else {
Write-Output "Connected."
}Editor is loading...
Leave a Comment