【问题标题】:Using a function to populate a textbox in Powershell使用函数在 Powershell 中填充文本框
【发布时间】:2019-04-13 22:26:15
【问题描述】:

我目前有一个脚本可以查询我们的广告以获取用户的广告属性“部门”。

现在,脚本将“成功”运行,但 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)
#{




#}

【问题讨论】:

    标签: winforms function powershell user-interface textbox


    【解决方案1】:

    我已经让你的脚本改变了“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

    【讨论】:

    • 谢谢,彼得罗。我已经为此做了 2 天。
    • 如果您可以将foreach 循环用作表达式 i> 太(例如,$arr = foreach ($i in 1..3) { $_ });这样做你就不需要辅助了。数组变量,并且您可以避免低效的增量“添加”到数组(这需要每次都创建一个新数组)。 Quibble:Write-Host 不写入 stdout,它写入 host,在控制台窗口中是控制台(终端),绕过 PowerShell 的标准输出等价物,成功流。
    • mkelement0 关于效率是正确的:stackoverflow.com/questions/14620290/powershell-array-add-vs 我的假设是不处理大型数组。这里是这样吗?然后可能需要更好的代码。在这种情况下,GUI 中的文本框也不是正确的容器
    • 我添加了 Write-Host 行用于我自己的故障排除目的——我知道它在功能上不需要。你能扩展你的“$arr = foreach”逻辑吗?我是否需要像 reverpie 的示例那样在函数“return $arr”的末尾返回?
    • mkelement0 我查看了上面的链接。我想试试你的方法,但我不知道如何让它与我的 ForEach 循环一起工作。我不明白在哪里或如何在 ForEach 循环中使用 $_。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2019-01-29
    • 2011-01-01
    • 2013-08-09
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多