如何获得复选框的活动状态,例如checkboxesVM。在powershell检查吗?

时间:2022-02-28 06:14:05

I have the code below working perfectly to display the listed folders and the specific VM's in each folder. The problem comes when I try to find out the checkedstate for each of the checkboxes - the active UI gives me a good checkmark, but nowhere can I find a value/variable I can reference to do two things initially - one, to mark/unmark all the VM's under each Folder if the Folder is checked/unchecked and two, to execute the desired actions - which in this case will include poweroff/poweron support.

下面的代码可以完美地显示列出的文件夹和每个文件夹中的特定VM。问题是当我试着找出checkedstate每个UI复选框——活跃的给了我一个很好的标记,但我能找到一个值/变量可以引用最初做两件事——一个,马克/取消标记所有VM的每个文件夹下如果选中或取消选中的文件夹和两个,执行所需的行为——在这种情况下将包括定时关机/集成软件包支持。

Sorry about the size - if you want just the core of the checkbox array - it is in the checkbox region. The button1 gives me the ability to see which of the checkboxes has been checked because I am looping through the checkboxes but that does not give me active control over the checkboxes. The script works great in layout but I cannot seem to find the variable references to obtain the state of the checkboxes or checkboxesVM can anyone tell me how to do this inside the UI so they report their active state in the listbox? Once I have that I think I can get it working for the stuff I need.

不好意思,它的大小-如果你只想要复选框数组的核心-它在复选框区域。button1使我能够看到选中了哪些复选框,因为我正在对复选框进行循环,但这并不能给我对复选框的主动控制。这个脚本在布局上做得很好,但是我似乎找不到用于获取复选框或checkboxesVM状态的变量引用,有人能告诉我如何在UI中执行吗?一旦我有了它,我想我就能让它为我需要的东西工作。

$ServerList = Get-VM | Sort-Object -Property Folder,Name | `
?{$_.PowerState -eq "PoweredOn"}
$ServerCount= $ServerList.Count
$VMFolders = $ServerList | Group-Object folder

#----------------------------------------------
Function GenerateForm {
$handler_button1_Click= {
#Click Event Monitor
$listBox1.Items.Clear()
foreach($x in $checkboxes) {
    $names = $x.name
    $chkStat = $x.Checkstate
    $status = $names+ " " +$chkStat
    $listBox1.Items.Add("$status")
    }
}
#end Click Event

$form1 = New-Object System.Windows.Forms.Form
$button1 = New-Object System.Windows.Forms.Button
$checkBoxes = New-Object System.Windows.Forms.CheckBox
$InitialFormWindowState = New-Object System.Windows.Forms.FormWindowState

#region Generated Form Code
$Form1.Text = "VMWare VM Server Power On/Off"
$form1.DataBindings.DefaultDataSourceUpdateMode = 0
$Icon = [system.drawing.icon]::ExtractAssociatedIcon($PSHOME + "\powershell.exe")
$form1.icon=$icon
if($ServerCount -ge 6){
    $Form1ShapeW = (1000)
    if($ServerCount -ge 20){
        $Form1ShapeH = ($ServerCount * 35)/5
        } else {
        $form1ShapeH = (120)
    }
} else {
    $Form1ShapeW = 100 + (155 * $ServerCount)
}
$System_Drawing_Size = New-Object System.Drawing.Size($Form1ShapeW,$Form1ShapeH)
$form1.ClientSize = $System_Drawing_Size
$form1.autosize = $true
#endregion

$x=5
$y=30
$i=0
$FX=100
$FY=$y

#region CheckBoxes
    $Checkboxes = @()
    $VMfolders | %{
    $Checkboxes += New-Object System.Windows.Forms.CheckBox
    $Checkboxes[-1].useVisualStyleBackColor = $true
    $System_Drawing_Size = New-Object System.Drawing.Size(85,25)
    $Checkboxes[-1].Size = $System_Drawing_Size
    $Checkboxes[-1].text = $_.name
    if($x -ge 200 ) {
        $X = 5
        $Y = $y + 27
        $FY = $y
    }
    $System_Drawing_Point = New-Object System.Drawing.Point($x,$y)
    $Checkboxes[-1].Location = $System_Drawing_Point
    $Checkboxes[-1].DataBindings.DefaultDataSourceUpdateMode = 0
    $Checkboxes[-1].name = "folder"
    $y = $y + 27

#region CheckBoxesVM
    $VMList = $_.Group
    $CheckboxesVM = @()
    $VMList | %{
    $CheckboxesVM += New-Object System.Windows.Forms.CheckBox
        $System_Drawing_Size = New-Object System.Drawing.Size(150,25)
        $CheckboxesVM[-1].Size = $System_Drawing_Size
        $CheckboxesVM[-1].text = $_
        if($fx -ge 1000 ) {
            $FX = 100
            $FY = $FY + 25
        }
        $System_Drawing_Point = New-Object System.Drawing.Point($FX,$FY)
        $CheckboxesVM[-1].Location = $System_Drawing_Point
        $CheckboxesVM[-1].DataBindings.DefaultDataSourceUpdateMode = 0
        $CheckboxesVM[-1].name = "server"
        $FX = $FX + 155
        $form1.Controls.Add($CheckboxesVM[-1])
        $y=$FY + 27
    }
    $FX = 100
    $FY = $FY + 27
    #endregion checkboxesVM

    $form1.Controls.Add($Checkboxes[-1])
}
#endregion Checkboxes

#region Buttons & Boxes
$button1.TabIndex = 1
$button1.Name = "button1"
$System_Drawing_Size = New-Object System.Drawing.Size(75,23)
$button1.Size = $System_Drawing_Size
$button1.UseVisualStyleBackColor = $True
$button1.Text = "Run Script"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = 25
$z = if($y -ge $FY){
    ($y + 27) }
else {
    ($FY + 27)
}
$System_Drawing_Point.Y = $z
$button1.Location = $System_Drawing_Point
$button1.DataBindings.DefaultDataSourceUpdateMode = 0
$button1.add_Click($handler_button1_Click)
$form1.Controls.Add($button1)
$listBox1 = New-Object System.Windows.Forms.ListBox
$listBox1.FormattingEnabled = $True
$System_Drawing_Size = New-Object System.Drawing.Size(400,60)
$listBox1.Size = $System_Drawing_Size
$listBox1.DataBindings.DefaultDataSourceUpdateMode = 0
$listBox1.Name = "listBox1"
$System_Drawing_Point = New-Object System.Drawing.Point
$System_Drawing_Point.X = ($x+100)
$System_Drawing_Point.Y = $y
$listBox1.Location = $System_Drawing_Point
$form1.Controls.Add($listBox1)
}
#End GenerateForm Function
#Call the Function
GenerateForm

1 个解决方案

#1


0  

From my Powershell GUI's, I run a function that checks the.checked state of boxes and radio buttons like this.

从Powershell GUI中,我运行一个函数来检查。复选框和单选按钮的状态如下所示。

If ($CheckBox.Checked -eq $true){Write-Host "Do Something !"}

I call the function from the $CheckBox_CheckedChanged event, so that my gui is updated immediately.

我从$CheckBox_CheckedChanged事件调用函数,以便立即更新gui。

#1


0  

From my Powershell GUI's, I run a function that checks the.checked state of boxes and radio buttons like this.

从Powershell GUI中,我运行一个函数来检查。复选框和单选按钮的状态如下所示。

If ($CheckBox.Checked -eq $true){Write-Host "Do Something !"}

I call the function from the $CheckBox_CheckedChanged event, so that my gui is updated immediately.

我从$CheckBox_CheckedChanged事件调用函数,以便立即更新gui。