Powershell 文本框捕获结果

问题描述 投票:0回答:1

场景:

我有一个带有 TEXTBOXA、TEXTBOXB 和一个 BUTTON 的表单。 BUTTON 会将 TEXTBOXB 中输入的所有组添加到正常工作的 TEXTBOXA 中。 但它不会通知我是否有任何组添加失败。

所以我想添加日志捕获。

For 循环的所有结果都应显示在 TextboxB 中。 目前,它只会显示它已完成,而不会提醒我任何错误。

这是代码:

Foreach ($group in $groups)
{
Add-ADGroupMember -Identity $group -Members $loginid.text -credential $admin -Confirm:$false
$Output2.text = "All Groups Added. Please review and confirm."
clear-content $bulkgrouppath

尝试使用写入错误和写入输出,但我认为我没有正确使用它

powershell forms textbox
1个回答
0
投票

您可以使用

try / catch
进行错误处理,作为当前实现的示例:

foreach ($group in $groups) {
    try {
        Add-ADGroupMember -Identity $group -Members $loginid.text -Credential $admin -Confirm:$false
        $Output2.Text = 'All Groups Added. Please review and confirm.'
        Clear-Content $bulkgrouppath
    }
    catch {
        # assign the error message to the TextBoxB
        $Output2.Text = $_.Exception.Message
        # additionally you can output it to the console for additional troubleshooting
        Write-Error $_
    }
}

如果您想附加错误消息到文本框,您可以使用:

$Output2.Text += $_.Exception.Message + [System.Environment]::NewLine
© www.soinside.com 2019 - 2024. All rights reserved.