如何在Windows 10中设置静态IP地址?

3
在寻找一个使用简单脚本来设置静态IP地址的代码时,我在StackOverflow上没有找到完整且易于实现的答案。这让我想到了以下问题:
什么是一种易于实现的代码,可以将你的Windows 10 IP地址设置为静态IP地址,并再次恢复为动态IP地址?
注意:这里的“易于实现”是指确保代码及其完整实现尽可能简单,而不是用户不能找到具有挑战性的内容。

1
@Bill_Stewart 这个问题是如何在Windows 10中设置静态IP,但表述非常糟糕。这是一个问答问题。 - Maximilian Burszley
1
请将问题以实际问题的形式重新表述。这里有一个例子,我提出了一个我已经知道答案的问题,然后回答了它。 - codewario
感谢您的评论,问题确实没有表达清楚。我已经修改了问题,将完整明确的问题包含在此帖子的“问题文本”正文中。 非常感谢您提供的实现建议示例,@BendertheGreatest。 - a.t.
2个回答

9
请注意,这是以下实现的翻译: http://www.midnightdba.com/Jen/2014/03/configure-static-or-dynamic-ip-and-dns-with-powershell/。所有功劳归属于MidnightDBA。希望对某些人有所帮助! 手动将IP地址设置为静态
  1. 开始>控制面板>网络和 Internet>网络和共享中心>更改适配器设置>鼠标右键单击正在使用的以太网/无线连接>属性>选择:Internet 协议版本 4(TCP/IPv4)>属性>
  2. 此步骤会显示类似于附图的屏幕。在那里您可以手动填写数字。这些数字可能在您自己的情况下不同,您需要执行注释3中建议的工作来确定自己的数字。 Example of manual setting of IP adress in windows 10.
半自动设置静态IP: 这意味着您可以通过打开一个文件(双击已创建的脚本),将IP地址设置为静态,通过运行另一个已创建的脚本将其恢复为动态IP地址。以下是指导步骤:
  1. start>type Powershell>rmb>Open powershell as administrator
  2. (Only do this step if you can not immediately run the script the first time.) Type: Set-ExecutionPolicy RemoteSigned -Scope CurrentUser and hit enter, to set the security policy so that you can run a powershell script.
  3. 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
    
  4. 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"
    
使IP地址再次变为动态(半自动):
  1. 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
    
  2. In powershell:

    cd c:/example_folder
    .\dynamic_ip.ps1
    

当你在powershell中第一次成功尝试后,你可以通过打开/运行脚本来设置静态IP地址,方法是使用powershell打开文件(在资源管理器中双击文件或右键单击>使用powershell打开)。但为了使其正常工作,脚本路径不能包含空格!

附加说明:

  1. 如果您再次离开家庭网络,请不要忘记将IP地址设置为动态,否则在尝试访问其他WiFi /以太网网络时可能会出现问题!

  2. 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

  3. 确定静态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地址(记住前两个规则)都应在该范围内。

  4. 这是手动填写本文第一张图数据以设置静态IP的(半)自动化等效方法。

  5. (WiFi连接问题排除) 如果:

    • 您可以同时连接到两个不同的WiFi网络(AB
    • 其中只有B具有正确的"your_routers_ip_adress"/本地网关地址
    • 并且您在连接到错误的WiFi(A)时意外将本地IP设置为(错误的)静态IP,
    • 然后在将本地IP地址重新设置为动态之前断开了错误的WiFi(A),
    • 并且(因此)遇到WiFi问题:(保持扫描网络要求)。

      然后:

    • 本地IP地址设置为动态

    • 重新连接到错误的WiFi网络(A)。
    • 将其设置回静态,再设置为动态
    • 断开WiFi(A)的连接。
    • 现在您应该能够正确连接到两个WiFi网络。

      或者:

    • 本地IP地址设置为静态

    • 重新连接到错误的WiFi网络(A)。

0

2
虽然这个链接可能回答了问题,但最好在此处包含答案的基本部分并提供参考链接。仅有链接的答案如果链接页面发生更改可能会变得无效。 - RtmY

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接