How to exit from cmdlet

X

Xaba Xaba

Hello, All

I have made specific validation of input parameters in my cmdlet:

protected override void BeginProcessing()
{
if (File.Exists(Output))
{
var message = string.Format("File '{0}' already exists. Do you want to
remove it?", Output);
var result = this.ShouldContinue(message, "File exists");
if (result)
{
File.Delete(Output);
}
else
{
//TODO: Stop cmdlet execution and exit
}
}
// Another validation code

}

protected override void ProcessRecord()
....



And I want to stop cmdlet execution (in place marked as TODO) and abort
execution of ProcessRecord method. How can I do it?

Maybe Cmdlet SDK have special method for abort cmdlet execution...

Thanks
 

My Computer

C

Chris Dent

Why do you need to terminate the CmdLet at that point? If it's to
report a problem then ThrowTerminatingError
(System.Management.Automation.CmdLet / PSCmdLet).

If it's natural termination then you should re-work your conditions in
such a way that the CmdLet completes gracefully.

Chris

Xaba Xaba wrote:

> Hello, All
>
> I have made specific validation of input parameters in my cmdlet:
>
> protected override void BeginProcessing()
> {
> if (File.Exists(Output))
> {
> var message = string.Format("File '{0}' already exists. Do you want to
> remove it?", Output);
> var result = this.ShouldContinue(message, "File exists");
> if (result)
> {
> File.Delete(Output);
> }
> else
> {
> //TODO: Stop cmdlet execution and exit
> }
> }
> // Another validation code
>
> }
>
> protected override void ProcessRecord()
> ...
>
>
>
> And I want to stop cmdlet execution (in place marked as TODO) and abort
> execution of ProcessRecord method. How can I do it?
>
> Maybe Cmdlet SDK have special method for abort cmdlet execution...
>
> Thanks
>
 

My Computer

Top