PowerShell:如何获取在组合框中选择的信息,以便稍后用作变量? [英] PowerShell: How do I get information selected in a combo box to be used as a variable later on in?

查看:71
本文介绍了PowerShell:如何获取在组合框中选择的信息,以便稍后用作变量?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I am trying to create a script to aid my helpdesk team when creating an AD Account. So far I am able to create a user account with a few attributes, however I am trying to make the script capable of filling all the required details out. What I would like to do is have a set list of locations and once a location is selected the script will use the variable $UserLocation in a range of functions, for now I just want the text returned ie; a user selects 'Location1' the variable $UserLocation returns the value 'Location1'. However at the moment it doesn't seem to work, I realise this is a really silly question but I can't figure it out for the life of me.

有问题的区域是##GetUserLocation 最终结果是用户 $UserLocation 返回组合框中选择的文本.

The area that has the issue is ##GetUserLocation The end result is to user $UserLocation to return the text selected in the combo box.

我到目前为止的代码是:

The Code I have so far is:

$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,做得很好 :)

Very nice UI, nicely done :)

仔细查看后,您有多次设置 $combobox1.add_SelectedIndexChanged 的代码,从而有效地删除了我在之前的回答中建议的事件处理程序.还有一个在您的代码中但根本没有使用的函数,一个 ComboList 助手.我在此处删减了您的代码,现在可以正常工作了.

On closer review, you had code to set $combobox1.add_SelectedIndexChanged more than once, effectively removing the eventhandler I suggested in my previous answer. There was also a function that was in your code but not used at all, a ComboList helper. I trimmed your code down here and it now works fine.

要点链接

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

Here's your issue. You are correctly setting up an event handler to trigger when a user changes the selected item in a combobox.

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

但是,此事件处理程序仅在用户单击确定"按钮之后 设置,这不是您想要执行的操作.此外,当用户点击确定按钮时,用户界面应该前进到下一个用户界面元素.所以只需像这样更改它:

BUT, this event handler is only set up after the user clicks the OK button, which isn't what you want to do. Also, when the user hits the OK button, the UI should probably progress to the next UI element. So just change it like this:

#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 变量现在保存了选定的值.

After testing this, the UI closes as expected, and also the $userLocation variable now held the value selected.

这篇关于PowerShell:如何获取在组合框中选择的信息,以便稍后用作变量?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