Quantcast
Channel: PowerShell.com – PowerShell Scripts, Tips, Forums, and Resources
Viewing all articles
Browse latest Browse all 6937

returning a value from a drop-down

$
0
0

Hello,

I'm writing a script to create new users through a form (the normal user creation process is long and complex and therefore prone to human error), but I'm having issues capturing values from a drop-down.

Here is my code.... (sorry couldn't see how to insert it as a block of code!)

########################

# Edit This item to change the DropDown Values

[array]$DropDownArray = "John", "Peter", "Paul"

# This Function Returns the Selected Value and Closes the Form

function Return-DropDown {

 $Choice = $DropDown.SelectedItem.ToString()
 $Form.Close()
 Write-Host $Choice

}

[System.Reflection.Assembly]::LoadWithPartialName("System.Windows.Forms")
[System.Reflection.Assembly]::LoadWithPartialName("System.Drawing")


$Form = New-Object System.Windows.Forms.Form

$Form.width = 300
$Form.height = 150
$Form.Text = ”DropDown”

$DropDown = new-object System.Windows.Forms.ComboBox
$DropDown.Location = new-object System.Drawing.Size(100,10)
$DropDown.Size = new-object System.Drawing.Size(130,30)

ForEach ($Item in $DropDownArray) {
 $DropDown.Items.Add($Item)
}

$Form.Controls.Add($DropDown)

$DropDownLabel = new-object System.Windows.Forms.Label
$DropDownLabel.Location = new-object System.Drawing.Size(10,10)
$DropDownLabel.size = new-object System.Drawing.Size(100,20)
$DropDownLabel.Text = "Items"
$Form.Controls.Add($DropDownLabel)

$Button = new-object System.Windows.Forms.Button
$Button.Location = new-object System.Drawing.Size(100,50)
$Button.Size = new-object System.Drawing.Size(100,20)
$Button.Text = "Select an Item"
$Button.Add_Click({Return-DropDown})
$form.Controls.Add($Button)

$Form.Add_Shown({$Form.Activate()})
$Form.ShowDialog()

$FirstName = $Choice
$LastName = "Test"
$Name = $FirstName + " " + $LastName
#Get First Initial from Firstname.
#From position 0 select the first 1 character
$FirstInitial = $FirstName.Substring(0,1)
$Alias = $FirstInitial + $LastName
$Email = $Alias + "@daslaw.co.uk"

New-Mailbox -UserPrincipalName $Email -Alias $Alias -Database "NonTeam" -Name $Name -OrganizationalUnit "Test Team" -Firstname $FirstName -LastName $LastName -DisplayName $Name -ResetPasswordOnNextLogon $true

########################

I've cut the code back to try and locate the error and it seems as though the data is being written to the variable ($Choice) as it appears in the host screen using "Write-Host $Choice" but it looks as though it is being cleared after.

 


Viewing all articles
Browse latest Browse all 6937

Trending Articles