다음을 통해 공유


Cmdlet.ShouldContinue Method

Definition

Overloads

ShouldContinue(String, String)

Confirm an operation or grouping of operations with the user. This differs from ShouldProcess in that it is not affected by preference settings or command-line parameters, it always does the query. This variant only offers Yes/No, not YesToAll/NoToAll.

ShouldContinue(String, String, Boolean, Boolean)

Confirm an operation or grouping of operations with the user. This differs from ShouldProcess in that it is not affected by preference settings or command-line parameters, it always does the query. This variant offers Yes, No, YesToAll and NoToAll.

ShouldContinue(String, String, Boolean, Boolean, Boolean)

Confirm an operation or grouping of operations with the user. This differs from ShouldProcess in that it is not affected by preference settings or command-line parameters, it always does the query. This variant offers Yes, No, YesToAll and NoToAll.

ShouldContinue(String, String)

Confirm an operation or grouping of operations with the user. This differs from ShouldProcess in that it is not affected by preference settings or command-line parameters, it always does the query. This variant only offers Yes/No, not YesToAll/NoToAll.

public:
 bool ShouldContinue(System::String ^ query, System::String ^ caption);
public:
 bool ShouldContinue(Platform::String ^ query, Platform::String ^ caption);
bool ShouldContinue(std::wstring const & query, std::wstring const & caption);
public bool ShouldContinue (string query, string caption);
member this.ShouldContinue : string * string -> bool
Public Function ShouldContinue (query As String, caption As String) As Boolean

Parameters

query
String

Textual query of whether the action should be performed, usually in the form of a question.

caption
String

Caption of the window which may be displayed when the user is prompted whether or not to perform the action. It may be displayed by some hosts, but not all.

Returns

If ShouldContinue returns true, the operation should be performed. If ShouldContinue returns false, the operation should not be performed, and the Cmdlet should move on to the next target resource.

Exceptions

The pipeline has already been terminated, or was terminated during the execution of this method. The Cmdlet should generally just allow PipelineStoppedException to percolate up to the caller of ProcessRecord etc.

Not permitted at this time or from this thread. ShouldContinue may only be called during a call to this Cmdlet's implementation of ProcessRecord, BeginProcessing or EndProcessing, and only from that thread.

Examples

namespace Microsoft.Samples.Cmdlet
{
    [Cmdlet(VerbsCommon.Remove,"myobjecttype4")]
    public class RemoveMyObjectType4 : Cmdlet
    {
        [Parameter( Mandatory = true )]
        public string Filename
        {
            get { return filename; }
            set { filename = value; }
        }
        private string filename;

        [Parameter]
        public SwitchParameter Force
        {
            get { return force; }
            set { force = value; }
        }
        private bool force;

        public override void ProcessRecord()
        {
            if (ShouldProcess(
                string.Format($"Deleting file {filename}"),
                string.Format($"Are you sure you want to delete file {filename}"),
                "Delete file"))
            {
                if (IsReadOnly(filename))
                {
                    if (!Force && !ShouldContinue(
                            string.Format($"File {filename} is read-only.  Are you sure you want to delete read-only file {filename}?"),
                            "Delete file"))
                            )
                    {
                        return;
                    }
                }
                // delete the object
            }
        }
    }
}

Remarks

Cmdlets using ShouldContinue should also offer a "bool Force" parameter which bypasses the calls to ShouldContinue and ShouldProcess. If this is not done, it will be difficult to use the Cmdlet from scripts and non-interactive hosts.

Cmdlets using ShouldContinue must still verify operations which will make changes using ShouldProcess. This will assure that settings such as -WhatIf work properly. You may call ShouldContinue either before or after ShouldProcess.

ShouldContinue may only be called during a call to this Cmdlet's implementation of ProcessRecord, BeginProcessing or EndProcessing, and only from that thread.

Cmdlets may have different "classes" of confirmations. For example, "del" confirms whether files in a particular directory should be deleted, whether read-only files should be deleted, etc. Cmdlets can use ShouldContinue to store YesToAll/NoToAll members for each such "class" to keep track of whether the user has confirmed "delete all read-only files" etc. ShouldProcess offers YesToAll/NoToAll automatically, but answering YesToAll or NoToAll applies to all subsequent calls to ShouldProcess for the Cmdlet instance.

