首页
社区
课程
招聘
[继续开源一套] 一件设置静态IP PS1脚本
发表于: 2026-6-7 22:56 2198

[继续开源一套] 一件设置静态IP PS1脚本

2026-6-7 22:56
2198
收藏
免费 18
打赏
分享
最新回复 (8)
雪    币: 1213
活跃值: (1919)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
2
学习一下
2026-6-8 11:14
0
雪    币: 858
活跃值: (1563)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
3
学习一下
2026-6-8 18:15
0
雪    币: 3039
活跃值: (2605)
能力值: ( LV6,RANK:80 )
在线值:
发帖
回帖
粉丝
4
学习
2026-6-8 18:30
0
雪    币: 6927
活跃值: (6970)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
5
66
2026-6-8 19:47
0
雪    币: 495
活跃值: (1163)
能力值: ( LV3,RANK:30 )
在线值:
发帖
回帖
粉丝
6
谢谢分享发   发
2026-6-8 19:50
0
雪    币: 4174
活跃值: (2928)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
7
学习学习
2026-6-11 09:56
0
雪    币: 2543
活跃值: (8277)
能力值: ( LV2,RANK:10 )
在线值:
发帖
回帖
粉丝
8
谢谢分享
2026-6-11 11:01
0
雪    币: 1040
活跃值: (1915)
能力值: ( LV2,RANK:15 )
在线值:
发帖
回帖
粉丝
9
# 以管理员身份运行此脚本
# 执行策略放行
Set-ExecutionPolicy -ExecutionPolicy Bypass -Scope Process -Force
Clear-Host

# ===================== 可自定义静态参数 =====================
# IPv4 静态配置
$StaticIPv4Addr    = "10.0.0.2"
$StaticIPv4Prefix  = 24
$StaticIPv4Gateway = "10.0.0.1"
$StaticIPv4Dns1    = "114.114.114.114"
$StaticIPv4Dns2    = "8.8.8.8"

# IPv6 静态配置
$StaticIPv6Addr    = "2409:xxxx:xxxx:xxxx::2"
$StaticIPv6Prefix  = 64
$StaticIPv6Gateway = "2409:xxxx:xxxx:xxxx::1"
$StaticIPv6Dns1    = "2400:da00::6666"
$StaticIPv6Dns2    = "2001:4860:4860::8888"

# ===================== 获取活动网卡 =====================
$adapter = Get-NetAdapter | Where-Object { $_.Status -eq "Up" } | Select-Object -First 1
if (-not $adapter) {
    Write-Host "错误:未检测到活动的网络适配器!" -ForegroundColor Red
    pause
    exit
}
$ifIndex = $adapter.ifIndex
$nicName = $adapter.Name

# ===================== 功能菜单 =====================
Write-Host "=============================================" -ForegroundColor Cyan
Write-Host "          网络配置工具 (IPv4 + IPv6)         " -ForegroundColor Cyan
Write-Host "  当前网卡:$nicName  接口索引:$ifIndex" -ForegroundColor Cyan
Write-Host "=============================================" -ForegroundColor Cyan
Write-Host "  1. 切换为 DHCP 自动获取 (IPv4+IPv6)"
Write-Host "  2. 手动配置静态 IP (IPv4+IPv6)"
Write-Host "=============================================" -ForegroundColor Cyan
$choice = Read-Host "请输入选项 [1/2]"

# ===================== 分支逻辑 =====================
switch ($choice)
{
    "1" {
        Write-Host "`n正在切换为 DHCP 自动获取..." -ForegroundColor Yellow

        # IPv4 开启DHCP、清空DNS
        Set-NetIPInterface -InterfaceIndex $ifIndex -AddressFamily IPv4 -Dhcp Enabled
        Set-DnsClientServerAddress -InterfaceIndex $ifIndex -ResetServerAddresses

        # IPv6 开启DHCPv6、清空DNS
        Set-NetIPInterface -InterfaceIndex $ifIndex -AddressFamily IPv6 -Dhcp Enabled
        Set-DnsClientServerAddress -InterfaceIndex $ifIndex -ResetServerAddresses

        Write-Host "`n✅ DHCP 配置完成!" -ForegroundColor Green
    }

    "2" {
        Write-Host "`n正在配置静态 IPv4 + IPv6 ..." -ForegroundColor Yellow

        # 先关闭DHCP,再配置静态IPv4
        Set-NetIPInterface -InterfaceIndex $ifIndex -AddressFamily IPv4 -Dhcp Disabled
        New-NetIPAddress -InterfaceIndex $ifIndex -IPAddress $StaticIPv4Addr -PrefixLength $StaticIPv4Prefix -DefaultGateway $StaticIPv4Gateway -AddressFamily IPv4 | Out-Null
        Set-DnsClientServerAddress -InterfaceIndex $ifIndex -ServerAddresses $StaticIPv4Dns1, $StaticIPv4Dns2

        # 配置静态IPv6
        Set-NetIPInterface -InterfaceIndex $ifIndex -AddressFamily IPv6 -Dhcp Disabled
        New-NetIPAddress -InterfaceIndex $ifIndex -IPAddress $StaticIPv6Addr -PrefixLength $StaticIPv6Prefix -DefaultGateway $StaticIPv6Gateway -AddressFamily IPv6 | Out-Null
        Set-DnsClientServerAddress -InterfaceIndex $ifIndex -ServerAddresses $StaticIPv6Dns1, $StaticIPv6Dns2

        Write-Host "`n✅ 静态IP配置完成!" -ForegroundColor Green
        Write-Host "---------------------------------------------"
        Write-Host "IPv4 地址: $StaticIPv4Addr/$StaticIPv4Prefix"
        Write-Host "IPv4 网关: $StaticIPv4Gateway"
        Write-Host "IPv4 DNS : $StaticIPv4Dns1 , $StaticIPv4Dns2"
        Write-Host "---------------------------------------------"
        Write-Host "IPv6 地址: $StaticIPv6Addr/$StaticIPv6Prefix"
        Write-Host "IPv6 网关: $StaticIPv6Gateway"
        Write-Host "IPv6 DNS : $StaticIPv6Dns1 , $StaticIPv6Dns2"
    }

    default {
        Write-Host "`n❌ 输入无效,请重新运行脚本选择正确选项!" -ForegroundColor Red
    }
}

# 展示最终网卡地址信息
Write-Host "`n---------------------------------------------"
Write-Host "当前网卡地址详情:" -ForegroundColor Cyan
Get-NetIPAddress -InterfaceIndex $ifIndex -AddressFamily IPv4
Get-NetIPAddress -InterfaceIndex $ifIndex -AddressFamily IPv6

Write-Host "`n按任意键退出..."
pause

AI改的,支持IPV6和恢复DHCP配置
2026-6-12 14:02
0
游客
登录 | 注册 方可回帖
返回