<# 脚本功能说明:选择要配置的相机网卡,点击配置按键,自动配置参数
# 2024.08.29
#>
# 获取管理员权限
if (-NOT ([Security.Principal.WindowsPrincipal][Security.Principal.WindowsIdentity]::GetCurrent()).IsInRole([Security.Principal.WindowsBuiltInRole] "Administrator"))
{
# 如果没有,提升权限
$arguments = "& '" + $script:MyInvocation.MyCommand.Path + "'"
Start-Process powershell -Verb runas -ArgumentList $arguments
# 退出当前脚本
break
}
set-ExecutionPolicy RemoteSigned
[Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
$app=[System.Windows.Forms.Application]
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName PresentationFramework
[System.Windows.Forms.Application]::EnableVisualStyles()
# 配置网卡参数
Function setNetParam ($adaptname)
{
try{
Set-NetAdapterAdvancedProperty -Name $adaptname -RegistryKeyword "*SpeedDuplex" -RegistryValue 6
Set-NetAdapterAdvancedProperty -Name $adaptname -RegistryKeyword "*ReceiveBuffers" -RegistryValue 2048
Set-NetAdapterAdvancedProperty -Name $adaptname -RegistryKeyword "*TransmitBuffers" -RegistryValue 2048
Set-NetAdapterAdvancedProperty -Name $adaptname -RegistryKeyword "*JumboPacket" -RegistryValue 9014
Set-NetAdapterAdvancedProperty -Name $adaptname -RegistryKeyword "ITR" -RegistryValue 3600
#Set-NetAdapterAdvancedProperty -Name $adaptname -RegistryKeyword "EEELinkAdvertisement" -RegistryValue 0
}
catch{
#Throw "Error:" ,$adaptname
"an error occured"
}
}
# 获取对应网卡的ipv4
Function getipv4($adaptname)
{
Write-Information "param",$adaptname
$ipv4 = Get-NetAdapter -name $adaptname | Get-NetIPAddress|where-object {$_.IPAddress -like "192.168.*"}|Where-Object {$_.AddressFamily -eq "IPv4"} | Select-Object -ExpandProperty IPAddress
return $ipv4
}
# 取消勾选允许计算机关闭此设备以节省电
Function disablePowerMang($adaptname)
{
try{
# $NICs = Get-NetAdapter | Where-Object {$_.status -eq 'Up'}
$NICs = Get-NetAdapter | Where-Object {$_.Name -eq $adaptname}
Foreach ($NIC in $NICs)
{
$powerMgmt = Get-WmiObject MSPower_DeviceEnable -Namespace root\wmi | where {$_.InstanceName -match [regex]::Escape($nic.PNPDeviceID)}
If ($powerMgmt.Enable -eq $True)
{
$powerMgmt.Enable = $False
$powerMgmt.psbase.Put()
}
}
}catch
{
Write-Host $adaptname," power configure exception"
}
Write-Host $adaptname," 电源管理配置完成"
}
$form = New-Object System.Windows.Forms.Form
$form.Text = '相机网卡参数自动配置'
$form.Size = New-Object System.Drawing.Size(700,360)
$form.StartPosition = 'CenterScreen'
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(175,250)
$OKButton.Size = New-Object System.Drawing.Size(75,33)
$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(250,250)
$CancelButton.Size = New-Object System.Drawing.Size(75,33)
$CancelButton.Text = '退出'
#$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
#$form.CancelButton = $CancelButton
$CancelButton.Add_Click({
$form.Close()
})
$form.Controls.Add($CancelButton)
$label = New-Object System.Windows.Forms.Label
$label.Location = New-Object System.Drawing.Point(10,20)
$label.Size = New-Object System.Drawing.Size(680,20)
$label.Text = '使用说明: [-->]:从左侧选择要配置的相机网卡(可多选); [<--]移除右侧选中的网卡。点击[配置] '
$form.Controls.Add($label)
$listBox = New-Object System.Windows.Forms.Listbox
$listBox.Location = New-Object System.Drawing.Point(10,40)
$listBox.Size = New-Object System.Drawing.Size(260,200)
$listBox.SelectionMode = 'MultiExtended'
$listBoxOK = New-Object System.Windows.Forms.Listbox
$listBoxOK.Location = New-Object System.Drawing.Point(360,40)
$listBoxOK.Size = New-Object System.Drawing.Size(300,120)
$listBoxOK.SelectionMode = 'MultiExtended'
$textbox1=new-object System.Windows.Forms.TextBox
$textBox1.Multiline = $true;
$textBox1.Text = ""
$textBox1.Location = New-Object System.Drawing.Point(360,170)
$textBox1.Size = new-object System.Drawing.Size(300, 100)
# 创建按钮
$button = New-Object System.Windows.Forms.Button
$button.Location = New-Object System.Drawing.Point(280,45)
$button.Size = New-Object System.Drawing.Size(75,23)
$button.Text = "->"
$buttonDel = New-Object System.Windows.Forms.Button
$buttonDel.Location = New-Object System.Drawing.Point(280,95)
$buttonDel.Size = New-Object System.Drawing.Size(75,23)
$buttonDel.Text = "<-"
$button1ClickEventHandler = [System.EventHandler] {
#[System.Windows.Forms.MessageBox]::Show("Hello world!")
foreach ($ad in $listBoxOK.SelectedIndices)
{
Write-Host "remove ",$ad
[void] $listBoxOK.Items.RemoveAt($ad)
}
}
$buttonDel.Add_Click($button1ClickEventHandler)
$ckbox = New-Object System.Windows.Forms.CheckBox
$ckbox.Location = New-Object System.Drawing.Point(280,125)
$ckbox.Size = New-Object System.Drawing.Size(75,43)
$ckbox.Text = "完成是否弹窗"
$ckbox.Checked = $false
$form.Controls.Add($button)
$form.Controls.Add($buttonDel)
$form.controls.Add($ckbox)
# 定义按钮的Click事件处理程序
$button.Add_Click({
####获取选中的项
$selectedItems = $listBox.SelectedItems
foreach ($ad in $selectedItems)
{
if($ad -like "ACS")
{
continue
}
## 右侧已经有的不再添加
$flagExist = $false
foreach($rightitem in $listBoxOK.Items)
{
if($ad -eq $rightitem)
{
$flagExist = $true
break
}
}
if($flagExist)
{
continue
}
[void] $listBoxOK.Items.Add($ad)
#$ipv4 = getipv4($ad)
#Write-Output $ad,"--->",$ipv4
}
})
$OKButton.Add_Click({
$textBox1.Clear()
if($listBoxOK.items.Count -gt 0)
{
$msResult = [System.Windows.Forms.MessageBox]::Show("开始配置网卡,请等待","Confirm","YesNo","Question")
switch ($msResult)
{
'yes'{}
'No' { return}
}
$textBox1.Text = "start configure..... `r`n"
foreach($ad in $listBoxOK.Items){
Write-Output "start update net param :"$ad
Write-Host $ad
#
setNetParam($ad)
disablePowerMang($ad)
$textBox1.Text += "【"+$ad + "】 ---->设置 成功! `r`n"
if($ckbox.Checked)
{
[System.Windows.Forms.MessageBox]::Show("【"+$ad + "】 ---->设置 成功!")
}
}
}
})
## 列出所有网卡名称
$adpaters=$(Get-NetAdapter | Select-Object -ExpandProperty Name)
# 将网卡加入列表
foreach ($ad in $adpaters)
{
if($ad -notlike "ACS")
{
[void] $listBox.Items.Add($ad)
}
}
$form.Controls.Add($listBox)
$form.Controls.Add($listBoxOK)
$form.Controls.Add($textBox1)
$form.Topmost = $true
$app::Run($form)
标签:Forms,form,Object,T2,System,网卡,窗体,adaptname,New From: https://www.cnblogs.com/hakula/p/18386589