See also

Applies to

ShouldContinue(String, String, Boolean, Boolean)

Confirm an operation or grouping of operations with the user. This differs from ShouldProcess in that it is not affected by preference settings or command-line parameters, it always does the query. This variant offers Yes, No, YesToAll and NoToAll.

public:
 bool ShouldContinue(System::String ^ query, System::String ^ caption, bool % yesToAll, bool % noToAll);
bool ShouldContinue(std::wstring const & query, std::wstring const & caption, bool & yesToAll, bool & noToAll);
public bool ShouldContinue (string query, string caption, ref bool yesToAll, ref bool noToAll);
member this.ShouldContinue : string * string * bool * bool -> bool
Public Function ShouldContinue (query As String, caption As String, ByRef yesToAll As Boolean, ByRef noToAll As Boolean) As Boolean

Parameters

query
String

Textual query of whether the action should be performed, usually in the form of a question.

caption
String

Caption of the window which may be displayed when the user is prompted whether or not to perform the action. It may be displayed by some hosts, but not all.

yesToAll
Boolean

true iff user selects YesToAll. If this is already true, ShouldContinue will bypass the prompt and return true.

noToAll
Boolean

true iff user selects NoToAll. If this is already true, ShouldContinue will bypass the prompt and return false.

Returns

If ShouldContinue returns true, the operation should be performed. If ShouldContinue returns false, the operation should not be performed, and the Cmdlet should move on to the next target resource.

Exceptions

The pipeline has already been terminated, or was terminated during the execution of this method. The Cmdlet should generally just allow PipelineStoppedException to percolate up to the caller of ProcessRecord etc.

Not permitted at this time or from this thread. ShouldContinue may only be called during a call to this Cmdlet's implementation of ProcessRecord, BeginProcessing or EndProcessing, and only from that thread.

Examples

namespace Microsoft.Samples.Cmdlet
{
    [Cmdlet(VerbsCommon.Remove,"myobjecttype4")]
    public class RemoveMyObjectType5 : Cmdlet
    {
        [Parameter( Mandatory = true )]
        public string Filename
        {
            get { return filename; }
            set { filename = value; }
        }
        private string filename;

        [Parameter]
        public SwitchParameter Force
        {
            get { return force; }
            set { force = value; }
        }
        private bool force;

        private bool yesToAll;
        private bool noToAll;

        public override void ProcessRecord()
        {
            if (ShouldProcess(
                string.Format($"Deleting file {filename}"),
                string.Format($"Are you sure you want to delete file {filename}"),
                "Delete file"))
            {
                if (IsReadOnly(filename))
                {
                    if (!Force && !ShouldContinue(
                            string.Format($"File {filename} is read-only.  Are you sure you want to delete read-only file {filename}?"),
                            "Delete file"),
                            ref yesToAll,
                            ref noToAll
                            )
                    {
                        return;
                    }
                }
                // delete the object
            }
        }
    }
}

Remarks

Cmdlets using ShouldContinue should also offer a "bool Force" parameter which bypasses the calls to ShouldContinue and ShouldProcess. If this is not done, it will be difficult to use the Cmdlet from scripts and non-interactive hosts.

Cmdlets using ShouldContinue must still verify operations which will make changes using ShouldProcess. This will assure that settings such as -WhatIf work properly. You may call ShouldContinue either before or after ShouldProcess.

ShouldContinue may only be called during a call to this Cmdlet's implementation of ProcessRecord, BeginProcessing or EndProcessing, and only from that thread.

Cmdlets may have different "classes" of confirmations. For example, "del" confirms whether files in a particular directory should be deleted, whether read-only files should be deleted, etc. Cmdlets can use ShouldContinue to store YesToAll/NoToAll members for each such "class" to keep track of whether the user has confirmed "delete all read-only files" etc. ShouldProcess offers YesToAll/NoToAll automatically, but answering YesToAll or NoToAll applies to all subsequent calls to ShouldProcess for the Cmdlet instance.

See also

Applies to

ShouldContinue(String, String, Boolean, Boolean, Boolean)

