Windows Form如何将命令结果输出到文本框

Windows Form How to output command results to textbox

我对 Windows 表单还很陌生,所以请记住这一点。我确实花了很多时间搜索和寻找答案(在提问之前应该这样做)。

所以我的问题是我正在尝试将 Powershell 结果输出显示到文本框。我的脚本有单选按钮,提供检查用户 AD 帐户状态、解锁帐户、禁用等选项。他们 select “组框”中的收音机,然后点击开始按钮。然后我想将命令输出显示到文本框。因此,例如,我应该能够将 Get-Aduser 过滤的结果显示到文本框。

这是我的结束函数的一个例子(我还没有向它添加所有未来的单选按钮):

Function StartOptions
{ 
$User=$InputBox.text 
  If ($Domain1Button.checked -eq $true) {$Domain = "Domain1"}
  If ($Domain2Button.checked -eq $true) {$Domain = "Domain2"}
  If ($UserStatusButton.checked -eq $true) {Get-ADUser -identity $User -Server $Domain -Properties * | Select-Object SamAccountName,Name,LockedOut,Enabled,EmailAddress,PasswordLastSet|Format-Table}
  $outputBox.Text= (Not sure what to put here to Output the results of Aduser)
  
  } 

组框示例:

#GROUPBOX Domain Selection
$groupBox1 = New-Object System.Windows.Forms.GroupBox 
$groupBox1.Location = New-Object System.Drawing.Size(20,90) 
$groupBox1.size = New-Object System.Drawing.Size(175,37) 
$groupBox1.text = "Choose a Domain:" 
$Form.Controls.Add($groupBox1)

单选按钮示例:

$Domain1Button= New-Object System.Windows.Forms.RadioButton
$Domain1Button.Location = '15,15'
$Domain1Button.size = '72,20'
$Domain1Button.Checked = $true
$Domain1Button.Text = "Domain1"
$groupBox1.Controls.Add($Domain1Button) 

如果这是一个愚蠢的问题,请原谅我。我真的很努力学习。

非常感谢。

根据我上面的评论...

Web 上有很多示例,Youtube 此用例很常见,远早于 PowerShell 出现。这就是了解和使用 'Form Events'.

Events Overview (Windows Forms)

'PowerShell assign command output to a winform text box'

Displaying Output in a GUI Application

$buttonComputerName_Click = {
    $textboxResults.Text = $env:ComputerName
}

How to execute a script from a Windows Form and capture the output in a text box

MS powershellgallery.com 中有用于构建 WinForm/WPF GUI 的模块...

Find-Module -Name '*gui*'
Find-Module -Name '*form*'

...只需安装并使用它们。

还有这个免费的 PowerShell GUI 开发网站。

https://poshgui.com