使用函数填充 Powershell 中的文本框

Using a function to populate a textbox in Powershell

我目前有一个脚本可以查询我们的 AD 以获取用户的 AD 属性 "Department"。

现在,脚本将 运行 "successfully" 但 Textbox2 的所有输出都转到控制台而不是 TextBox2。

如果我将我的函数 Get-CCUsers 更改为不带变量的查询,例如 Get-ADUser -Filter "Department -eq 19330"(我们的部门使用数字),那么输出会按照我的意愿显示在 TextBox2 中。

如何让我的函数填充 TextBox2?
顺便说一句,这个脚本是我有限的理解拼凑而成的,这里可能会有多余或废话。

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)
#{




#}

我已经让你的脚本工作改变 'Get-CCUSers' 函数

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
}

简而言之:

  1. 我删除了 Write-Host(输出实际上被重定向到标准输出)
  2. 我在初始化为数组的函数中添加了一个$res
  3. 在 ForEach 循环中,结果作为项目添加到数组中
  4. 函数返回$res,然后使用$RunButton