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

标签 powershell automation windows-10 static-ip-address

在使用简单的脚本搜索代码以设置静态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>
  • 这将导致屏幕类似于附件图像。您可以在那里手动填写数字。这些数字在您自己的情况下可能会有所不同,您需要做注释3中建议的工作,以便自己确定这些数字。
    Example of manual setting of IP adress in windows 10.

  • 设置静态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 -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
    
  • 在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参见“您的静态_ip_adress”,通常以.1结尾
    your_subnetmask参见“您的静态_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结尾的地址,因为这些地址通常是为网络协议(protocol)保留的。

    3.2不要在IP池的最开始分配地址,例如10.0.0.1作为起始地址始终为路由器保留。即使出于安全考虑更改了路由器的IP地址,我们仍然建议不要分配计算机。

    3.3不要在可用的专用IP地址池之外分配地址。这意味着,如果路由器的池为10.0.0.0到10.255.255.255,则您分配的每个IP(请牢记前两个规则)都应在该范围内。
  • 这是(半)自动等效项,它是手动填写此帖子的第一张图的数据,以便设置静态IP。
  • (Wifi连接问题疑难解答)如果:
  • 您有2个不同的wifi网络(AB),您都可以在同一位置连接到它们
  • 其中只有B具有正确的"your_routers_ip_adress"/local gateway-adress
  • 当您连接到错误的wifi(local IP),
  • 时,您不小心将static IP设置为(错误的)A
  • 然后断开错误的wifi(A),然后再将local IP地址设置为dynamic,
  • 和(因此)经历了wifi问题:(使scanning network requirements保持了)。

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

    或者:
  • local IP地址设置为static
  • 重新连接到错误的wifi网络(A)。
  • 将其重新设置为static,然后再次设置为dynamic
  • 与wifi断开连接(A)。
  • 现在,您应该能够再次正确连接到两个wifi网络。
  • 关于powershell - 如何在Windows 10中设置静态IP地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51272961/

    相关文章:

    elasticsearch - 在elastalert-test-rule或执行规则时未收到警报

    .net - 执行 vsvars32.bat 命令后,Powershell 没有正确的路径信息

    powershell - 如何使用 'Match' cmdlet在PowerShell中过滤字符串数组(或列表)?

    testing - 登录后重定向逻辑不适用于 TestCafe 角色

    python - 为什么python子进程在运行某个命令时停止执行?

    android - 针对不同的 Android 操作系统版本显示不同的 xpath

    powershell - 使用 System.IO.FileSystemWatcher 后使用 Powershell 循环文件

    windows - 如果找到文件,则使get-Childitem停止

    winapi - GetWindowRect 返回包含 "invisible"边框的大小

    python-3.x - 我可以使用 pipenv 创建两个虚拟环境,每个环境都有不同的 python 版本(即 3.7 和 3.6)吗?