2017-02-27 15 views
1

我在Powershell中創建了一個腳本:當我導入CSV文件時,我想檢查用戶是否已經存在。這是腳本:System.Management.Automation.ExitException:Powershell Studio中的系統錯誤

$wshell = New-Object -ComObject Wscript.Shell 

$adusers = Get-ADUser -SearchBase "DC=KOJ,DC=NL" -Filter * | 
    Select-Object -ExpandProperty SamAccountName 

$UserList = Import-Csv -Path $txt_csv.Text -Delimiter ";" | 
    Select-Object -ExpandProperty UPN 

$exist = Compare-Object $adusers $Userlist -IncludeEqual -ExcludeDifferent | 
    Select-Object -ExpandProperty InputObject 

$flag = 0 

foreach ($user in $exist) {   
    $flag = 1   
    $a = $wshell.Popup("$user exist make unique!", 0, "stop", 0x1)   
    Write-Host $user "already exists"   
} 

if ($flag -eq 1) {   
    $form.Close() 
    exit 
} 

當用戶已經存在,我得到以下錯誤:

************** Exception Text ************** 
System.Management.Automation.ExitException: 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, Hashtable errorResults, Boolean enumerate) 
    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__DisplayClassa.<InvokeWithPipe>b__8() 
    at System.Management.Automation.Runspaces.RunspaceBase.RunActionIfNoRunningPipelinesWithThreadCheck(Action action) 
    at System.Management.Automation.ScriptBlock.InvokeWithPipe(Boolean useLocalScope, 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.InvokeAsDelegateHelper(Object dollarUnder, Object dollarThis, Object[] args) 
    at lambda_method(Closure , Object , EventArgs) 
    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.Control.ControlNativeWindow.OnMessage(Message& m) 
    at System.Windows.Forms.Control.ControlNativeWindow.WndProc(Message& m) 
    at System.Windows.Forms.NativeWindow.Callback(IntPtr hWnd, Int32 msg, IntPtr wparam, IntPtr lparam) 

(FYI我已經讀this SO post

我不知道我的條件必須在if聲明中使用。

我也試過[environment]::exit(0),但後來我的PowerShell環境關閉,我不想這樣做。

你們可以幫我解決問題嗎?

親切的問候。

+0

嗨,'$ form'是什麼?你爲什麼認爲你需要在這裏退出? – sodawillow

+0

$ form是主要的gui界面。當用戶存在時,我想停止腳本並關閉GUI窗體。 – Fearcoder

回答

0

我用return,一切正常!

+0

出於好奇,是不是'$ form.Close()'足以關閉表單? – sodawillow

+0

只有GUI將關閉,但腳本仍在運行。 **返回**腳本停止並且GUI仍然打開。 – Fearcoder