在使用简单的脚本搜索代码以设置静态IP地址后,我在StackOverflow上找不到完整且易于实现的答案。这使我想到了以下问题:
将Windows 10 IP地址设置为静态IP地址,然后再次设置为动态IP地址的“易于^”实现代码是什么?
^注意:Easy旨在确保代码及其完整实现尽可能简单,而不是使用户感到挑战。
最佳答案
请注意,这是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>
设置静态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
cd c:/example_folder
.\static_ip.ps1
注意,如果
static_ip.ps1
文件的路径包含空格,请将change directory
-command更改为: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
cd c:/example_folder
.\dynamic_ip.ps1
成功在Powershell中首次尝试后,您可以通过使用powershell打开脚本来打开/运行脚本,从而简单地设置静态IP地址(在资源管理器中,双击文件或
right mouse button (rmb)>open with powershell
)。 但这要起作用,脚本路径不能包含任何空格! 其他说明:
your_static_ip_adress
:您可以通过以下方式来读取动态IP地址和路由器IP地址:start>type cmd>open command prompt>type: ipconfig
或键入:ipconfig -all
。*此外,上述说明中所述的规则通常适用。your_routers_ip_adress
参见“您的静态_ip_adress”,通常以.1
结尾your_subnetmask
参见“您的静态_ip_adress”your_dns
,它可以是路由器的IP地址,例如googles DNS 8.8.8.8
。 资源:
https://www.howtogeek.com/184310/ask-htg-should-i-be-setting-static-ip-addresses-on-my-router/
3.1不要分配以.0或.255结尾的地址,因为这些地址通常是为网络协议(protocol)保留的。
3.2不要在IP池的最开始分配地址,例如10.0.0.1作为起始地址始终为路由器保留。即使出于安全考虑更改了路由器的IP地址,我们仍然建议不要分配计算机。
3.3不要在可用的专用IP地址池之外分配地址。这意味着,如果路由器的池为10.0.0.0到10.255.255.255,则您分配的每个IP(请牢记前两个规则)都应在该范围内。
A
和B
),您都可以在同一位置连接到它们B
具有正确的"your_routers_ip_adress"/local gateway-adress
local IP
),static IP
设置为(错误的)A
。A
),然后再将local IP
地址设置为dynamic,scanning network requirements
保持了)。然后:
local IP
地址设置为dynamic
。 A
)。 static
,然后再次设置为dynamic
。 A
)。 或者:
local IP
地址设置为static
。 A
)。 static
,然后再次设置为dynamic
。 A
)。 关于powershell - 如何在Windows 10中设置静态IP地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51272961/