所以我有一個poweshell窗體,您可以在其中將用戶添加到列表框並同時添加到數組列表。當我嘗試返回所有用戶的數組列表時,它不返回任何內容。從函數中返回一個ArrayList
這裏是我的代碼
#---------------------#
# Function AddUserGUI #
#---------------------#
Function AddUserGUI()
{
$AllUsers = New-Object System.Collections.ArrayList
#---Form
$Form = New-Object System.Windows.Forms.Form
$Form.FormBorderStyle = "FixedSingle"
$Form.Size = New-Object System.Drawing.Size(295,280)
$Form.MaximizeBox = $False
$Form.Text = "Add User(s)"
$Form.StartPosition = "CenterScreen"
$Form.Font = New-Object System.Drawing.Font("Segoe UI",9,0,3,1)
#---TexBox
$objTextBox = New-Object System.Windows.Forms.TextBox
$objTextBox.Location = New-Object System.Drawing.Size(10,10)
$objTextBox.Size = New-Object System.Drawing.Size(183,100)
$objTextBox.MaxLength = 100
#---ListBox
$objListBox = New-Object System.Windows.Forms.ListBox
$objListBox.Location = New-Object System.Drawing.Size(10,45)
$objListBox.Size = New-Object System.Drawing.Size(183,199)
$objListBox.HorizontalScrollbar = $True
#---Buttons
$Add = New-Object System.Windows.Forms.Button
$Add.Location = New-Object System.Drawing.Size(205,10)
$Add.Size = New-Object System.Drawing.Size(75,23)
$Add.FlatStyle = "PopUp"
$Add.Text = "Add"
$Remove = New-Object System.Windows.Forms.Button
$Remove.Location = New-Object System.Drawing.Size(205,45)
$Remove.Size = New-Object System.Drawing.Size(75,23)
$Remove.FlatStyle = "PopUp"
$Remove.Text = "Remove"
$Confirm = New-Object System.Windows.Forms.Button
$Confirm.Location = New-Object System.Drawing.Size(205,220)
$Confirm.Size = New-Object System.Drawing.Size(75,23)
$Confirm.FlatStyle = "PopUp"
$Confirm.Text = "Confirm"
#---Buttons On-Click Actions
$Add_OnClick=
{
if($objTextBox.Text -ne "")
{
$UserChk = $true #[Bool](Get-ADObject -Filter { sAMAccountName -eq $objTextBox.Text } -Searchbase "ou=User_PFS,dc=pfs,dc=local")
if($UserChk -eq $True)
{
if($objListBox.Items -contains $objTextBox.Text) {DisplayMsgBox "User already in list." "Duplicate entry" "Information" 0}
ElseIf($objTextBox.Text -ne $NULL -and $objListBox.Items -notcontains $objTextBox.Text)
{
$objListBox.Items.Add($objTextBox.Text)
[void]$AllUsers.Add($objTextBox.Text)
}
}
ElseIf($UserChk -eq $False) {DisplayMsgBox "User not found." "Invalid User Name" "Information" 0}
$objTextBox.Text = ""
}
}
$Remove_OnClick=
{
$AllUsers.Remove($objListBox.SelectedItem)
$objListBox.Items.Remove($objListBox.SelectedItem)
}
$Confirm_OnClick=
{
if($AllUsers.Count -eq 0)
{
DisplayMsgBox "Please add at least one user to the list." "No Users Found" "Information" 0
}
ElseIf($AllUsers.Count -gt 0)
{
$Form.Close()
Return $AllUsers
}
}
#---Adding Elements to the form
$Add.add_Click($Add_OnClick)
$Remove.add_Click($Remove_OnClick)
$Confirm.add_Click($Confirm_OnClick)
$Form.Controls.Add($objListBox)
$Form.Controls.Add($objTextBox)
$Form.Controls.Add($Add)
$Form.Controls.Add($Remove)
$Form.Controls.Add($Confirm)
#---Display Form
$Form.ShowDialog()| Out-Null
}
$AllUsers = AddUserGUI
foreach($User in $AllUsers){write-host $User}
foreach循環任何沒有輸出。我怎樣才能正確地返回這個數組列表?