本文整理匯總了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]);
}
}
}