windows10如何设置静态IP地址?

How to set a static IP address in windows 10?

在搜索使用简单脚本设置静态 IP 地址的代码后,我无法在 Whosebug 上找到完整且易于实施的答案。这让我想到了以下问题:

什么是 "easy^" 实现代码来将您的 windows 10 IP 地址设置为静态 IP 地址,然后再返回动态 IP 地址?

^ 注:简单是指确保代码及其完整实现尽可能简单的指标,而不是用户觉得没有挑战性。

请注意,这是执行:http://www.midnightdba.com/Jen/2014/03/configure-static-or-dynamic-ip-and-dns-with-powershell/. All credits go to MidnightDBA。我希望它对某人有所帮助!

手动将 IP 地址设置为静态

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

  2. 这应该会导致屏幕与所附图像相似。您可以在那里手动填写数字。这些数字在您自己的情况下(可能)会有所不同,您需要执行注释 3 中建议的工作来自己确定这些数字。

设置静态IP(半自动):

这意味着您将能够通过打开文件(双击您创建的脚本)将 IP 地址设置为静态,并通过 运行 运行另一个脚本返回动态 IP 地址你做了。指令步骤如下:

  1. start>type Powershell>rmb>Open powershell as administrator
  2. (只有当您第一次不能立即 运行 脚本时才执行此步骤。)键入:Set-ExecutionPolicy RemoteSigned -Scope CurrentUser 并按回车键,以设置安全策略,以便您可以 运行 一个 powershell 脚本。
  3. 创建一个 .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: 
    Stop-Process -Id $PID
    
  4. 然后在powershell中输入:

    cd c:/example_folder
    .\static_ip.ps1
    

    注意,如果 static_ip.ps1 文件的路径包含 space,请将 change directory-命令更改为:

    cd "c:/example_folder"
    

再次使 IP 动态化(半自动):

  1. 创建一个名为 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: 
    Stop-Process -Id $PID
    
  2. 在 powershell 中:

    cd c:/example_folder
    .\dynamic_ip.ps1
    

在您第一次在 powershell 中成功尝试后,您可以简单地通过 opening/running 脚本设置一个静态 IP 地址,方法是用 powershell 打开它(在资源管理器中,双击该文件,或 right mouse button (rmb)>open with powershell). 但要使其正常工作,脚本路径不能包含任何 spaces!

补充说明:

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

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

  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. 这是(半)自动相当于手动填写此 post 第一个数字的数据以设置静态 IP。

  5. (Wifi连接问题排查)如果:

    • 您有 2 个不同的 wifi 网络(AB),您可以在同一位置连接到它们
    • 只有B有权利"your_routers_ip_adress"/local gateway-adress
    • 并且您不小心将 local IP 设置为(错误的)static IP,同时连接到错误的 wifi (A),
    • 然后断开错误的 wifi (A),然后再将 local IP 地址设置为动态,
    • 并且(因此)遇到 wifi 问题:(保持 scanning network requirements)。

      然后:

    • local IP地址设置为dynamic

    • 重新连接到错误的 wifi 网络 (A)。
    • 将其设置回 static,然后再次设置为 dynamic
    • 断开 wifi 连接(A)。
    • 现在您应该能够再次正确连接到两个 wifi 网络。

      或:

    • local IP地址设置为static

    • 重新连接到错误的 wifi 网络 (A)。
    • 将其设置回 static,然后再次设置为 dynamic
    • 断开 wifi 连接(A)。
    • 现在您应该能够再次正确连接到两个 wifi 网络。

有关 GUI 和 PowerShell 的不错信息。

当您通过PowerShell 手动分配IP 时,DNS 服务器IP 很重要。此外,它可以通过命令提示符完成,这在远程管理 PC 时很有用。下面post有信息。也许您可以考虑在 post.

中添加这些步骤

https://tinylaptop.net/how-to-configure-setup-static-ip-on-windows-10-laptop/