前言
修改IP、网关、子网掩码、DNS等配置时需要打开网络配置器配置,有些麻烦。尤其是经常需要重复性操作时(例如去学校图书馆蹭网)。用PowerShell脚本自动修改,方便简单而且十分轻量,程序只需占用1KB空间。
参考
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 param ([int ] $inputNum )$cim = Get-CimInstance Win32_NetworkAdapterConfiguration -Filter "IPEnabled = True" function CheckReturn { param ($obj ) if ($obj .ReturnValue -eq 0 ) { "Successed" } else { "Failed" } } if ($inputNum -eq 0 ) { CheckReturn(Invoke-CimMethod $cim -MethodName EnableDHCP) CheckReturn(Invoke-CimMethod $cim -MethodName SetDNSServerSearchOrder) } elseif (($inputNum -gt 0 ) -and ($inputNum -lt 254 )) { CheckReturn(Invoke-CimMethod $cim -MethodName EnableStatic -Arguments @ { IPAddress = @ ("10.200.200." + $inputNum .ToString()); SubnetMask = @ ("255.255.255.0" ) }) CheckReturn(Invoke-CimMethod $cim -MethodName SetGateways -Arguments @ { DefaultIPGateway = @ ("10.200.200.254" ) }) CheckReturn(Invoke-CimMethod $cim -MethodName SetDNSServerSearchOrder -Arguments @ { DNSServerSearchOrder = @ ("114.114.114.114" ) }) } else { "Input number between 1 to 253 to set network adapter, or 0 to disable network adapter" }
旧的修改方法:Windows
Management Instrumentation (WMI)
参考
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 param ([int ] $inputNum )$wmi = Get-WmiObject Win32_NetworkAdapterConfiguration -Filter "IPEnabled = True" function CheckReturn { param ($obj ) if ($obj .ReturnValue -eq 0 ) { "Successed" } else { "Failed" } } if ($inputNum -eq 0 ){ CheckReturn($wmi .EnableDHCP()) CheckReturn($wmi .SetDNSServerSearchOrder()) } elseif (($inputNum -gt 0 ) -and ($inputNum -lt 254 )){ CheckReturn($wmi .EnableStatic("10.200.200." + $inputNum .ToString(), "255.255.255.0" )) CheckReturn($wmi .SetGateways("10.200.200.254" )) CheckReturn($wmi .SetDNSServerSearchOrder("114.114.114.114" )) } else { "Input number between 1 to 253 to set network adapter, or 0 to disable network adapter" }
附:C#代码
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 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 #nullable enable using System.Management;using System.Runtime.InteropServices;using System.Runtime.Versioning;namespace NetworkAdapter ;[SupportedOSPlatform(nameof(OSPlatform.Windows)) ] public static class NetworkAdapter { public static void Main () { while (true ) { if (!int .TryParse(Console.ReadLine(), out var temp)) continue ; switch (temp) { case 0 : DisableNetworkAdapter(); return ; case > 0 and < 254 : SetNetworkAdapter("10.200.200." + temp, "255.255.255.0" , "10.200.200.254" , "114.114.114.114" ); return ; default : Console.WriteLine("输入错误:请输入机房电脑序号(1-253),输入0恢复默认网络设置" ); break ; } } } private static void SetNetworkAdapter (string ipAddress, string subnetMask, string gateway, string dns ) { if (GetManagementObject() is { } mo) { var inPar = mo.GetMethodParameters("EnableStatic" ); inPar["IPAddress" ] = new [] { ipAddress }; inPar["SubnetMask" ] = new [] { subnetMask }; _ = mo.InvokeMethod("EnableStatic" , inPar, null !); inPar = mo.GetMethodParameters("SetGateways" ); inPar["DefaultIPGateway" ] = new [] { gateway }; _ = mo.InvokeMethod("SetGateways" , inPar, null !); inPar = mo.GetMethodParameters("SetDNSServerSearchOrder" ); inPar["DNSServerSearchOrder" ] = new [] { dns }; _ = mo.InvokeMethod("SetDNSServerSearchOrder" , inPar, null !); } } private static void DisableNetworkAdapter () { if (GetManagementObject() is { } mo) { _ = mo.InvokeMethod("SetDNSServerSearchOrder" , Array.Empty<object >()); _ = mo.InvokeMethod("EnableDHCP" , Array.Empty<object >()); } } private static ManagementObject? GetManagementObject() { var mc = new ManagementClass("Win32_NetworkAdapterConfiguration" ); var moc = mc.GetInstances(); foreach (var o in moc) if ((bool )o["IPEnabled" ]) return (ManagementObject)o; return null ; } }
完整代码(Github)
https://github.com/Poker-sang/NetworkAdapter