PowerShell:如何获取在组合框中选择的信息,以便稍后用作变量?

PowerShell: How do I get information selected in a combo box to be used as a variable later on in?

我正在尝试创建一个脚本来帮助我的服务台团队创建 AD 帐户。 到目前为止,我能够创建一个具有一些属性的用户帐户,但是我正在尝试使脚本能够填写所有必需的详细信息。 我想要做的是有一组位置列表,一旦选择了一个位置,脚本将在一系列函数中使用变量 $UserLocation,现在我只想要文本 returned ie;用户选择 'Location1' 变量 $UserLocation returns 值 'Location1'。但是目前它似乎不起作用,我意识到这是一个非常愚蠢的问题,但我终生无法弄清楚。

有问题的区域是 ##GetUserLocation 最终结果是用户 $UserLocation 到 return 在组合框中选择的文本。

我目前的代码是:

$primaryTelexNumber = "1"
$telexNumber = "7"

##Get Users First Name
[string] [Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$title = "AD New User Script: First Name"
$msg = "Please Enter the new User's First Name"
$FirstName = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title)

##Get Users Last Name
[string] [Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$title = "AD New User Script: Last Name"
$msg = "Please Enter the new User's Last Name"
$LastName = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title)

##Define UserName - not wokring yet hense manual capture
#$UserName = "$LastName($FirstName.SubString(0,1))"

##Get UserName
[string] [Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$title = "AD New User Script: User Name"
$msg = "Please Enter the proposed User Name"
$UserName = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title)

##Set UserPassowrd
[string] [Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$title = "AD New User Script: Password"
$msg = "Please Enter initial Password"
$Password = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title) 

