使用简单的脚本搜索代码以设置静态IP地址后,我在StackOverflow上找不到完整且易于实现的答案。这使我想到了以下问题:
将Windows 10 IP地址设置为静态IP地址,然后再次设置为动态IP地址是什么“容易^”实现代码?
^注意:“简单”是确保代码及其完整实现尽可能简单的一种指标,而不是使用户觉得它具有挑战性。
答案 0 :(得分:6)
请注意,这是http://www.midnightdba.com/Jen/2014/03/configure-static-or-dynamic-ip-and-dns-with-powershell/的实现。所有学分都归MidnightDBA。我希望它对某人有益!
要将IP地址手动设置为静态
Start>control panel>Network and Internet>Network and Sharing Center>Change adapter settings>rmb on the ethernet/wifi/connection that is in use>properties>Select: Internet Protocol Version 4(TCP/IPv4)>Properties>
这将导致屏幕类似于所附图像。您可以在那里手动填写数字。这些数字在您自己的情况下可能会有所不同,您需要做注释3中建议的工作,以便自己确定这些数字。
要(半自动)设置静态IP:
这意味着您可以通过打开文件(双击创建的脚本)将IP地址设置为静态,并通过运行创建的另一个脚本将IP地址设置为动态IP地址。指示步骤如下:
start>type Powershell>rmb>Open powershell as administrator
Set-ExecutionPolicy RemoteSigned -Scope CurrentUser
并按Enter,以设置安全策略,以便您可以运行Powershell脚本。 创建一个名为.ps1
的文件,例如static_ip.ps1
(例如c:/example_folder
)中的内容:
$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");
或仅通过双击static_ip.ps1
脚本即可设置静态IP:
(请注意示例值填写)
# 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://stackoverflow.com/questions/14874619/powershell-exit-doesnt-really-exit
Stop-Process -Id $PID
然后在powershell中输入:
cd c:/example_folder
.\static_ip.ps1
请注意,如果static_ip.ps1
文件的路径包含空格,请将change directory
命令更改为:
cd "c:/example_folder"
要使IP重新(半自动)动态,请执行以下操作:
创建一个文本文件,例如dynamic_ip.ps1
,位于文件夹c:/examplefolder
中的内容:
$wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled ='true'";
$wmi.EnableDHCP();
$wmi.SetDNSServerSearchOrder();
或仅需双击dynamic_ip.ps1
脚本即可对其进行更改:
#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://stackoverflow.com/questions/14874619/powershell-exit-doesnt-really-exit
Stop-Process -Id $PID
在powershell中:
cd c:/example_folder
.\dynamic_ip.ps1
成功在Powershell中首次尝试后,您可以通过使用powershell打开脚本来打开/运行脚本,从而简单地设置静态IP地址(在资源管理器中,双击文件,或right mouse button (rmb)>open with powershell
)。 但这要起作用,脚本路径不能包含任何空格!
其他说明:
如果您再次离开家庭网络,请不要忘记重新设置IP地址的动态性,否则当您尝试在其他wifi /以太网网络中访问Internet时就会遇到问题!
your_static_ip_adress
:您可以通过以下方式读取动态IP地址和路由器IP地址:start>type cmd>open command prompt>type: ipconfig
,或键入:ipconfig -all
。*此外,以上注释中描述的规则,通常适用。
your_routers_ip_adress
参见“ your_static_ip_adress”,通常以.1
your_subnetmask
参见“ your_static_ip_adress”
your_dns
,它可以是路由器的IP地址,例如googles 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"/local gateway-adress
权限的地方local IP
)时意外地将static IP
设置为(错误的)A
,A
地址重新设置为动态之前,断开了错误的wifi(local IP
),并且(因此)遇到wifi问题:(保持scanning network requirements
)。
然后选择:
将local IP
地址设置为dynamic
。
A
)。static
,然后再次设置为dynamic
。A
)的连接。现在您应该可以再次正确连接到两个wifi网络。
或者:
将local IP
地址设置为static
。
A
)。static
,然后再次设置为dynamic
。A
)的连接。答案 1 :(得分:0)
具有GUI和PowerShell的良好信息。
通过PowerShell手动分配IP时,DNS服务器IP很重要。另外,它可以通过命令提示符完成,这在远程管理PC时很有用。以下帖子具有相关信息。也许您可以考虑在帖子中添加这些步骤。
https://tinylaptop.net/how-to-configure-setup-static-ip-on-windows-10-laptop/