Windows desktop applications
Collapse the table of content
Expand the table of content
Information
The topic you requested is included in another documentation set. For convenience, it's displayed below. Choose Switch to see the topic in its original location.

Cmdlet::ShouldContinue Method (String^, String^)

 

Updated: April 27, 2016

Applies To: Windows PowerShell

Requests confirmation of an operation from the user by sending a second query to the user.

Namespace:   System.Management.Automation
Assembly:  System.Management.Automation (in System.Management.Automation.dll)

public:
bool ShouldContinue(
	String^ query,
	String^ caption
)

Parameters

query
Type: System::String^

The query that confirms whether the cmdlet should continue. For more information, see the following code examples.

caption
Type: System::String^

The caption that is displayed above the query. This caption can be displayed in a window that might be displayed by some, but not all, hosts. For more information, see the following code examples.

Return Value

Type: System::Boolean

A value of true indicates that the cmdlet should perform the operation. A value of false indicates that the operation should not be performed and the cmdlet should move to the next resource.

Exception Condition
PipelineStoppedException

Thrown when the pipeline is stopped. The pipeline was terminated either before the call was made or while the call was in progress. By default, the cmdlet should allow the caller of the processing record method to catch this exception.

Also, if the pipeline was terminated because of ActionPreference.Stop or ActionPreference.Inquire, the command failure will ultimately be ActionPreferenceStopException.

InvalidOperationException

Thrown when the call cannot be completed at this time or cannot be completed from this thread. For more information, see the following Remarks section.

This method is typically called after a call to ShouldProcess to provide additional feedback from the user. For more information about confirmation requests, see Requesting Confirmation.

A call to ShouldContinue is not affected by preference settings or by command-line parameters; the query is always displayed to the user.

Cmdlets calling ShouldContinue should also implement a Force parameter that allows the cmdlet to bypass these calls. If this is not done, it will be difficult to use the cmdlet from scripts and noninteractive hosts.

This method can be called only from within the cmdlet implementation of the BeginProcessing, ProcessRecord, and EndProcessing methods and only from that thread. If this call is made from outside these implementations or from another thread, an InvalidOperationException exception is thrown.

For more information about cmdlets, see Windows PowerShell Cmdlets.

In the following code example, the call to ShouldContinue does not specify a caption or query so the default text is displayed.

protected override void ProcessRecord()
{
  if (ShouldProcess("ShouldProcess target"))
  {
    if (Force || ShouldContinue("ShouldContinue query",
                                "ShouldContinue caption"))
    {
      // Add code that performs the operation.
    }
  }
}

The first message is displayed as a result of the call to the Cmdlet::ShouldProcess method. The second message is displayed as a result of the call to the ShouldContinue method when the user answers Yes or Yes to All to the first message. Notice that in this case the default confirmation message is displayed.

Confirm
Are you sure you want to perform this action?
Performing operation "Test-RequestConfirmationTemplate1" on Target "Resource".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): y

Confirm
Continue with this operation?
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"):

In the following code example, the call to the ShouldContinue method specifies a caption and query.

protected override void ProcessRecord()
{
  if (ShouldProcess("ShouldProcess target"))
  {
    if (Force || ShouldContinue("ShouldContinue query",
                                "ShouldContinue caption"))
    {
      // Add code that performs the operation.
    }
  }
}

The first message is displayed as a result of the call to the Cmdlet::ShouldProcess method. The second message is displayed as a result of the call to the ShouldContinue method. Notice that in this case the cmdlet has full control over what is displayed in the second confirmation message.

Confirm
Are you sure you want to perform this action?
Performing operation "Test-RequestConfirmationTemplate1" on Target "Resource".
[Y] Yes  [A] Yes to All  [N] No  [L] No to All  [S] Suspend  [?] Help (default is "Y"): y

ShouldContinue caption
ShouldContinue query
[Y] Yes  [N] No  [S] Suspend  [?] Help (default is "Y"):
Return to top

Community Additions

Show:
© 2016 Microsoft