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

a.t*_*.t. 1 powershell automation static-ip-address windows-10

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

什么是"简单的" - 实现代码将Windows 10 IP地址设置为静态IP地址,然后再返回动态IP地址?

^注意:Easy是一个指标,用于确保代码及其完整实现尽可能简单,而不是用户无法发现它具有挑战性.

a.t*_*.t. 6

请注意,这是以下内容的实现:http://www.midnightdba.com/Jen/2014/03/configure-static-or-dynamic-ip-and-dns-with-powershell/.所有学分都归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中建议的工作,以便为自己确定这些数字. 在Windows 10中手动设置IP地址的示例.

设置静态IP(半自动):

这意味着您可以通过打开文件(双击您制作的脚本)将IP地址设置为静态,然后通过运行您创建的另一个脚本返回动态IP地址.指令步骤如下:

  1. start>type Powershell>rmb>Open powershell as administrator
  2. (如果第一次无法立即运行脚本,则只执行此步骤.)键入:Set-ExecutionPolicy RemoteSigned -Scope CurrentUser并按Enter键以设置安全策略,以便您可以运行powershell脚本.
  3. 例如,使用内容创建一个.ps1名为的文件:static_ip.ps1c:/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");
    
    Run Code Online (Sandbox Code Playgroud)

    或者只需在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
    
    Run Code Online (Sandbox Code Playgroud)
  4. 然后在powershell中输入:

    cd c:/example_folder
    .\static_ip.ps1
    
    Run Code Online (Sandbox Code Playgroud)

    注意,如果static_ip.ps1文件的路径包含空格,则将change directory-command 更改为:

    cd "c:/example_folder"
    
    Run Code Online (Sandbox Code Playgroud)

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

  1. 创建一个名为example的文本文件,例如dynamic_ip.ps1位于c:/examplefolder包含内容的文件夹中:

    $wmi = Get-WmiObject win32_networkadapterconfiguration -filter "ipenabled ='true'";
    $wmi.EnableDHCP();
    $wmi.SetDNSServerSearchOrder();
    
    Run Code Online (Sandbox Code Playgroud)

    或者只需双击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
    
    Run Code Online (Sandbox Code Playgroud)
  2. 在powershell中:

    cd c:/example_folder
    .\dynamic_ip.ps1
    
    Run Code Online (Sandbox Code Playgroud)

在PowerShell第一次尝试成功之后,您可以通过打开/运行脚本来设置静态IP地址,方法是使用powershell打开它(在资源管理器中,双击文件或right mouse button (rmb)>open with powershell).但为了实现这一点,脚本的路径不能包含任何空格!

补充说明:

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

  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",通常以a结尾 .1

    your_subnetmask 见"your_static_ip_adress"

    your_dns,这可以是你的路由器ip地址,或者例如google 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连接问题排除故障)如果:

    • 你有2个不同的wifi网络(AB),你可以在同一个位置连接
    • 哪里才有B权利"your_routers_ip_adress"/local gateway-adress
    • 你连接到错误的wifi(),你不小心把你设置local IP为(错误),static IPA
    • 然后断开错误的wifi(A),然后再将local IP地址设置为动态,
    • 和(因此)遇到无线麻烦:(保持scanning network requirements).

      然后:

    • 设置local IP地址dynamic.

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

      要么:

    • 设置local IP地址static.

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