我需要将 PowerShell 输出数据显示到 PowerShell 表单发件箱

I need to display the PowerShell output data to the PowerShell form outbox

我正在尝试执行脚本以禁用域服务器中的批量用户。用户被禁用,并显示哪个用户帐户被禁用。在 PowerShell 输出中,它显示哪些 id 已被禁用。当我尝试在 PowerShell 表单中显示相同的输出时。它仅显示最后一个用户 ID 禁用列表。供您参考的脚本和屏幕截图。

Add-Type -AssemblyName System.Windows.Forms

$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
    Multiselect = $false # Multiple files can be chosen
    Filter      = 'SpreadSheet (*.csv)|*.csv' # Specified file types
}
 
[void]$FileBrowser.ShowDialog()

$file = $FileBrowser.FileName;

If ($FileBrowser.FileNames -like "*\*") {

    # Do something 
    Import-Csv $FileBrowser.FileNames | ForEach-Object { $samAccountName = $_."samAccountName" 
        Get-ADUser -Identity $samAccountName | Disable-ADAccount
        Start-Sleep 1

        $Result = Write-Output "Account has been disabled $samAccountName "
        # Assign Result to OutputBox
        $outputBox.Text = $Result
    }
}

以下用于禁用用户帐户的脚本

Add-Type -AssemblyName System.Windows.Forms
$FileBrowser = New-Object System.Windows.Forms.OpenFileDialog -Property @{
    Multiselect = $false # Multiple files can be chosen
    Filter = 'SpreadSheet (*.csv)|*.csv' # Specified file types
}
 
[void]$FileBrowser.ShowDialog()
$file = $FileBrowser.FileName;
If($FileBrowser.FileNames -like "*\*") {
    # Do something 
    Import-Csv $FileBrowser.FileNames | ForEach-Object {$samAccountName = $_."samAccountName" 
        Get-ADUser -Identity $samAccountName | Disable-ADAccount
        Start-Sleep 1
$Result = Write-Output "Account has been disabled $samAccountName "
 # Assign Result to OutputBox
  $outputBox.Text = $Result
       
    
}
  }
}

您当前在执行 $outputBox.Text = $Result 时会覆盖之前的值。确保在更新文本框时包括以前的值,如下所示:

$outputBox.Text = $outputBox.Text,$Result -join "`r`n"

这样,每次后续更新都会添加一个换行符,然后将新值添加到文本框,因此它更像是控制台中的屏幕缓冲区。