首页 > 系统相关 >powershell 创建winform窗体 T2 : 自动配置相机网卡参数

powershell 创建winform窗体 T2 : 自动配置相机网卡参数

时间:2024-08-29 14:28:30浏览次数:11  
标签:Forms form Object T2 System 网卡 窗体 adaptname New

<# 脚本功能说明:选择要配置的相机网卡,点击配置按键,自动配置参数
# 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

相关文章

  • 0828-T2 超级幸运数
    0828-T2超级幸运数题意给出数字\(A\),\(B\)。求出以\(A\),\(B\)为两端的数的最小值。思路分\(AB\)和\(BA\)两种情况。当\(x\)和\(y\)拼接时,\(x\)的尾部和\(y\)的头部可以合并。如\(132\)和\(231\)合并出来为\(13231\)。求出\(x\)和\(y\)的最长公共前......
  • Q: USB无线网卡搜不到路由器WiFi,但也能搜索到少部分信号。
    原因分析:一般在路由器的配置的无线信道是自动,路由器的2.4G频段有13个左右交叠的信道。由于USB无线网卡的设置信道区间可能不在无线信道范围内,导致无线网卡搜索不到对应wifi。解决问题:鼠标右键点击任务栏上的网络图标,点击“打开网络和Internet设置”,在弹出的设置窗口左边选择......
  • 【Shell脚本】检测网卡流量,并按规定格式记录在日志中
    #!/bin/bash########################################################检测网卡流量,并按规定格式记录在日志中#规定一分钟记录一次#日志格式如下所示:#2019-08-1220:40#ens33input:1234bps#ens33output:1235bps######################################......
  • 【C#】【WinForm】 按功能列出常见窗体控件
    Windows窗体提供执行多个功能的控件和组件。下表按常规功能列出了Windows窗体控件和组件。此外,如果存在多个提供相同功能的控件,则会列出推荐的控件,并附有关于它所取代的控件的说明。在单独的后续表中,列出了被取代的控件及其建议的替换项。按功能列出的推荐控件和组件......
  • T240827【定理3.3 Cauchy积分定理的 Goursat 证明】
    [T240819]Cauchy积分定理:设\(f(z)\)在\(z\)平面上的单连通区域\(D\)内解析,\(C\)为\(D\)内的任一条周线,则\[\int_Cf(z)~\mathrmdz=0\]证:【Goursat证明】Step1:若\(C\)为\(D\)内任一三角形\(\Delta\).假设\(|\int_{\Delta}f(z)~\mathrmdz|=M\),下证......
  • powershell 创建winform窗体 T1
    $app=[System.Windows.Forms.Application]$myForm=new-objectSystem.Windows.Forms.Form$myForm.Text="mywindow"$button1=new-objectSystem.Windows.Forms.Button$button1.Size=new-objectSystem.Drawing.Size-argumentlist75,23$button1.Text=&......
  • 【python3.8安装报错】lmportError: DLL load failed while importing ft2font: 找不
    客户需求背景:安装python3.8无法运行,报错lmportError:DLLloadfailedwhileimportingft2font:找不到指定的模块 考虑兼容性问题这个问题首先需要考虑的是是否是python版本太高了,因为python3.9不支持win7,而python3.8版本对win7的兼容性也有一定的限制。解决:首先考......
  • 桌面窗体工具类库 - C#小函数类推荐
          此文记录的是检测桌面窗体的小函数。/***桌面窗体工具类库AustinLiu刘恒辉ProjectManagerandSoftwareDesignerE-Mail:[email protected]:http://lzhdim.cnblogs.comDate:2024-01-1515:18:00说明:用......
  • CTT2021
    D1T1末日魔法少女计划知识点:DP求构造,\(B\)叉树。感觉最近见到好多用DP来求最优构造的题目。可以将\(A_{i,j}=1\)看作拥有区间信息\([i,j)\),要求构造最少的区间信息,使得任何区间\([l,r)\)都可以被最多\(k\)个已知区间的加和表示。考虑\(k=2\)的时候,就是要构建猫......
  • Lanelet2与OpenDrive和OpenStreetMap的关系
    Lanelet2、OpenDrive和OpenStreetMap在自动驾驶和智能交通系统中都扮演着重要角色,但它们之间在功能和用途上存在一些差异。以下是它们之间关系的详细阐述:Lanelet2定义与功能:Lanelet2是一个专为自动驾驶和智能交通系统设计的高精度道路网络表示框架。它提供了丰富的数据结......