How to exit from a function in c# without closing PowerShell
I am using Powershell as my 开发者_高级运维Console. I created a PSSnapin and added it to Powershell. Now i want that for some exception, it should exit there, but since i have called it from some function, it returns back there which i don't want. I want it to exit at that instant only without closing the powershell Console. I tried Environment.Exit(0) but it is closing the console application. Is there any other way to exit?
Snapins run in the PowerShell process and as such, PowerShell attempts to protect itself by catching exceptions that a snapin throws. The last thing you want to do is call Environment.Exit()
because that will kill the PowerShell process. You can either let the exception escape from your cmdlet implementation (or throw if necessary) but it would be better to use the ThrowTerminatingError()
method on your PSCmdlet instance to signal a terminating condition for your cmdlet.
精彩评论