Share via

List box and parameters

Mark Verhunce 41 Reputation points
2022-04-11T20:08:28.62+00:00

I have written a script that creates a new user in AD. Right now the script asks for parameters first and then a window pops up to choose what department the new employee will be added to and thus be made a member of the groups for that department.

I have a switch parameter for each department and string parameters for the AD attributes.

I would like the list box to open before entering parameters.

Here is the code:

[CmdletBinding()]
param (
[Parameter(
Mandatory = $False,
ValueFromPipeline = $True,
ValueFromPipelineByPropertyName = $True)]
[Alias("A")]
[Switch] $Accounting,

    [Parameter(
        Mandatory = $False,
        ValueFromPipeline = $True,
        ValueFromPipelineByPropertyName = $True)]
        [Alias("CU")]
        [Switch] $ConstructionUser

[Parameter(
Mandatory = $True,
ValueFromPipeline = $True,
ValueFromPipelineByPropertyName = $True)]
[Alias("FN")]
[String] $FirstName,

[Parameter(
Mandatory = $True,
ValueFromPipeline = $True,
ValueFromPipelineByPropertyName = $True)]
[Alias("LN")]
[String] $LastName)

Add-Type -AssemblyName System.Windows.Forms
Add-Type -AssemblyName System.Drawing

$Form = New-Object System.Windows.Forms.Form
$Form.Text = 'Select a Department!'
$Form.Size = New-Object System.Drawing.Size(300,200)
$Form.StartPosition = 'CenterScreen'

$OkButton = New-Object System.Windows.Forms.Button
$OkButton.Location = New-Object System.Drawing.Point(75,120)
$OkButton.Size = New-Object System.Drawing.Size(75,23)
$OkButton.Text = 'OK'
$OkButton.DialogResult = [System.Windows.Forms.DialogResult]::OK
$Form.AcceptButton = $OkButton
$Form.Controls.Add($OkButton)

$CancelButton = New-Object System.Windows.Forms.Button
$CancelButton.Location = New-Object System.Drawing.Point(150,120)
$CancelButton.Size = New-Object System.Drawing.Size(75,23)
$CancelButton.Text = 'Cancel'
$CancelButton.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$Form.CancelButton = $CancelButton
$Form.Controls.Add($CancelButton)

$Label = New-Object System.Windows.Forms.Label
$Label.Location = New-Object System.Drawing.Point(10,20)
$Label.Size = New-Object System.Drawing.Size(280,20)
$Label.Text = 'Please select a Department:'
$Form.Controls.Add($Label)

$ListBox = New-Object System.Windows.Forms.ListBox
$ListBox.Location = New-Object System.Drawing.Point(10,40)
$ListBox.Size = New-Object System.Drawing.Size(260,20)
$ListBox.Height = 80

[void] $ListBox.Items.Add('Accounting')
[void] $ListBox.Items.Add('Construction User')

$Form.Controls.Add($ListBox)

$Form.Topmost = $true

$Result = $Form.ShowDialog()

If ($Result -eq [System.Windows.Forms.DialogResult]::OK)
{
    $Department = $ListBox.SelectedItem
}

If ($Department -eq Acounting)
{Write-Host $Department}

ElseIf ($Department -eq 'Construction User')
{Write-Host = "Construction User was selected"}

Windows for business | Windows Server | User experience | PowerShell
0 comments No comments

1 answer

Sort by: Most helpful
  1. Mark Verhunce 41 Reputation points
    2022-04-12T15:59:14.093+00:00

    I have found the answer. A command can not be executed before parameters.

    Was this answer helpful?

    0 comments No comments

Your answer

Answers can be marked as 'Accepted' by the question author and 'Recommended' by moderators, which helps users know the answer solved the author's problem.