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

PowerShell IP验证GUI脚本修复与定制开发请求

符合要求的IP验证与批量写入PowerShell GUI脚本

以下是完全满足需求的PowerShell GUI脚本,解决了方法调用错误问题,同时匹配布局、验证规则和功能要求:

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

# 主窗口配置
$mainForm = New-Object System.Windows.Forms.Form
$mainForm.Text = "IP Blocking Automation"
$mainForm.Size = New-Object System.Drawing.Size(600, 450)
$mainForm.StartPosition = "CenterScreen"
$mainForm.BackColor = [System.Drawing.Color]::FromArgb(240,248,255)
$mainForm.FormBorderStyle = "FixedSingle"
$mainForm.MaximizeBox = $false

# 顶部标题面板
$topPanel = New-Object System.Windows.Forms.Panel
$topPanel.Size = New-Object System.Drawing.Size(580, 60)
$topPanel.Location = New-Object System.Drawing.Point(10,10)
$topPanel.BackColor = [System.Drawing.Color]::FromArgb(70,130,180)

# 左上角"xyz"标签
$xyzLabel = New-Object System.Windows.Forms.Label
$xyzLabel.Text = "xyz"
$xyzLabel.Font = New-Object System.Drawing.Font("Arial", 16, [System.Drawing.FontStyle]::Bold)
$xyzLabel.ForeColor = [System.Drawing.Color]::White
$xyzLabel.Location = New-Object System.Drawing.Point(15,15)
$topPanel.Controls.Add($xyzLabel)

# 右上角功能标题
$titleLabel = New-Object System.Windows.Forms.Label
$titleLabel.Text = "IP blocking automation"
$titleLabel.Font = New-Object System.Drawing.Font("Arial", 14, [System.Drawing.FontStyle]::Regular)
$titleLabel.ForeColor = [System.Drawing.Color]::White
$titleLabel.Location = New-Object System.Drawing.Point(350,20)
$topPanel.Controls.Add($titleLabel)

# Logo容器(替换为本地Logo路径)
$logoPictureBox = New-Object System.Windows.Forms.PictureBox
$logoPictureBox.Size = New-Object System.Drawing.Size(50,50)
$logoPictureBox.Location = New-Object System.Drawing.Point(280,5)
$logoPictureBox.SizeMode = "StretchImage"
# 替换为你的Logo文件路径
$logoPath = "C:\path\to\your\xyz-logo.png"
if (Test-Path $logoPath) {
    $logoPictureBox.Image = [System.Drawing.Image]::FromFile($logoPath)
} else {
    $logoPictureBox.Text = "Logo"
    $logoPictureBox.ForeColor = [System.Drawing.Color]::White
}
$topPanel.Controls.Add($logoPictureBox)
$mainForm.Controls.Add($topPanel)

# 错误提示标签
$errorLabel = New-Object System.Windows.Forms.Label
$errorLabel.Text = ""
$errorLabel.Font = New-Object System.Drawing.Font("Arial", 10)
$errorLabel.ForeColor = [System.Drawing.Color]::Red
$errorLabel.Location = New-Object System.Drawing.Point(10,80)
$errorLabel.AutoSize = $true
$mainForm.Controls.Add($errorLabel)

# IP多行输入框
$ipTextBox = New-Object System.Windows.Forms.TextBox
$ipTextBox.Multiline = $true
$ipTextBox.AcceptsReturn = $true
$ipTextBox.ScrollBars = "Vertical"
$ipTextBox.Size = New-Object System.Drawing.Size(580, 250)
$ipTextBox.Location = New-Object System.Drawing.Point(10,110)
$ipTextBox.Font = New-Object System.Drawing.Font("Consolas", 10)
$ipTextBox.BackColor = [System.Drawing.Color]::White
$mainForm.Controls.Add($ipTextBox)

# Add按钮
$addButton = New-Object System.Windows.Forms.Button
$addButton.Text = "Add"
$addButton.Size = New-Object System.Drawing.Size(100,35)
$addButton.Location = New-Object System.Drawing.Point(250,375)
$addButton.Font = New-Object System.Drawing.Font("Arial", 12, [System.Drawing.FontStyle]::Bold)
$addButton.BackColor = [System.Drawing.Color]::FromArgb(60,179,113)
$addButton.ForeColor = [System.Drawing.Color]::White
$addButton.FlatStyle = "Flat"

