什么是一种易于实现的代码,可以将你的Windows 10 IP地址设置为静态IP地址,并再次恢复为动态IP地址?
注意:这里的“易于实现”是指确保代码及其完整实现尽可能简单,而不是用户不能找到具有挑战性的内容。
开始>控制面板>网络和 Internet>网络和共享中心>更改适配器设置>鼠标右键单击正在使用的以太网/无线连接>属性>选择:Internet 协议版本 4(TCP/IPv4)>属性>
start>type Powershell>rmb>Open powershell as administrator
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
and hit enter, to set the security policy so that you can run a powershell script. create a .ps1
file named e.g. static_ip.ps1
in for example c:/example_folder
with the content:
$wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled ='true'";
$wmi.EnableStatic("your_static_ip_adress", "your_subnetmask");
$wmi.SetGateways("your_routers_ip_adress", 1);
$wmi.SetDNSServerSearchOrder("your_dns");
OR to set the static IP with just a single double click on the static_ip.ps1
script:
(Note example values filled in)
# 18-07-20 Todo: add wifi network detection that automatically triggers setting a static IP and back dynamic IP.
# First ensure the script is automatically ran as administrator, else it appearently does not have the privileges to change the local IP adress:
$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
$testadmin = $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
if ($testadmin -eq $false) {
Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
exit $LASTEXITCODE
}
# Next set it static:
$wmi.EnableStatic("192.21.89.5", "255.255.254.0");
$wmi.SetGateways("192.21.89.1", 1);
$wmi.SetDNSServerSearchOrder("192.21.89.1");
# Now close the window this has just created.
# This leaves other Powershell windows open if they were already open before you ran this script.
# Also, It yields an error with a $ sign at the start of the line.
# Source: https://dev59.com/mG7Xa4cB1Zd3GeqPqHYz
Stop-Process -Id $PID
Then in powershell enter:
cd c:/example_folder
.\static_ip.ps1
Note, if the path to the static_ip.ps1
file contains a space change the change directory
-command to:
cd "c:/example_folder"
Create a text file named for example dynamic_ip.ps1
located e.g. in folder c:/examplefolder
with content:
$wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled ='true'";
$wmi.EnableDHCP();
$wmi.SetDNSServerSearchOrder();
OR to just change it with a single double-click on the dynamic_ip.ps1
script:
#18-07-20 Todo: add wifi network detection that automatically triggers setting a static IP and back dynamic IP.
# First ensure the script is automatically ran as administrator, else it appearently does not have the privileges to change the local IP adress:
$currentUser = New-Object Security.Principal.WindowsPrincipal $([Security.Principal.WindowsIdentity]::GetCurrent())
$testadmin = $currentUser.IsInRole([Security.Principal.WindowsBuiltinRole]::Administrator)
if ($testadmin -eq $false) {
Start-Process powershell.exe -Verb RunAs -ArgumentList ('-noprofile -noexit -file "{0}" -elevated' -f ($myinvocation.MyCommand.Definition))
exit $LASTEXITCODE
}
# Next set it dynamic:
$wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled ='true'";
$wmi.EnableDHCP();
$wmi.SetDNSServerSearchOrder();
# Now close the window this has just created.
# This leaves other Powershell windows open if they were already open before you ran this script.
# Also, It yields an error with a $ sign at the start of the line.
# Source: https://dev59.com/mG7Xa4cB1Zd3GeqPqHYz
Stop-Process -Id $PID
In powershell:
cd c:/example_folder
.\dynamic_ip.ps1
当你在powershell中第一次成功尝试后,你可以通过打开/运行脚本来设置静态IP地址,方法是使用powershell打开文件(在资源管理器中双击文件或右键单击>使用powershell打开)。但为了使其正常工作,脚本路径不能包含空格!
附加说明:
如果您再次离开家庭网络,请不要忘记将IP地址设置为动态,否则在尝试访问其他WiFi /以太网网络时可能会出现问题!
your_static_ip_adress
: 您可以通过以下方式读取您的动态IP地址和路由器IP地址:开始>键入cmd>打开命令提示符>键入:ipconfig
或键入:ipconfig -all
。 此外,通常适用于上面说明的规则。
your_routers_ip_adress
参见“your_static_ip_adress”,通常以.1
结尾。
your_subnetmask
参见“your_static_ip_adress”。
your_dns
,这可以是您的路由器IP地址,也可以是例如谷歌DNS 8.8.8.8
。
确定静态IP地址的规则: 来源:https://www.howtogeek.com/184310/ask-htg-should-i-be-setting-static-ip-addresses-on-my-router/
3.1 不要分配以 .0 或 .255 结尾的地址,因为这些地址通常保留用于网络协议。
3.2 不要分配给IP池的起始地址,例如10.0.0.1,因为起始地址始终保留给路由器。 即使出于安全目的更改了路由器的IP地址,我们仍建议不要分配计算机。
3.3 不要分配超出私有IP地址可用池的地址。 这意味着如果您的路由器池是10.0.0.0到10.255.255.255,那么您分配的每个IP地址(记住前两个规则)都应在该范围内。
这是手动填写本文第一张图数据以设置静态IP的(半)自动化等效方法。
(WiFi连接问题排除) 如果:
A
和B
)B
具有正确的"your_routers_ip_adress"/本地网关地址
A
)时意外将本地IP
设置为(错误的)静态IP
,本地IP
地址重新设置为动态之前断开了错误的WiFi(A
),并且(因此)遇到WiFi问题:(保持扫描网络要求
)。
然后:
将本地IP
地址设置为动态
。
A
)。静态
,再设置为动态
。A
)的连接。现在您应该能够正确连接到两个WiFi网络。
或者:
将本地IP
地址设置为静态
。
A
)。非常好的GUI和PowerShell相关信息。
当你通过PowerShell手动分配IP时,DNS服务器IP至关重要。此外,也可以通过命令提示符来完成,这在远程管理PC时非常有用。以下文章提供了相关信息。或许你可以考虑在你的文章中添加这些步骤。
https://tinylaptop.net/how-to-configure-setup-static-ip-on-windows-10-laptop/