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
| #首先查看当前计算机上的网络适配器信息
$i = 0
${ip rules} = "\b[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\.[0-9]{1,3}\b"
Get-NetAdapter | ft -AutoSize name, InterfaceDescription, MacAddress, InterfaceIndex, Status
$netid = Read-Host "请输入要设置的网络适配器序号"
foreach ($id in (Get-NetAdapter).ifIndex)
{
if ($id -eq $netid)
{
$i++
}
}
if ($i -eq 0)
{
Write-Warning "输入的网络适配器序号不存在";
break
}
if ((Get-NetAdapter -InterfaceIndex $netid).Status -eq "Disconnected")
{
"当前适配器未连接";
break
}
$ip = Read-Host "请输入要设置的IP地址"
if ($ip -notmatch ${ip rules})
{
Write-Warning "请输入正确的IP地址";
break
}
$dnsip = Read-Host "请输入DNS服务器的IP地址"
if ($dnsip -notmatch ${ip rules})
{
Write-Warning "请输入正确的IP地址";
break
}
$mask = Read-Host "请输入子网掩码的长度"
if ((Get-NetAdapter -InterfaceIndex $netid).Status -eq "Disconnected")
{
Write-Warning "当前适配器未连接";
break
}
elseif ((Get-NetAdapter -InterfaceIndex $netid).Status -eq "up")
{
set-DnsClientServerAddress -InterfaceIndex $netid -ServerAddresses $dnsip
if ((Get-NetIPAddress -InterfaceIndex $netid -AddressFamily IPv4).IPv4Address.StartsWith("169"))
{
New-NetIPAddress -InterfaceIndex $netid -IPAddress $ip -PrefixLength $mask
}
elseif ((Get-NetIPAddress -InterfaceIndex $netid -AddressFamily IPv4).IPv4Address.StartsWith("192"))
{
Remove-NetIPAddress -InterfaceIndex $netid -AddressFamily IPv4
New-NetIPAddress -InterfaceIndex $netid -IPAddress $ip -PrefixLength $mask
}
}
|