# IP验证与写入逻辑
$addButton.Add_Click({
    $errorLabel.Text = ""
    $inputIps = $ipTextBox.Text -split "`n" | ForEach-Object { $_.Trim() } | Where-Object { $_ -ne "" }
    $validIps = @()
    $blockedSubnets = @("111.112.44.0/24", "111.112.45.0/24")

    foreach ($ip in $inputIps) {
        # 验证IP格式合法性
        if (-not [System.Net.IPAddress]::TryParse($ip, [ref]$null)) {
            $errorLabel.Text += "无效IP格式: $ip`n"
            continue
        }

        $ipAddress = [System.Net.IPAddress]::Parse($ip)
        # 拒绝私有IP(含回环地址)
        if ($ipAddress.AddressFamily -eq [System.Net.Sockets.AddressFamily]::InterNetwork) {
            $bytes = $ipAddress.GetAddressBytes()
            $isPrivate = $false
            if ($bytes[0] -eq 10) { $isPrivate = $true }
            elseif ($bytes[0] -eq 192 -and $bytes[1] -eq 168) { $isPrivate = $true }
            elseif ($bytes[0] -eq 172 -and ($bytes[1] -ge 16 -and $bytes[1] -le 31)) { $isPrivate = $true }
            elseif ($bytes[0] -eq 127) { $isPrivate = $true }

            if ($isPrivate) {
                $errorLabel.Text += "私有IP不允许: $ip`n"
                continue
            }
        } else {
            $errorLabel.Text += "仅支持IPv4地址: $ip`n"
            continue
        }

        # 检查是否在禁止子网内
        $inBlockedSubnet = $false
        foreach ($subnet in $blockedSubnets) {
            $subnetParts = $subnet -split "/"
            $subnetIp = [System.Net.IPAddress]::Parse($subnetParts[0])
            $prefixLength = [int]$subnetParts[1]
            $mask = [System.Net.IPAddress]::Parse(([System.BitConverter]::ToString(([uint32]::MaxValue - ([uint32]::MaxValue -shr $prefixLength)) -shr (32 - $prefixLength)) -replace "-","."))
            
            $networkIp = [System.Net.IPAddress]::Parse((([uint32][System.Net.IPAddress]::Parse($ip)) -band ([uint32]$mask)) -as [string])
            if ($networkIp -eq $subnetIp) {
                $inBlockedSubnet = $true
                break
            }
        }

        if ($inBlockedSubnet) {
            $errorLabel.Text += "禁止的子网IP: $ip`n"
            continue
        }

        $validIps += $ip
    }

    # 写入合法IP到文件
    if ($validIps.Count -gt 0) {
        $outputPath = "ValidIPs.txt"
        $validIps | Out-File -FilePath $outputPath -Append -Encoding utf8
        [System.Windows.Forms.MessageBox]::Show("成功添加 $($validIps.Count) 个合法IP到文件!", "操作成功", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Information)
        $ipTextBox.Text = ""
    } elseif ($errorLabel.Text -eq "") {
        $errorLabel.Text = "请输入至少一个IP地址"
    }
})

$mainForm.Controls.Add($addButton)

# 启动窗口
$mainForm.ShowDialog() | Out-Null

关键说明

  • 布局匹配:
    • 顶部蓝色面板承载双标题与Logo区域,Logo需替换为本地文件路径
    • 输入框上方设红色错误提示标签,实时反馈验证问题
    • 底部居中放置绿色醒目Add按钮
  • 验证规则:
    • 自动过滤空行与首尾空格
    • 拒绝格式无效IP、私有IP(含回环)、指定禁止子网内的IP
    • 仅支持IPv4公网IP
  • 功能实现:
    • 点击Add按钮后批量验证IP,合法IP追加写入当前目录下的ValidIPs.txt
    • 操作完成弹出提示并清空输入框,错误信息集中展示

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

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.07.17 04:14:51