header1   header
header
header Register : : Login header
header
connector   connector
menuleft menuright
submenu   submenu
left
How to exit from cmdlet
Last Post 18 May 2010 06:56 AM by George Howarth. 3 Replies.
Printer Friendly
  •  
  •  
  •  
  •  
  •  
Sort:
PrevPrev NextNext
You are not authorized to post a reply.
Author Messages
closerUser is Offline
New Member
New Member
Posts:5
Avatar

--
18 May 2010 05:19 AM
    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
    George HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    18 May 2010 05:35 AM
    I haven't had any experience with Cmdlet development, but I can take a couple of guesses:

    - There is a StopProcessing() method (http://msdn.microsoft.com/en-us/lib....85).aspx) and a Stopping property (http://msdn.microsoft.com/en-us/library/system.management.automation.cmdlet.stopping(v=VS.85).aspx) that you can maybe use.

    - You can throw a FileNotFoundException which will stop the execution of your code.
    closerUser is Offline
    New Member
    New Member
    Posts:5
    Avatar

    --
    18 May 2010 05:58 AM
    The StopProcessing is empty virtual method (see http://img683.imageshack.us/img683/2448/stopprocessing.png). The Stopping is read-only property. Therefore this way don't work.

    Also I don't want to throw exception because I don't want to see exception message in PowerShell window when user abort cmdlet execution.

    But thanks for your answer.
    George HowarthUser is Offline
    Basic Member
    Basic Member
    Posts:360
    Avatar

    --
    18 May 2010 06:56 AM
    I guess you could set a boolean flag and return from the method, then evaluate the boolean flag in the ProcessRecord() method and do the neccessary processing depending on the value.
    You are not authorized to post a reply.


    Active Forums 4.3
    right
    footer   footer
    footer Sponsored by Quest Software • SAPIEN Technologies • Compellent • Microsoft Windows Server 2008 R2 footer
    footer   footer