Confirm an operation or grouping of operations with the user. This differs from ShouldProcess in that it is not affected by preference settings or command-line parameters, it always does the query. This variant offers Yes, No, YesToAll and NoToAll.

public:
 bool ShouldContinue(System::String ^ query, System::String ^ caption, bool hasSecurityImpact, bool % yesToAll, bool % noToAll);
bool ShouldContinue(std::wstring const & query, std::wstring const & caption, bool hasSecurityImpact, bool & yesToAll, bool & noToAll);
public bool ShouldContinue (string query, string caption, bool hasSecurityImpact, ref bool yesToAll, ref bool noToAll);
member this.ShouldContinue : string * string * bool * bool * bool -> bool
Public Function ShouldContinue (query As String, caption As String, hasSecurityImpact As Boolean, ByRef yesToAll As Boolean, ByRef noToAll As Boolean) As Boolean

Parameters

query
String

Textual query of whether the action should be performed, usually in the form of a question.

caption
String

Caption of the window which may be displayed when the user is prompted whether or not to perform the action. It may be displayed by some hosts, but not all.

hasSecurityImpact
Boolean

true if the operation being confirmed has a security impact. If specified, the default option selected in the selection menu is 'No'.

yesToAll
Boolean

true iff user selects YesToAll. If this is already true, ShouldContinue will bypass the prompt and return true.

noToAll
Boolean

true iff user selects NoToAll. If this is already true, ShouldContinue will bypass the prompt and return false.

Returns

If ShouldContinue returns true, the operation should be performed. If ShouldContinue returns false, the operation should not be performed, and the Cmdlet should move on to the next target resource.

Exceptions

The pipeline has already been terminated, or was terminated during the execution of this method. The Cmdlet should generally just allow PipelineStoppedException to percolate up to the caller of ProcessRecord etc.

Not permitted at this time or from this thread. ShouldContinue may only be called during a call to this Cmdlet's implementation of ProcessRecord, BeginProcessing or EndProcessing, and only from that thread.

Examples

namespace Microsoft.Samples.Cmdlet
{
    [Cmdlet(VerbsCommon.Remove,"myobjecttype4")]
    public class RemoveMyObjectType5 : Cmdlet
    {
        [Parameter( Mandatory = true )]
        public string Filename
        {
            get { return filename; }
            set { filename = value; }
        }
        private string filename;

        [Parameter]
        public SwitchParameter Force
        {
            get { return force; }
            set { force = value; }
        }
        private bool force;

        private bool yesToAll;
        private bool noToAll;

        public override void ProcessRecord()
        {
            if (ShouldProcess(
                string.Format($"Deleting file {filename}"),
                string.Format($"Are you sure you want to delete file {filename}"),
                "Delete file"))
            {
                if (IsReadOnly(filename))
                {
                    if (!Force && !ShouldContinue(
                            string.Format($"File {filename} is read-only.  Are you sure you want to delete read-only file {filename}?"),
                            "Delete file"),
                            ref yesToAll,
                            ref noToAll
                            )
                    {
                        return;
                    }
                }
                // delete the object
            }
        }
    }
}

Remarks

Cmdlets using ShouldContinue should also offer a "bool Force" parameter which bypasses the calls to ShouldContinue and ShouldProcess. If this is not done, it will be difficult to use the Cmdlet from scripts and non-interactive hosts.

Cmdlets using ShouldContinue must still verify operations which will make changes using ShouldProcess. This will assure that settings such as -WhatIf work properly. You may call ShouldContinue either before or after ShouldProcess.

ShouldContinue may only be called during a call to this Cmdlet's implementation of ProcessRecord, BeginProcessing or EndProcessing, and only from that thread.

Cmdlets may have different "classes" of confirmations. For example, "del" confirms whether files in a particular directory should be deleted, whether read-only files should be deleted, etc. Cmdlets can use ShouldContinue to store YesToAll/NoToAll members for each such "class" to keep track of whether the user has confirmed "delete all read-only files" etc. ShouldProcess offers YesToAll/NoToAll automatically, but answering YesToAll or NoToAll applies to all subsequent calls to ShouldProcess for the Cmdlet instance.

See also

Applies to