powershell - powershell中的列表框-集合还是多维数组?
问题描述
我正在使用以下代码创建一个列表框,并使用它根据列表框中的显示名称重新启动计算机。我实际上想用实际的计算机名称替换显示名称。我可以为此使用数组吗?我仍然习惯于powershell。如果我不能做一个数组,列表框中是否有可以存储计算机名称的属性或集合?
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Select a Computer'
$form.Size = New-Object System.Drawing.Size(300,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 select a computer:'
$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)
$listBox.Height = 80
[void] $listBox.Items.Add('Screen in building 1 first floor')
[void] $listBox.Items.Add('Screen in building 1 second floor')
[void] $listBox.Items.Add('Screen in building 2 4th floor')
[void] $listBox.Items.Add('Screen in building 3 basement')
$form.Controls.Add($listBox)
$form.Topmost = $true
$result = $form.ShowDialog()
if ($result -eq [System.Windows.Forms.DialogResult]::OK)
{
$c = Get-Credential Domain\Username
Restart-Computer -ComputerName "Variable 2 of selection from listbox" -Credential $c -Force
$x = $listBox.SelectedItem
$x
}
解决方案
更新:在下面的示例中,我将使用 Active Directory 中的计算机Import-Module ActiveDirectory
为了listbox
使用多维数组向您添加项目,我们可以使用 anObject
和 theNoteProperty
添加到listbox
前任:
#This example I am grabbing all computers from AD with the OS of windows server -- all windows servers
Import-Module ActiveDirectory
#Using the Name property for reboot and the Description for the listbox... you can use a different attribute if you like
$Computers = Get-ADComputer -filter {OperatingSystem -like "Windows Server*" -and Enabled -eq $true} -Properties Name, Description, OperatingSystem | Select Name, Description, OperatingSystem
$listboxCollection =@()
foreach($Computer in $Computers)
{
$Object = New-Object Object
$Object | Add-Member -type NoteProperty -Name CompName -Value $Computer.Name
$Object | Add-Member -type NoteProperty -Name Values -Value $Computer.Description
#fill the $listboxCollection
$listboxCollection += $Object
}
#Add collection to the $listbox
$listBox.Items.AddRange($listboxCollection)
如果您要从这里显示您的表单,您的所有条目都将显示为System.Object
。要从 to 中的Decription
每个集合中获取$listboxCollection
,我们已经告诉它ValueMember
and DisplayMember
。
前任:
#This is using the properties above to display the correct item
$listBox.ValueMember = "CompName"
$listBox.DisplayMember = "Values"
与您的原始帖子保持一致,要获得“确定”按钮上的选定项目listbox
,您需要输入一个条件。但是,有几种不同的方法可以处理此事件。
前任:
#show form as dialog
$result = $form.ShowDialog()
if($result = [System.Windows.Forms.DialogResult]::OK)
{
$selectedComputer = $listBox.SelectedItem.CompName
Restart-Computer -ComputerName $selectedComputer -Credential Get-Credential Domain\Username -Force
}
变量$selectedComputer
将是选定的计算机。注意:在这个例子中,没有任何检查来确保有一个有效的选择。
完整的解决方案:
Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing
#This example I am grabbing all computers from AD with the OS of windows server -- all windows servers
Import-Module ActiveDirectory
#Using the Name property for reboot and the Description for the listbox... you can use a different attribute if you like
$Computers = Get-ADComputer -filter {OperatingSystem -like "Windows Server*" -and Enabled -eq $true} -Properties Name, Description, OperatingSystem | Select Name, Description, OperatingSystem
$form = New-Object System.Windows.Forms.Form
$form.Text = 'Select a Computer'
$form.Size = New-Object System.Drawing.Size(300,200)
$form.StartPosition = 'CenterScreen'
$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)
$listBox.Height = 80
#create an empty collection to use later
$listboxCollection =@()
foreach($Computer in $Computers)
{
$Object = New-Object Object
$Object | Add-Member -type NoteProperty -Name CompName -Value $Computer.Name
$Object | Add-Member -type NoteProperty -Name Values -Value $Computer.Description
$listboxCollection += $Object
}
$listBox.Items.AddRange($listboxCollection)
#This is using the properties above to display the correct item
$listBox.ValueMember = "CompName"
$listBox.DisplayMember = "Values"
#add listbox to form
$form.Controls.Add($listBox)
#Ok Button
$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)
#keep on top
$form.Topmost = $true
#show form as dialog
$result = $form.ShowDialog()
if($result = [System.Windows.Forms.DialogResult]::OK)
{
#this tells it to get the Name of the property and not just the Item
$selectedComputer = $listBox.SelectedItem.CompName
Restart-Computer -ComputerName $selectedComputer -Credential Get-Credential Domain\Username -Force
}
注意:如果您使用 Active Directory 查询,您的计算机必须都具有Name
andDescription
属性(当然,除非您计划处理空/空值)
推荐阅读
- python - 未使用功能。为什么?
- sql - SQL 从结果中删除重复项
- loops - 如何找到依赖于外循环“i”的内循环的时间复杂度:
- python - 包含可变长度和逗号分隔的值字符串的 pandas 列如何堆叠成单独的值?
- php - 使用 XPath 从 XML 文档中提取特定于标签的数据
- c++ - 使用类在 C++ 中存储和列出变量/方法是一种好习惯吗
- python - AysncResult 和 python 池中带有 error_callback 的回调之间的区别
- sql-server - SQL Server 2016 中内存优化表分区的替代方法
- ajax - 从Vue中的数组中删除嵌套对象
- java - 使用构造函数创建唯一 ID