简体   繁体   中英

Outputting to a GUI Powershell

Hope you can help.

I have the below code that I am running to get a list of the groups an AD user is in.

I have created the GUI as below and I am trying to get the output of the AD command to appear inside the GUI, where I have created a text box.

My two issues are:

  1. If you click 'Search', the GUI window closes.
  2. If you remove lines to keep the GUI window from closing, it doesn't output anything to the text box.

Any help would be appreciated - really think it is a line of code I am missing! Thanks

$testform.StartPosition = 'CenterScreen'
$okb = New-Object System.Windows.Forms.Button
$okb.Location = New-Object System.Drawing.Point(85,130)
$okb.Size = New-Object System.Drawing.Size(75,25)
$okb.Text = 'Search'
$okb.DialogResult = [System.Windows.Forms.DialogResult]::OK
$testform.AcceptButton = $okb
$testform.Controls.Add($okb)
$test.Location = New-Object System.Drawing.Point(270,130)
$test.Size = New-Object System.Drawing.Size(75,25)
$test.Text = 'close'
$test.DialogResult = [System.Windows.Forms.DialogResult]::Cancel
$testform.AcceptButton = $test
$testform.Controls.Add($test)
$lb = New-Object System.Windows.Forms.Label
$lb.Location = New-Object System.Drawing.Point(20,40)
$lb.Size = New-Object System.Drawing.Size(240,20)
$lb.Text = 'Please enter the username:'
$testform.Controls.Add($lb)
$tb = New-Object System.Windows.Forms.TextBox
$tb.Location = New-Object System.Drawing.Point(40,80)
$tb.Size = New-Object System.Drawing.Size(240,20)
$textBoxDisplay = New-Object 'System.Windows.Forms.TextBox'
$textBoxDisplay.Location = '30, 175'
$textBoxDisplay.Multiline = $true
$textBoxDisplay.Name = "textBoxDisplay"
$textBoxDisplay.Size = '470, 150'
$textBoxDisplay.TabIndex = 1
$testform.Controls.Add($tb)
$testform.Controls.Add($textBoxDisplay)
$testform.Topmost = $true
$testform.Add_Shown({$tb.Select()})
$rs = $testform.ShowDialog()
if ($rs -eq [System.Windows.Forms.DialogResult]::OK)
{
$y = $tb.Text
    $answer = (Get-ADUser $y  -Properties MemberOf).memberof | Get-ADGroup | Select-Object name ```

You may define your function as:

$Button_Click = {
    $y = $tb.Text
    $answer = ....
    $textBoxDisplay.Text=$answer
}

and assign it to the button click event using Add_Click(...) :

$okb.Add_Click($Button_Click)

and then show the dialog:

$testform.ShowDialog()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM