消息框响应给出了未处理的异常

时间:2019-05-16 09:10:56

标签: powershell

我正在尝试创建用于禁用用户的工具。我想创建一个“请确认您要执行此操作”消息框。

因此,我做了以下禁用用户的功能,但是当我调用该功能并在消息框上按No时,出现未处理的异常。

如果我先运行带有某些代码的第一个IF语句,那么它可以很好地工作,只是在我调用该函数时不行。

有什么想法吗?

function DisableUser { 
    if ([System.Windows.Forms.MessageBox]::Show("Are you really really really sure you want to disable this user?`n`n" + $labelNameValue.Text, "Are you sure sure?", "YesNo", "Warning") -eq "No") {
        Break
    }

    $DisabledUsersOU = "OU=Disabled Users,DC=domain,DC=local"

    $userObject = Get-ADUser -Identity $textboxSearch.Text -Properties Manager
    $adGroups = Get-ADPrincipalGroupMembership -Identity $userObject.SamAccountName | Where-Object { $_.Name -ne "Domain Users" } | Sort-Object

    # REMOVE -WhatIf

    Remove-ADPrincipalGroupMembership -Identity $labelUsernameValue.Text -MemberOf $adGroups -Confirm:$false -WhatIf
    $userObject | Move-ADObject -TargetPath $DisabledUsersOU -WhatIf
    Set-ADUser $userObject -Manager $Null -Description "$date Disabled by $env:USERNAME" -WhatIf
    Disable-ADAccount -Identity $userObject -WhatIf

    [System.Windows.Forms.MessageBox]::Show("User would have been disabled if this button worked!`nBut is doesn't, so nothing happend.", "BANG!!!!!!", "Ok", "Information")
}

这是我收到的错误消息:

************** Exception Text **************
System.Management.Automation.BreakException: System error.
   at System.Management.Automation.Interpreter.ThrowInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0)
   at System.Management.Automation.DlrScriptCommandProcessor.RunClause(Action`1 clause, Object dollarUnderbar, Object inputToProcess)
   at System.Management.Automation.DlrScriptCommandProcessor.Complete()
   at System.Management.Automation.CommandProcessorBase.DoComplete()
   at System.Management.Automation.Internal.PipelineProcessor.DoCompleteCore(CommandProcessorBase commandRequestingUpstreamCommandsToStop)
   at System.Management.Automation.Internal.PipelineProcessor.SynchronousExecuteEnumerate(Object input)
   at System.Management.Automation.PipelineOps.InvokePipeline(Object input, Boolean ignoreInput, CommandParameterInternal[][] pipeElements, CommandBaseAst[] pipeElementAsts, CommandRedirection[][] commandRedirections, FunctionContext funcContext)
   at System.Management.Automation.Interpreter.ActionCallInstruction`6.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.EnterTryCatchFinallyInstruction.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.Interpreter.Run(InterpretedFrame frame)
   at System.Management.Automation.Interpreter.LightLambda.RunVoid1[T0](T0 arg0)
   at System.Management.Automation.ScriptBlock.InvokeWithPipeImpl(ScriptBlockClauseToInvoke clauseToInvoke, Boolean createLocalScope, Dictionary`2 functionsToDefine, List`1 variablesToDefine, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Object[] args)
   at System.Management.Automation.ScriptBlock.<>c__DisplayClass57_0.<InvokeWithPipe>b__0()
   at System.Management.Automation.Runspaces.RunspaceBase.RunActionIfNoRunningPipelinesWithThreadCheck(Action action)
   at System.Management.Automation.ScriptBlock.InvokeWithPipe(Boolean useLocalScope, ErrorHandlingBehavior errorHandlingBehavior, Object dollarUnder, Object input, Object scriptThis, Pipe outputPipe, InvocationInfo invocationInfo, Boolean propagateAllExceptionsToTop, List`1 variablesToDefine, Dictionary`2 functionsToDefine, Object[] args)
   at System.Management.Automation.ScriptBlock.InvokeAsDelegateHelper(Object dollarUnder, Object dollarThis, Object[] args)
   at System.EventHandler.Invoke(Object sender, EventArgs e)
   at System.Windows.Forms.Control.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnClick(EventArgs e)
   at System.Windows.Forms.Button.OnMouseUp(MouseEventArgs mevent)
   at System.Windows.Forms.Control.WmMouseUp(Message& m, MouseButtons button, Int32 clicks)
   at System.Windows.Forms.Control.WndProc(Message& m)
   at System.Windows.Forms.ButtonBase.WndProc(Message& m)
   at System.Windows.Forms.Button.WndProc(Message& m)
   at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam)

1 个答案:

答案 0 :(得分:0)

我不确定为什么会出现此错误。可能是因为您显示消息框的方式可以使其显示在其他窗口的后面。多次运行它而没有意识到以前的实例正在等待用户输入,可能会导致错误消息。

也许最好尝试一下:

Add-Type -AssemblyName Microsoft.VisualBasic

$title   = "Are you sure sure?"
$message = "Are you really really really sure you want to disable this user?`r`n`r`n$($labelNameValue.Text)"
$buttons = "YesNo"     # choose from "OKOnly", "OKCancel", "AbortRetryIgnore", "YesNoCancel", "YesNo", "RetryCancel"
$icon    = "Question"  # choose from "Critical", "Question", "Exclamation", "Information"
if ([Microsoft.VisualBasic.Interaction]::MsgBox($message, "$buttons,SystemModal,$icon", $title) -eq 'No') {
    return   # exit the function
}

代替

if ([System.Windows.Forms.MessageBox]::Show("Are you really really really sure you want to disable this user?`n`n" + $labelNameValue.Text, "Are you sure sure?", "YesNo", "Warning") -eq "No") {
    Break
}

我对解决用户删除问题的所有方式有些困惑。我可以看到$textboxSearch.Text$labelUsernameValue.Text$labelNameValue.Text$userObject混杂在一起在那个功能。也许认为这需要更长的时间?

由于带有SystemModal标志,该消息框将显示在顶部,因此不再有隐藏的警报框的可能性。

P.S. break不退出功能。它通常用于循环和switch语句中。如果在这种情况下使用,break将“跳出”循环或switch语句。请改用return

希望有帮助