0

I currently have a script that queries our AD for the users' AD attribute "Department".

Right now, the script will run "successfully" but all output for Textbox2 goes to the console instead of TextBox2.

If I change my function Get-CCUsers to be a query without variables, like Get-ADUser -Filter "Department -eq 19330" (we use numbers for our departments) then the output shows in TextBox2 as I want it to.

How can I get my function to populate TextBox2?
BTW, this script was cobbled together with my limited understanding, and there may well be superfluous or nonsense lines in here.

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

$form = New-Object System.Windows.Forms.Form
$form.Text = 'Howard Center Profile Migration'
$form.Size = New-Object System.Drawing.Size(800,650)
$form.StartPosition = 'CenterScreen'

$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 enter the Cost Center # in the space below:'
$form.Controls.Add($label)

$textBox = New-Object System.Windows.Forms.TextBox
$textBox.Location = New-Object System.Drawing.Point(10,40)
$textBox.Size = New-Object System.Drawing.Size(100,20)
$form.Controls.Add($textBox)

$RunButton = New-Object System.Windows.Forms.Button
$RunButton.Location = New-Object System.Drawing.Point(75,120)
$RunButton.Size = New-Object System.Drawing.Size(75,23)
$RunButton.Text = 'RUN'
#$RunButton.DialogResult = [System.Windows.Forms.DialogResult]::OK


<#$RunButton.Add_Click({
#add here code triggered by the event
   $TextBox2.Text = Get-Process | Format-Table -Property ProcessName, Id, CPU -AutoSize | Out-String
})
#>

$form.AcceptButton = $RunButton
$form.Controls.Add($RunButton)

$label2 = New-Object System.Windows.Forms.Label
$label2.Location = New-Object System.Drawing.Point(10,70)
$label2.Size = New-Object System.Drawing.Size(280,20)
$label2.Text = 'When you are ready click the Run button below'
$form.Controls.Add($label2)

$TextBox2 = New-Object system.windows.Forms.TextBox
$TextBox2.Text = ""
$TextBox2.Multiline = $true
$TextBox2.BackColor = "#013686"
$TextBox2.ScrollBars = "Both"
$TextBox2.Width = 750
$TextBox2.Height = 450
$TextBox2.location = new-object system.drawing.point(10,150)
$TextBox2.Font = "Microsoft Sans Serif,10"
$TextBox2.ForeColor = "#ffffff"



$Form.controls.Add($TextBox2)

$form.Topmost = $true

function Get-CCUsers {
Write-Host "The textbox text is $textbox.Text"
$dept = $textBox.Text
$deptUsers = Get-ADUser -Filter "Department -eq $dept"
ForEach ($user in $deptUsers) {
    IF ( ((get-aduser $user).enabled) -eq $True ) {
        $UHomeDir = (Get-ADUser $user -Properties HomeDirectory).HomeDirectory
        $UProfPath = (Get-ADUser $user -Properties ProfilePath).ProfilePath
        Write-Host "$user, $UHomeDir, $UProfPath"
        }
    }
}

$RunButton.Add_Click({
    $TextBox2.Text = Get-CCUsers
        })

$TextBox2.Add_TextChanged({
    $TextBox2.SelectionStart = $TextBox2.Text.Length
    $TextBox2.ScrollToCaret()
    })

$form.Add_Shown({$Form.Update()})
$result = $form.ShowDialog()

$global:x = $textBox.Text
# $x


# if ($result -eq [System.Windows.Forms.DialogResult]::OK)
#{




#}
mklement0
  • 382,024
  • 64
  • 607
  • 775

1 Answers1

0

I have made your script working changing the 'Get-CCUSers' function

function Get-CCUsers {
Write-Host "The textbox text is $textbox.Text"
$dept = $textBox.Text
$deptUsers = Get-ADUser -Filter "Department -eq '$dept'"
$res = @()
ForEach ($user in $deptUsers) {
    IF ( ((get-aduser $user).enabled) -eq $True ) {
        $UHomeDir = (Get-ADUser $user -Properties HomeDirectory).HomeDirectory
        $UProfPath = (Get-ADUser $user -Properties ProfilePath).ProfilePath
        $res += "$user, $UHomeDir, $UProfPath"
        }
    }
return $res
}

In short:

  1. I removed the Write-Host (the output was actually redirected to stdout)
  2. I added a $res in the function initialized as array
  3. In the ForEach loop, results are added as items in the array
  4. The $res is returned by the function, and then used $RunButton
mklement0
  • 382,024
  • 64
  • 607
  • 775
reverpie
  • 371
  • 2
  • 6
  • Thanks, Pietro. I'd been going at this for 2 days. – Michael Louth Nov 11 '18 at 00:30
  • It would be simpler - and more efficient - to _only_ remove the `Write-Host`, given that you can use a `foreach` loop as an _expression_ too (e.g., `$arr = foreach ($i in 1..3) { $_ }`); in doing so you won't need an aux. array variable, and you avoid the inefficient incremental "adding" to the array (which requires creating a new array every time). Quibble: `Write-Host` doesn't write to _stdout_, it writes to the _host_, which in a console window is the console (terminal), which _bypasses_ PowerShell's stdout equivalent, the success stream. – mklement0 Nov 11 '18 at 03:45
  • mkelement0 is right about the efficiency: https://stackoverflow.com/questions/14620290/powershell-array-add-vs My supposition was not to deal with large arrays. Is that the case here? Then a better code is probably needed. In this case a textbox in a GUI is not the right container too – reverpie Nov 11 '18 at 15:13
  • I had added the Write-Host line for my own troubleshooting purposes--I knew it was not functionally needed. Could you expand on your "$arr = foreach" logic? Would I need to return at the end of the function "return $arr" as in reverpie's example? – Michael Louth Nov 11 '18 at 16:15
  • mkelement0 I've looked at the link above. I'd like to try your method, but I cannot figure out how to make it work with my ForEach loop. I don't understand where or how to use the $_ with my ForEach loop. – Michael Louth Nov 11 '18 at 17:41
  • @reverpie, I think some of the arrays might be large. I think I finally mastered mkelement0's suggestion – Michael Louth Nov 12 '18 at 02:24
  • @mkelement I think I mastered your suggestion, below is what I came up with. – Michael Louth Nov 12 '18 at 02:25
  • `function Get-CCUsers { $dept = $textBox.Text $deptUsers = Get-ADUser -Filter "Department -eq '$dept'" $deptUsers | ForEach { $_ IF ( ((get-aduser $_).enabled) -eq $True ) { $UHomeDir = (Get-ADUser $_ -Properties HomeDirectory).HomeDirectory $UProfPath = (Get-ADUser $_ -Properties ProfilePath).ProfilePath $arr += "$_, $UHomeDir, $UProfPath" + "`r`n" } } | Out-Null return $arr } – Michael Louth Nov 12 '18 at 02:27