##GetUserLocation
function Show-DropDownSelector_psf {

    #----------------------------------------------
    #region Import the Assemblies
    #----------------------------------------------
    [void][reflection.assembly]::Load('System.Drawing, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b03f5f7f11d50a3a')
    [void][reflection.assembly]::Load('System.Windows.Forms, Version=4.0.0.0, Culture=neutral, PublicKeyToken=b77a5c561934e089')
    #endregion Import Assemblies

    #----------------------------------------------
    #region Generated Form Objects
    #----------------------------------------------
    [System.Windows.Forms.Application]::EnableVisualStyles()
    $formADNewUserScriptSetUs = New-Object 'System.Windows.Forms.Form'
    $buttonCancel = New-Object 'System.Windows.Forms.Button'
    $buttonOK = New-Object 'System.Windows.Forms.Button'
    $labelSelectUserLocation = New-Object 'System.Windows.Forms.Label'
    $combobox1 = New-Object 'System.Windows.Forms.ComboBox'
    $InitialFormWindowState = New-Object 'System.Windows.Forms.FormWindowState'
    #endregion Generated Form Objects

    #----------------------------------------------
    # User Generated Script
    #----------------------------------------------
    
    $formADNewUserScriptSetUs_Load={
        #TODO: Initialize Form Controls here
        
    }
    
    #region Control Helper Functions
    function Update-ComboBox
    {
    <#
        .SYNOPSIS
            This functions helps you load items into a ComboBox.
        
        .DESCRIPTION
            Use this function to dynamically load items into the ComboBox control.
        
        .PARAMETER ComboBox
            The ComboBox control you want to add items to.
        
        .PARAMETER Items
            The object or objects you wish to load into the ComboBox's Items collection.
        
        .PARAMETER DisplayMember
            Indicates the property to display for the items in this control.
            
        .PARAMETER ValueMember
            Indicates the property to use for the value of the control.
        
        .PARAMETER Append
            Adds the item(s) to the ComboBox without clearing the Items collection.
        
        .EXAMPLE
            Update-ComboBox $combobox1 "Red", "White", "Blue"
        
        .EXAMPLE
            Update-ComboBox $combobox1 "Red" -Append
            Update-ComboBox $combobox1 "White" -Append
            Update-ComboBox $combobox1 "Blue" -Append
        
        .EXAMPLE
            Update-ComboBox $combobox1 (Get-Process) "ProcessName"
        
        .NOTES
            Additional information about the function.
    #>
        
        param
        (
            [Parameter(Mandatory = $true)]
            [ValidateNotNull()]
            [System.Windows.Forms.ComboBox]
            $ComboBox,
            [Parameter(Mandatory = $true)]
            [ValidateNotNull()]
            $Items,
            [Parameter(Mandatory = $false)]
            [string]$DisplayMember,
            [Parameter(Mandatory = $false)]
            [string]$ValueMember,
            [switch]
            $Append
        )
        
        if (-not $Append)
        {
            $ComboBox.Items.Clear()
        }
        
        if ($Items -is [Object[]])
        {
            $ComboBox.Items.AddRange($Items)
        }
        elseif ($Items -is [System.Collections.IEnumerable])
        {
            $ComboBox.BeginUpdate()
            foreach ($obj in $Items)
            {
                $ComboBox.Items.Add($obj)
            }
            $ComboBox.EndUpdate()
        }
        else
        {
            $ComboBox.Items.Add($Items)
        }
        
        if ($DisplayMember)
        {
            $ComboBox.DisplayMember = $DisplayMember
        }
        
        if ($ValueMember)
        {
            $ComboBox.ValueMember = $ValueMember
        }
    }
    
    
    #endregion
    
    $combobox1_SelectedIndexChanged={
        #TODO: Place custom script here
        
    }
    
    $buttonOK_Click={
        #TODO: Place custom script here
        $combobox1.add_SelectedIndexChanged({ $UserLocation = $combobox1.SelectedItem })
    }
    
    # --End User Generated Script--
    #----------------------------------------------
    #region Generated Events
    #----------------------------------------------
    
    $Form_StateCorrection_Load=
    {
        #Correct the initial state of the form to prevent the .Net maximized form issue
        $formADNewUserScriptSetUs.WindowState = $InitialFormWindowState
    }
    
    $Form_Cleanup_FormClosed=
    {
        #Remove all event handlers from the controls
        try
        {
            $buttonOK.remove_Click($buttonOK_Click)
            $combobox1.remove_SelectedIndexChanged($combobox1_SelectedIndexChanged)
            $formADNewUserScriptSetUs.remove_Load($formADNewUserScriptSetUs_Load)
            $formADNewUserScriptSetUs.remove_Load($Form_StateCorrection_Load)
            $formADNewUserScriptSetUs.remove_FormClosed($Form_Cleanup_FormClosed)
        }
        catch { Out-Null <# Prevent PSScriptAnalyzer warning #> }
    }
    #endregion Generated Events

    #----------------------------------------------
    #region Generated Form Code
    #----------------------------------------------
    $formADNewUserScriptSetUs.SuspendLayout()
    #
    # formADNewUserScriptSetUs
    #
    $formADNewUserScriptSetUs.Controls.Add($buttonCancel)
    $formADNewUserScriptSetUs.Controls.Add($buttonOK)
    $formADNewUserScriptSetUs.Controls.Add($labelSelectUserLocation)
    $formADNewUserScriptSetUs.Controls.Add($combobox1)
    $formADNewUserScriptSetUs.AutoScaleDimensions = New-Object System.Drawing.SizeF(6, 13)
    $formADNewUserScriptSetUs.AutoScaleMode = 'Font'
    $formADNewUserScriptSetUs.ClientSize = New-Object System.Drawing.Size(371, 123)
    $formADNewUserScriptSetUs.Name = 'formADNewUserScriptSetUs'
    $formADNewUserScriptSetUs.Text = 'AD New User Script: Set User Location'
    $formADNewUserScriptSetUs.add_Load($formADNewUserScriptSetUs_Load)
    #
    # buttonCancel
    #
    $buttonCancel.Location = New-Object System.Drawing.Point(231, 86)
    $buttonCancel.Name = 'buttonCancel'
    $buttonCancel.Size = New-Object System.Drawing.Size(75, 23)
    $buttonCancel.TabIndex = 3
    $buttonCancel.Text = 'Cancel'
    $buttonCancel.UseVisualStyleBackColor = $True
    #
    # buttonOK
    #
    $buttonOK.Location = New-Object System.Drawing.Point(137, 86)
    $buttonOK.Name = 'buttonOK'
    $buttonOK.Size = New-Object System.Drawing.Size(75, 23)
    $buttonOK.TabIndex = 2
    $buttonOK.Text = 'OK'
    $buttonOK.UseVisualStyleBackColor = $True
    $buttonOK.add_Click($buttonOK_Click)
    #
    # labelSelectUserLocation
    #
    $labelSelectUserLocation.AutoSize = $True
    $labelSelectUserLocation.Location = New-Object System.Drawing.Point(25, 45)
    $labelSelectUserLocation.Name = 'labelSelectUserLocation'
    $labelSelectUserLocation.Size = New-Object System.Drawing.Size(106, 13)
    $labelSelectUserLocation.TabIndex = 1
    $labelSelectUserLocation.Text = 'Select User Location'
    #
    # combobox1
    #
    $combobox1.FormattingEnabled = $True
    [void]$combobox1.Items.Add('Location1')
    [void]$combobox1.Items.Add('Location2')
    [void]$combobox1.Items.Add('Location3')
    [void]$combobox1.Items.Add('Location4')
    [void]$combobox1.Items.Add('Location5')
    [void]$combobox1.Items.Add('Location6')
    [void]$combobox1.Items.Add('Location7')
    [void]$combobox1.Items.Add('Location8')
    [void]$combobox1.Items.Add('Location9')
    [void]$combobox1.Items.Add('Location10')
    [void]$combobox1.Items.Add('Location11')
    [void]$combobox1.Items.Add('Location12')
    $combobox1.Location = New-Object System.Drawing.Point(137, 42)
    $combobox1.Name = 'combobox1'
    $combobox1.Size = New-Object System.Drawing.Size(199, 21)
    $combobox1.TabIndex = 0
    $combobox1.add_SelectedIndexChanged($combobox1_SelectedIndexChanged)
    $formADNewUserScriptSetUs.ResumeLayout()
    #endregion Generated Form Code

    #----------------------------------------------

    #Save the initial state of the form
    $InitialFormWindowState = $formADNewUserScriptSetUs.WindowState
    #Init the OnLoad event to correct the initial state of the form
    $formADNewUserScriptSetUs.add_Load($Form_StateCorrection_Load)
    #Clean up the control events
    $formADNewUserScriptSetUs.add_FormClosed($Form_Cleanup_FormClosed)
    #Show the Form
    return $formADNewUserScriptSetUs.ShowDialog()

} #End Function

#Call the form
Show-DropDownSelector_psf | Out-Null

##Get E-Mail
[string] [Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$title = "AD New User Script: E-mail"
$msg = "Please Enter the user's E-mail Address"
$Email = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title)

##Get JobTitle
[string] [Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$title = "AD New User Script: Job Title"
$msg = "Please Enter the user's job title"
$JobTitle = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title)

##Get Department
[string] [Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$title = "AD New User Script: Department"
$msg = "Please Enter the user's Department"
$Department = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title)

##Get DeskPhoneNumber
[string] [Reflection.Assembly]::LoadWithPartialName('Microsoft.VisualBasic')
$title = "AD New User Script: Desk Phone Extension"
$msg = "Please Enter the user's Desk Phone Extension"
$DeskPhone = [Microsoft.VisualBasic.Interaction]::InputBox($msg, $title)

##Confirm Account Details

[System.Windows.Forms.MessageBox]::show("Verify the followning is correct:
User Name:      $UserName       
Password:       $Password
Full Name:      $FirstName $LastName
Job Title:      $JobTitle
Department:     $Department
Desk Phone Ext: $DeskPhone 
Email Address:  $Email


Click OK to continue and Create the account. Press Cancel to stop the process.
 
" , "AD New User", 1)


##Create AD User Account
$NewUserAttributes = @{
 'UserPrincipalName' = $UserName
 'DisplayName' = "$LastName $FirstName"
 'Name' = $UserName
 'GivenName' = $FirstName
 'Surname' = $LastName
 'Title' = $JobTitle
 'Department' = $Department
 'Description' = $JobTitle
 'EmailAddress' = $Email
 'SamAccountName' = $UserName
 'AccountPassword' = (ConvertTo-SecureString $Password -AsPlainText -Force)
 'Enabled' = $True
 'ChangePasswordAtLogon' = $True
 'path' = "OU=Users,OU=TEST,OU=Site,DC=Dev,DC=Test,DC=Local"
 }
 New-ADUser @NewUserAttributes
 
 ##Set IP Phone
 Set-ADUser $UserName -add @{ipPhone="$DeskPhone"}

非常好UI,干得漂亮:)

编辑*

经过仔细审查,您有不止一次设置 $combobox1.add_SelectedIndexChanged 的代码,有效地删除了我在之前的回答中建议的事件处理程序。还有一个函数在您的代码中但根本没有使用,一个 ComboList 帮助器。我在这里整理了您的代码,现在可以正常工作了。

Gist Link

旧答案

这是您的问题。您正确地设置了一个事件处理程序,以便在用户更改组合框中的所选项目时触发。

$buttonOK_Click={
  #TODO: Place custom script here
  $combobox1.add_SelectedIndexChanged({ $UserLocation = $combobox1.SelectedItem })
}

但是,此事件处理程序仅在 用户单击“确定”按钮后设置,这不是您想要的。此外,当用户点击“确定”按钮时,UI 应该可能 前进到下一个 UI 元素。所以只需像这样更改它:

#move this event handler OUTSIDE of the $buttonOK_Click handler.
$combobox1.add_SelectedIndexChanged({ $UserLocation = $combobox1.SelectedItem })

#when the user clicks OK, echo out to the screen their new selection (for debugging purposes)
#and then also close the UI, as the user would expect

$buttonOK_Click={
  #TODO: Place custom script here
  $formADNewUserScriptSetUs.Close();
  write-host "user select location $($combobox1.SelectedItem )"
}

测试后,UI 按预期关闭,并且 $userLocation 变量现在保存了选定的值。

这是我个人的做法。您在范围方面存在一些问题。我不认为在函数中创建表单是个好主意,使用它会是一场噩梦,而且你也会遇到范围的大问题。

函数旨在充当您的 UI 应用程序 (imo) 的事件侦听器。

可能有更好的方法,但我习惯于使用 Set-Variable 检索值,因为 Add_Click 事件的范围与当前范围不同。做类似 $locationResult = $combobox1.selectedItem 的事情是行不通的。

$formADNewUserScriptSetUs = New-Object 'System.Windows.Forms.Form'
$formADNewUserScriptSetUs.AutoScaleDimensions = New-Object System.Drawing.SizeF(6, 13)
$formADNewUserScriptSetUs.AutoScaleMode = 'Font'
$formADNewUserScriptSetUs.ClientSize = New-Object System.Drawing.Size(371, 123)

$combobox1 = New-Object 'System.Windows.Forms.ComboBox'
$combobox1.FormattingEnabled = $true
1..12|%{[void]$combobox1.Items.Add("Location $_")}
$combobox1.Location = New-Object System.Drawing.Point(137, 42)
$combobox1.Name = 'combobox1'
$combobox1.Size = New-Object System.Drawing.Size(199, 21)
$combobox1.TabIndex = 0

$buttonOK = New-Object 'System.Windows.Forms.Button'
$buttonOK.Location = New-Object System.Drawing.Point(137, 86)
$buttonOK.Name = 'buttonOK'
$buttonOK.Size = New-Object System.Drawing.Size(75, 23)
$buttonOK.TabIndex = 2
$buttonOK.Text = 'OK'
$buttonOK.UseVisualStyleBackColor = $True
$buttonOK.add_Click({
    Write-Host $combobox1.selectedItem #Your answer here
    # Use this    
    Set-Variable -Name locationResult -Value $combobox1.selectedItem -Force -Scope Script 
    # or this to retrieve the user selection
    $script:locationResult = $combobox1.selectedItem
})

$formADNewUserScriptSetUs.Controls.Add($buttonOK)
$formADNewUserScriptSetUs.Controls.Add($combobox1)

$formADNewUserScriptSetUs.ShowDialog()