$app=[System.Windows.Forms.Application]
$myForm=new-object System.Windows.Forms.Form
$myForm.Text="my window"
$button1 = new-object System.Windows.Forms.Button
$button1.Size = new-object System.Drawing.Size -argumentlist 75, 23
$button1.Text = "点我试试"
$textbox1=new-object System.Windows.Forms.TextBox
$textBox1.Multiline = $true;
$textBox1.Text = "hello world"
$textBox1.Size = new-object System.Drawing.Size -argumentlist 281, 227
$flowLayoutPanel1 = new-object System.Windows.Forms.FlowLayoutPanel
$myForm.Controls.Add($flowLayoutPanel1)
$flowLayoutPanel1.Controls.Add($textBox1);
$flowLayoutPanel1.Controls.Add($button1);
$flowLayoutPanel1.Dock = "Fill"
$flowLayoutPanel1.FlowDirection = "TopDown"
$button1ClickEventHandler = [System.EventHandler] {
[System.Windows.Forms.MessageBox]::Show("Hello world!")
}
$button1.Add_Click($button1ClickEventHandler)
$app::EnableVisualStyles()
$app::Run($myForm)
# listbox项选择demo
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
Add-Type -AssemblyName PresentationFramework
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Data Entry Form'
$form.Size = New-Object System.Drawing.Size(700,200)
$form.StartPosition = 'CenterScreen'
$OKButton = New-Object System.Windows.Forms.Button
$OKButton.Location = New-Object System.Drawing.Point(75,120)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = 'OK'
$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(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = 'Cancel'
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$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(280,20)
$label.Text = 'Please make a selection from the list below:'
$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,20)
# 需要更新参数的列表
$listBoxNew = New-Object System.Windows.Forms.Listbox
$listBoxNew.Location = New-Object System.Drawing.Point(360,40)
$listBoxNew.Size = New-Object System.Drawing.Size(260,20)
# 创建按钮
$button = New-Object System.Windows.Forms.Button
$button.Location = New-Object System.Drawing.Point(280,80)
$button.Text = "执行操作"
$button.Size = New-Object System.Drawing.Size(75,23)
# 定义按钮的Click事件处理程序
$button.Add_Click({
# 获取选中的项
$selectedItem = $listBox.SelectedItem
})
$listBox.SelectionMode = 'MultiExtended'
#[void] $listBox.Items.Add('Item 1')
#[void] $listBox.Items.Add('Item 2')
#[void] $listBox.Items.Add('Item 3')
#[void] $listBox.Items.Add('Item 4')
#[void] $listBox.Items.Add('Item 5')
$adpaters=$(Get-NetAdapter | Select-Object -ExpandProperty Name)
foreach ($ad in $adpaters)
{
[void] $listBox.Items.Add($ad)
}
$listBox.Height = 70
$form.Controls.Add($listBox)
$form.Topmost = $true
$form.Controls.Add($listBoxNew)
$result = $form.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$x = $listBox.SelectedItems
$x
foreach ($ad in $x)
{
[void] $listBoxNew.Items.Add($ad)
}
}
# 字符串匹配
Get-NetAdapter|Where-Object {$_.InterfaceDescription -like 'Intel*'}
输出:
Name InterfaceDescription ifIndex Status MacAddress LinkSpeed
---- -------------------- ------- ------ ---------- ---------
有线 Intel(R) Ethernet Connection (11) I2... 5 Up B0-7B-25-07-1B-BB 1 Gbps
选择网卡自动设置参数
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"
}
}
# 取消勾选允许计算机关闭此设备以节省电
Function disablePowerMang($adaptname)
{
# $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()
}
}
}
$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(75,250)
$OKButton.Size = New-Object System.Drawing.Size(75,23)
$OKButton.Text = 'OK'
$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(150,250)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = 'Cancel'
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$form.CancelButton = $CancelButton
$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(280,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)
$textbox1=new-object System.Windows.Forms.TextBox
$textBox1.Multiline = $true;
$textBox1.Text = ""
$textBox1.Location = New-Object System.Drawing.Point(360,45)
$textBox1.Size = new-object System.Drawing.Size(300, 227)
# 创建按钮
$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 = "-->"
$form.Controls.Add($button)
# 定义按钮的Click事件处理程序
$button.Add_Click({
#$form.Controls.Remove($listBoxNew)
# 需要更新参数的列表
# $listBoxNew = New-Object System.Windows.Forms.Listbox
# $listBoxNew.Location = New-Object System.Drawing.Point(360,40)
# $listBoxNew.Size = New-Object System.Drawing.Size(260,20)
####获取选中的项
# $selectedItem = $listBox.SelectedItem
# $selectedItem
# foreach ($ad in $selectedItem)
# {
# [void] $listBoxNew.Items.Add($ad)
# }
# $listBoxNew.Height = 70
# $form.Controls.Add($listBoxNew)
$textBox1.Text=""
$allText = ""
####获取选中的项
$selectedItem = $listBox.SelectedItem
$allText = $(-Join $selectedItem)
foreach ($ad in $selectedItem)
{
# [void] $listBoxNew.Items.Add($ad)
Write-Output "start update net param :"$ad
[System.Windows.Forms.MessageBox]::Show("开始配置网卡【"+$ad + "】,请等待,配置完成将弹窗")
setNetParam($ad)
disablePowerMang($ad)
[System.Windows.Forms.MessageBox]::Show("【"+$ad + "】 ---->设置 成功!")
}
$textBox1.Text = $allText
})
$listBox.SelectionMode = 'MultiExtended'
$adpaters=$(Get-NetAdapter | Select-Object -ExpandProperty Name)
foreach ($ad in $adpaters)
{
[void] $listBox.Items.Add($ad)
}
#$listBox.Height = 70
$form.Controls.Add($listBox)
#$form.Controls.Add($listBoxNew)
$form.Controls.Add($textBox1)
$form.Topmost = $true
$form.Add_Shown({$form.Activate()})
$result = $form.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$x = $listBox.SelectedItems
$x
}
标签:Object,System,T1,Forms,Add,窗体,New,powershell,Size From: https://www.cnblogs.com/hakula/p/18381081