Powershell / 多个复选框,一个事件处理程序

问题描述 投票:0回答:1

我正在制作一个 PowerShell 脚本,该脚本使用 Windows 窗体创建图形用户界面 (GUI) 应用程序,并允许用户通过复选框选择特定邮箱属性(例如 DisplayName、UserPrincipalName),以导出到 CSV 文件以获取以下列表:邮箱。

有大量的邮箱(Exchange)属性。例如 DisplayName、UserPrincipalName、别名等。通过避免编辑从 Office 365 导出的 CSV,该程序几乎可以节省几分钟的时间。我很可能在这方面花费的时间比仅仅编辑导出的 CSV 的时间要多。我余下的职业生涯 :P

简而言之,具有独特属性的多个复选框和单个事件处理程序。

我目前正在为每个单独的复选框创建一个单独的复选框和一个单独的事件处理程序。例如,假设以下内容针对不同的邮箱属性重复了 30 多次:

#Creates a new Check Box named $DisplayNameCheckBox
$DisplayNameCheckBox = New-Object System.Windows.Forms.CheckBox
#Displays the text "DisplayName" next to the check box
$DisplayNameCheckBox.Text = "DisplayName"
#Places the $ExitButton on the GUI at (X,Y) position
$DisplayNameCheckBox.Location = New-Object System.Drawing.Point(10, 20)
#Adds a click event handler to the button. The code inside the script block { ... } will execute when the button is clicked.
$DisplayNameCheckBox.Add_Click({
        #If $DisplayNameCheckBox is ticked (true)
            if ($DisplayNameCheckBox.Checked) {
                #"DisplayNameCheckBox is checked" is displayed in console
                Write-Host "DisplayNameCheckBox is checked"
                #Adds DisplayName to ArrayList
                $mailboxPropertiesArrayList.Add("DisplayName")
                #"Current Values in mailboxPropertiesArrayList: $mailboxPropertiesArrayList" is displayed in console
                Write-Host "Current Values in mailboxPropertiesArrayList: $mailboxPropertiesArrayList"

我创建了一个函数,它本质上创建了这些复选框,并与单独创建它们相比减少了所需的代码行数。现在有一个事件处理程序可以处理所有这些复选框。然而,我遇到了一些问题:

  • 复选框的邮箱 $property 值为“Null”。看来 $checkBox.Add_Click 不知道正在单击哪个复选框 这可能是因为 $checkBox.Add_Click 位于函数内。我尝试返回这个,但我预计会出现一些奇怪的行为,它要么为空,要么 $property 的值是不正确的值。

我需要一种方法来减少创建这些复选框时的代码量,但我还需要程序来识别正在单击哪个复选框,以便可以准确地将其添加到mailboxPropertiesArrayList(我在调试时将mailboxPropertiesArrayList设为全局,但尚未将其更改回来。请忽略:) )

我创建了一个函数,本质上是创建这些复选框并减少使用的代码行数。例如:

function Add-CheckBox($text, $location, $propertyName) {
        Write-Host $propertyName
        $checkBox = New-Object System.Windows.Forms.CheckBox
        $checkBox.Text = $text
        $checkBox.Location = $location
        $FormGUI.Controls.Add($checkBox)
        $property = $propertyName
    
        # Return the checkbox and property name as an object
        return [PSCustomObject]@{
            CheckBox = $checkBox
            PropertyName = $property
    }
}
# Define checkbox properties and add them to the form
$checkboxes = @()
$checkboxes += Add-CheckBox "DisplayName" (New-Object Drawing.Point(10, 20)) "DisplayName"
$checkboxes += Add-CheckBox "UserPrincipalName" (New-Object Drawing.Point(10, 42)) "UserPrincipalName"
$checkboxes += Add-CheckBox "Alias" (New-Object Drawing.Point(10, 60)) "Alias"
$checkboxes += Add-CheckBox "TotalItemSize" (New-Object Drawing.Point(10, 80)) "TotalItemSize"

The Event Click Handler was originally within the function. The value of $property is null. 

It appears the the $checkBox.Add_Click does not know what checkbox is being clicked. It has no idea. This might be because the  $checkBox.Add_Click is in a function. This was my previous attempt: - I also had mailboxPropertiesArrayList as a global variable in case this was also being lost within the function.

创建复选框并将其添加到表单的功能

函数 Add-CheckBox($text, $location, $propertyName) { 写入主机 $propertyName $checkBox = 新对象 System.Windows.Forms.CheckBox $checkBox.Text = $text $checkBox.Location = $位置 $FormGUI.Controls.Add($checkBox)

# Define the click event handler for the checkbox
$checkBox.Add_Click({
    param($sender, $eventArgs)
    $clickedCheckBox = $sender
    $property = $propertyName  # Capture the value of $propertyName in the closure
    if ($clickedCheckBox.Checked) {
        Write-Host "$property CheckBox is checked"
        $Global:mailboxPropertiesArrayList.Add($property)
        Write-Host "Current Values in mailboxPropertiesArrayList: $($Global:mailboxPropertiesArrayList -join ', ')"
    }
})

}


I then had all the check boxes created in the function to reduce the lines of code to make each check box and then $checkBox.Add_Click was placed outside the function. For example:

foreach ($checkbox in $checkboxes) { $checkbox.CheckBox.Add_Click({ 参数($sender,$eventArgs) $clickedCheckBox = $sender $property = $checkbox.PropertyName # 使用复选框对象中的 PropertyName 如果($clickedCheckBox.Checked){ Write-Host“$property CheckBox 已选中” $Global:mailboxPropertiesArrayList.Add($property) 写入主机“mailboxPropertiesArrayList 中的当前值:$($Global:mailboxPropertiesArrayList -join ', ')” } }) }


I was getting closer. Now $property has a value but the value is always TotalItemSize no matter which check box I click.
powershell user-interface exchangewebservices
1个回答
0
投票

抱歉格式不佳。我还在学习如何使用这个网站:P

© www.soinside.com 2019 - 2024. All rights reserved.