/// <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); } } }
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]); } } }