You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

使用PowerShell为笔记本配置多网络静态IP替代netsh脚本

PowerShell脚本实现多地点静态IP切换(替代netsh)

我来帮你搞定这个PowerShell脚本的问题!微软确实在逐步淘汰netsh,转向PowerShell的NetTCPIP模块,下面是一个完整的、可交互的脚本,用来在多个静态IP配置之间切换,还支持切回DHCP模式:

完整脚本示例

# 检查是否以管理员身份运行(修改网络配置需要此权限)
$currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent())
if (-not $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) {
    Write-Error "请右键点击PowerShell,选择「以管理员身份运行」后执行此脚本!"
    exit 1
}

# 定义各个地点的网络配置
# 注意:替换InterfaceAlias为你的网卡名称(可通过Get-NetAdapter命令查看)
$networkConfigs = @{
    "办公室" = @{
        InterfaceAlias = "以太网"
        IPAddress      = "192.168.1.100"
        PrefixLength   = 24  # 对应子网掩码255.255.255.0
        DefaultGateway = "192.168.1.1"
        DnsServers     = @("192.168.1.2", "192.168.1.3")
    }
    "家里" = @{
        InterfaceAlias = "以太网"
        IPAddress      = "192.168.3.20"
        PrefixLength   = 24
        DefaultGateway = "192.168.3.1"
        DnsServers     = @("8.8.8.8", "8.8.4.4")
    }
    "客户现场" = @{
        InterfaceAlias = "以太网"
        IPAddress      = "10.0.0.50"
        PrefixLength   = 24
        DefaultGateway = "10.0.0.1"
        DnsServers     = @("10.0.0.2")
    }
    "自动获取IP(DHCP)" = @{
        InterfaceAlias = "以太网"
    }
}

# 显示交互选择菜单
Write-Host "`n请选择要切换的网络位置:" -ForegroundColor Cyan
$index = 1
$networkConfigs.Keys | ForEach-Object {
    Write-Host "$index. $_"
    $index++
}

# 获取用户选择
do {
    $choice = Read-Host "`n输入选项编号"
    if ($choice -match "^[1-$($networkConfigs.Count)]$") {
        break
    }
    Write-Warning "输入无效,请输入1到$($networkConfigs.Count)之间的数字"
} while ($true)

$selectedLocation = ($networkConfigs.Keys)[$choice - 1]
$config = $networkConfigs[$selectedLocation]
$adapter = Get-NetAdapter -Name $config.InterfaceAlias

# 根据选择执行配置切换
if ($selectedLocation -eq "自动获取IP(DHCP)") {
    # 切换到DHCP模式
    Set-NetIPInterface -InterfaceIndex $adapter.InterfaceIndex -Dhcp Enabled
    Set-DnsClientServerAddress -InterfaceIndex $adapter.InterfaceIndex -ResetServerAddresses
    Write-Host "`n已成功切换到【$selectedLocation】模式" -ForegroundColor Green
} else {
    # 切换到静态IP模式:先清理旧配置,再设置新配置
    Set-NetIPInterface -InterfaceIndex $adapter.InterfaceIndex -Dhcp Disabled
    Get-NetIPAddress -InterfaceIndex $adapter.InterfaceIndex -AddressFamily IPv4 | Remove-NetIPAddress -Confirm:$false

    New-NetIPAddress -InterfaceIndex $adapter.InterfaceIndex `
        -IPAddress $config.IPAddress `
        -PrefixLength $config.PrefixLength `
        -DefaultGateway $config.DefaultGateway

    Set-DnsClientServerAddress -InterfaceIndex $adapter.InterfaceIndex `
        -ServerAddresses $config.DnsServers

    Write-Host "`n已成功切换到【$selectedLocation】网络配置" -ForegroundColor Green
}

关键说明

  • 管理员权限:必须以管理员身份运行脚本,否则无法修改网络配置
  • 网卡名称替换:运行Get-NetAdapter命令可以查看你的网卡名称(比如「Wi-Fi」「以太网」),替换脚本中的InterfaceAlias值
  • 配置自定义:根据你的实际网络参数,修改各个地点的IPAddress、DefaultGateway、DnsServers等字段
  • PrefixLength说明:这是子网掩码的无类别域间路由(CIDR)表示,比如24对应255.255.255.0,16对应255.255.0.0

脚本运行流程

  1. 检查是否有管理员权限,没有则提示退出
  2. 显示预设的网络位置菜单
  3. 等待用户输入有效选项
  4. 根据选择:
    • 如果选DHCP:启用网卡的DHCP自动获取,重置DNS为自动
    • 如果选静态IP:先禁用DHCP、移除旧的静态IP,再设置新的IP、网关和DNS

内容的提问来源于stack exchange,提问作者Gary Henning

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.22 08:41:22