powershell:
代码语言:javascript复制#getgw
$gw=(Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'").defaultipgateway
if([string]::IsNullOrEmpty($gw)){
$gw=(Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled = 'true'").DHCPServer
}
if([string]::IsNullOrEmpty($gw)){
$interface = (Get-WmiObject Win32_NetworkAdapter -Filter 'NetEnabled=True').GUID
$string=reg query "HKLMSYSTEMCurrentControlSetServicesTcpipParametersInterfaces$interface" | findstr /i DhcpServer
$gw = $string -split 's ' | Select-Object -Last 1
}
#set route metric
$NICName=(Get-WmiObject Win32_NetworkAdapter -Filter 'NetEnabled=True').NetConnectionID
foreach ($interfacename in $NICName) {
netsh interface ip set interface interface="$interfacename" metric=1
}
#set defaultgw metric
$interfaces = Get-WmiObject Win32_NetworkAdapter -Filter 'NetEnabled=True' | ForEach-Object { $_.GUID }
foreach ($interfaceGuid in $interfaces) {
$registryPath = "HKLM:SYSTEMCurrentControlSetServicesTcpipParametersInterfaces$interfaceGuid"
REG ADD "HKLMSYSTEMCurrentControlSetServicesTcpipParametersInterfaces$interfaceGuid" /v DefaultGatewayMetric /t REG_MULTI_SZ /d "1 " /f
REG ADD "HKLMSYSTEMCurrentControlSetServicesTcpipParametersInterfaces$interfaceGuid" /v DefaultGateway /t REG_MULTI_SZ /d "$gw " /f
REG ADD "HKLMSYSTEMCurrentControlSetServicesTcpipParametersInterfaces$interfaceGuid" /v DhcpDefaultGateway /t REG_MULTI_SZ /d "$gw " /f
}