本文整理汇总了C#中System.Management.Automation.PSCmdlet.ShouldProcess方法的典型用法代码示例。如果您正苦于以下问题:C# PSCmdlet.ShouldProcess方法的具体用法?C# PSCmdlet.ShouldProcess怎么用?C# PSCmdlet.ShouldProcess使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类System.Management.Automation.PSCmdlet
的用法示例。
在下文中一共展示了PSCmdlet.ShouldProcess方法的2个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的C#代码示例。
示例1: RestartWinRMService
/// <summary>
/// Run script to restart the WinRM service. The script will write
/// output and error into the cmdlets streams.
/// </summary>
/// <param name="cmdlet">
/// Cmdlet's context in which the restart-service script is run.
/// </param>
/// <param name="isErrorReported">
/// if true, then this method is a no-op.
/// </param>
/// <param name="force">
/// if true, then the user will not be prompted.
/// </param>
/// <param name="noServiceRestart">
/// if true, we dont attempt to restart winrm service ie. this will be a no-op.
/// </param>
internal static void RestartWinRMService(PSCmdlet cmdlet, bool isErrorReported, bool force, bool noServiceRestart)
{
// restart the service only if there is no error running WSMan config command
if (!(isErrorReported || noServiceRestart))
{
// restart-service winrm to make the changes effective.
string restartServiceAction = RemotingErrorIdStrings.RestartWSManServiceAction;
string restartServiceTarget = StringUtil.Format(RemotingErrorIdStrings.RestartWSManServiceTarget, "WinRM");
if (force || cmdlet.ShouldProcess(restartServiceTarget, restartServiceAction))
{
cmdlet.WriteVerbose(StringUtil.Format(RemotingErrorIdStrings.RestartWSManServiceMessageV));
ScriptBlock restartServiceScript = cmdlet.InvokeCommand.NewScriptBlock(restartWSManFormat);
var emptyArray = Utils.EmptyArray<object>();
restartServiceScript.InvokeUsingCmdlet(
contextCmdlet: cmdlet,
useLocalScope: true,
errorHandlingBehavior: ScriptBlock.ErrorHandlingBehavior.WriteToCurrentErrorPipe,
dollarUnder: AutomationNull.Value,
input: emptyArray,
scriptThis: AutomationNull.Value,
args: emptyArray);
}
}
}
示例2: RestartWinRMService
internal static void RestartWinRMService(PSCmdlet cmdlet, bool isErrorReported, bool force, bool noServiceRestart)
{
if (!isErrorReported && !noServiceRestart)
{
string restartWSManServiceAction = RemotingErrorIdStrings.RestartWSManServiceAction;
string target = StringUtil.Format(RemotingErrorIdStrings.RestartWSManServiceTarget, "WinRM");
if (force || cmdlet.ShouldProcess(target, restartWSManServiceAction))
{
cmdlet.WriteVerbose(StringUtil.Format(RemotingErrorIdStrings.RestartWSManServiceMessageV, new object[0]));
cmdlet.InvokeCommand.NewScriptBlock("restart-service winrm -force -confirm:$false").InvokeUsingCmdlet(cmdlet, true, ScriptBlock.ErrorHandlingBehavior.WriteToCurrentErrorPipe, AutomationNull.Value, new object[0], AutomationNull.Value, new object[0]);
}
}
}