静态IP配置工具开发报错求助——受训初级测试员技术咨询
解决PowerShell静态IP配置工具的Error 87报错问题
嘿,作为初级测试员能自己动手写配置工具已经超棒啦!你遇到的Windows System Error 87本质是参数无效,我帮你梳理下问题和修复方案:
核心报错原因
你代码里获取用户输入的部分犯了个小疏忽:
$IP = $IPtextBox $Gateway = $GWtextbox $Dns = $DNStextbox
这里你直接把TextBox控件对象赋值给变量了,但New-NetIPAddress和Set-DnsClientServerAddress需要的是字符串类型的IP地址,不是控件本身!这就导致命令收到了无效参数,触发Error 87。
其他潜在问题
Get-NetAdapter | ? {$_.Status -eq "up"}可能返回多个网络适配器(比如有线+无线),直接操作会导致混乱- 没有输入验证,用户输入无效IP时会直接报错
- 缺少管理员权限检查,修改IP需要管理员身份运行PowerShell
修正后的完整代码
Add-Type -AssemblyName System.Windows.Forms Add-Type -AssemblyName System.Drawing $form = New-Object System.Windows.Forms.Form $form.Text = 'Static IP Tool' $form.Size = New-Object System.Drawing.Size(300,320) $form.StartPosition = 'CenterScreen' # 检查是否以管理员身份运行 $currentPrincipal = New-Object Security.Principal.WindowsPrincipal([Security.Principal.WindowsIdentity]::GetCurrent()) if (-not $currentPrincipal.IsInRole([Security.Principal.WindowsBuiltInRole]::Administrator)) { [System.Windows.Forms.MessageBox]::Show("请以管理员身份运行此脚本!", "权限不足", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error) exit } # 适配器选择下拉框 $adapterLabel = New-Object System.Windows.Forms.Label $adapterLabel.Location = New-Object System.Drawing.Point(10,170) $adapterLabel.Size = New-Object System.Drawing.Size(280,20) $adapterLabel.Text = '选择网络适配器:' $form.Controls.Add($adapterLabel) $adapterComboBox = New-Object System.Windows.Forms.ComboBox $adapterComboBox.Location = New-Object System.Drawing.Point(10,190) $adapterComboBox.Size = New-Object System.Drawing.Size(260,20) # 获取所有启用的适配器 Get-NetAdapter | Where-Object {$_.Status -eq "Up"} | ForEach-Object { $adapterComboBox.Items.Add($_.Name) } if ($adapterComboBox.Items.Count -gt 0) { $adapterComboBox.SelectedIndex = 0 } $form.Controls.Add($adapterComboBox) # IP输入区域 $IPlabel = New-Object System.Windows.Forms.Label $IPlabel.Location = New-Object System.Drawing.Point(10,20) $IPlabel.Size = New-Object System.Drawing.Size(280,20) $IPlabel.Text = 'IP地址:' $form.Controls.Add($IPlabel) $IPtextBox = New-Object System.Windows.Forms.TextBox $IPtextBox.Location = New-Object System.Drawing.Point(10,40) $IPtextBox.Size = New-Object System.Drawing.Size(260,20) $form.Controls.Add($IPtextBox) # 子网掩码前缀输入 $maskLabel = New-Object System.Windows.Forms.Label $maskLabel.Location = New-Object System.Drawing.Point(10,70) $maskLabel.Size = New-Object System.Drawing.Size(280,20) $maskLabel.Text = '子网掩码前缀(如24=255.255.255.0):' $form.Controls.Add($maskLabel) $maskTextBox = New-Object System.Windows.Forms.TextBox $maskTextBox.Location = New-Object System.Drawing.Point(10,90) $maskTextBox.Size = New-Object System.Drawing.Size(260,20) $maskTextBox.Text = "24" $form.Controls.Add($maskTextBox) # 网关输入区域 $GWlabel = New-Object System.Windows.Forms.Label $GWlabel.Location = New-Object System.Drawing.Point(10,120) $GWlabel.Size = New-Object System.Drawing.Size(280,20) $GWlabel.Text = '网关:' $form.Controls.Add($GWlabel) $GWtextbox = New-Object System.Windows.Forms.TextBox $GWtextbox.Location = New-Object System.Drawing.Point(10,140) $GWtextbox.Size = New-Object System.Drawing.Size(260,20) $form.Controls.Add($GWtextbox) # DNS输入区域 $DNSlabel = New-Object System.Windows.Forms.Label $DNSlabel.Location = New-Object System.Drawing.Point(10,220) $DNSlabel.Size = New-Object System.Drawing.Size(280,20) $DNSlabel.Text = 'DNS(多个用逗号分隔):' $form.Controls.Add($DNSlabel) $DNStextbox = New-Object System.Windows.Forms.TextBox $DNStextbox.Location = New-Object System.Drawing.Point(10,240) $DNStextbox.Size = New-Object System.Drawing.Size(260,20) $form.Controls.Add($DNStextbox) # 按钮区域 $okButton = New-Object System.Windows.Forms.Button $okButton.Location = New-Object System.Drawing.Point(55,270) $okButton.Size = New-Object System.Drawing.Size(90,25) $okButton.Text = '确定' $okButton.DialogResult = [System.Windows.Forms.DialogResult]::OK $form.AcceptButton = $okButton $form.Controls.Add($okButton) $cancelButton = New-Object System.Windows.Forms.Button $cancelButton.Location = New-Object System.Drawing.Point(160,270) $cancelButton.Size = New-Object System.Drawing.Size(90,25) $cancelButton.Text = '取消' $cancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel $form.CancelButton = $cancelButton $form.Controls.Add($cancelButton) $form.Topmost = $true $result = $form.ShowDialog() if ($result -eq [System.Windows.Forms.DialogResult]::OK) { # 修正:获取TextBox的Text属性 $IP = $IPtextBox.Text.Trim() $MaskBits = [int]$maskTextBox.Text.Trim() $Gateway = $GWtextbox.Text.Trim() $Dns = $DNStextbox.Text.Trim() -split ',' | ForEach-Object { $_.Trim() } $IPType='IPv4' $adapterName = $adapterComboBox.SelectedItem # 输入验证 $ipRegex = [regex]'^(?:(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)\.){3}(?:25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)$' if (-not $ipRegex.IsMatch($IP)) { [System.Windows.Forms.MessageBox]::Show("请输入有效的IP地址!", "输入错误", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error) exit } if ($Gateway -and -not $ipRegex.IsMatch($Gateway)) { [System.Windows.Forms.MessageBox]::Show("请输入有效的网关地址!", "输入错误", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error) exit } foreach ($dnsServer in $Dns) { if ($dnsServer -and -not $ipRegex.IsMatch($dnsServer)) { [System.Windows.Forms.MessageBox]::Show("请输入有效的DNS地址!", "输入错误", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error) exit } } try { # 获取指定适配器 $adapter = Get-NetAdapter -Name $adapterName # 移除现有IP和网关 If (($adapter | Get-NetIPConfiguration).IPv4Address.IPAddress) { $adapter | Remove-NetIPAddress -AddressFamily $IPType -Confirm:$false -ErrorAction Stop } If (($adapter | Get-NetIPConfiguration).Ipv4DefaultGateway) { $adapter | Remove-NetRoute -AddressFamily $IPType -Confirm:$false -ErrorAction Stop } # 配置新IP和网关 $adapter | New-NetIPAddress ` -AddressFamily $IPType ` -IPAddress $IP ` -PrefixLength $MaskBits ` -DefaultGateway $Gateway ` -ErrorAction Stop # 配置DNS if ($Dns) { $adapter | Set-DnsClientServerAddress -ServerAddresses $Dns -ErrorAction Stop } [System.Windows.Forms.MessageBox]::Show("静态IP配置成功!", "操作完成", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Information) } catch { [System.Windows.Forms.MessageBox]::Show("配置失败:$_", "错误", [System.Windows.Forms.MessageBoxButtons]::OK, [System.Windows.Forms.MessageBoxIcon]::Error) } }
关键修改点说明
- 修复参数传递:把
$IP = $IPtextBox改成$IP = $IPtextBox.Text,确保传递的是用户输入的字符串 - 增加权限检查:启动时验证是否为管理员,避免权限不足报错
- 适配器选择:让用户手动选择要配置的网络适配器,避免多适配器冲突
- 输入验证:用正则表达式检查IP、网关、DNS的格式有效性
- 错误处理:添加
try-catch块捕获异常并友好提示用户 - 灵活子网掩码:让用户可以自定义子网掩码前缀,不再固定为24
使用注意事项
- 必须以管理员身份运行PowerShell脚本
- 输入DNS时如果有多个,用逗号分隔(比如
8.8.8.8,1.1.1.1) - 配置前确保网络适配器处于启用状态
内容的提问来源于stack exchange,提问作者Tuhø
相关产品推荐
相关产品推荐

