Search Wiki:

DoNotCallCertainHostMethods


TypeName DoNotCallCertainHostMethods
CheckId PS1104
Error-Level Error
Category Microsoft.PowerShell
Certainty 80%
BreakingChangeNon-breaking
Cause A cmdlet calls the WriteErrorLine, WriteWarningLine, WriteVerboseLine, or WriteDebugLine methods of the PSHostUserInterface class directly. Instead, the cmdlet should call a related WriteError, ThrowTerminatingError, WriteWarning, WriteVerbose, or WriteDebug method of the Cmdlet class.
Rule descriptionCmdlets that call certain host methods do not work properly in Windows PowerShell streams. For example, if a cmdlet calls the PSHostUserInterface.WriteWarning method, the warning is written only to the host. The warning does not appear in a warning stream where it can be accessed even when there is no host, and the warning does not have a value for the InvocationInfo property of the warning object.
How to fix violations Replace all calls to the WriteErrorLine, WriteWarningLine, WriteVerboseLine, or WriteDebugLine methods of PSHostUserInterface class with calls to the WriteError, WriteWarning, WriteVerbose, or WriteDebug methods of the Cmdlet class.
When to exclude warnings Do not suppress warnings from this rule.
Related rules DoNotUseConsoleApi
See also FxCop Rules for Windows PowerShell

Examples


// Example of a cmdlet that violates this rule.
[Cmdlet("Set", "Password")]
public class SetPasswordCommand : PSCmdlet
{
    protected override void BeginProcessing()
    {
        // ...
        this.Host.UI.WriteVerboseLine(
                "The password has been set.");
    }
}

// Example of a functionally equivalent cmdlet that follows this rule.
[Cmdlet("Set", "Password")]
public class SetPasswordCommand : PSCmdlet
{
    protected override void BeginProcessing()
    {
        // ...
 
        this.WriteVerbose("The password has been set.");
    }
}

Back to FxCop Rules for Windows PowerShell.
Last edited Mar 26 2010 at 4:01 PM  by JuneB, version 1
Updating...
Page view tracker