From a9b04c220f8a17e02fd7f6efe42266e47eafd22d Mon Sep 17 00:00:00 2001 From: Nate Pink Date: Thu, 19 Nov 2026 20:29:54 +0000 Subject: [PATCH 1/3] Add maintenance mode cmdlets to the PowerShell module Adds Enable- and Disable-ServiceControlMaintenanceMode and the audit equivalents, with sc-maintenance-on/off and audit-maintenance-on/off aliases. Each cmdlet stops the service, updates the maintenance mode setting, and starts the service. --- .../MaintenanceModeCmdletBase.cs | 54 +++ .../MaintenanceMode/MaintenanceModeCmdlets.cs | 43 ++ .../Particular.ServiceControl.Management.psm1 | 4 + ...Control.Management.PowerShell.dll-help.xml | 368 ++++++++++++++++++ 4 files changed, 469 insertions(+) create mode 100644 src/ServiceControl.Management.PowerShell/Cmdlets/MaintenanceMode/MaintenanceModeCmdletBase.cs create mode 100644 src/ServiceControl.Management.PowerShell/Cmdlets/MaintenanceMode/MaintenanceModeCmdlets.cs diff --git a/src/ServiceControl.Management.PowerShell/Cmdlets/MaintenanceMode/MaintenanceModeCmdletBase.cs b/src/ServiceControl.Management.PowerShell/Cmdlets/MaintenanceMode/MaintenanceModeCmdletBase.cs new file mode 100644 index 0000000000..4f5fc9c9f6 --- /dev/null +++ b/src/ServiceControl.Management.PowerShell/Cmdlets/MaintenanceMode/MaintenanceModeCmdletBase.cs @@ -0,0 +1,54 @@ +namespace ServiceControl.Management.PowerShell +{ + using System; + using System.Management.Automation; + using ServiceControlInstaller.Engine.Instances; + + public abstract class MaintenanceModeCmdletBase : PSCmdlet + { + [ValidateNotNullOrEmpty] + [Parameter(Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0, HelpMessage = "Specify the instance name")] + public string[] Name { get; set; } + + protected abstract bool Enable { get; } + + protected abstract ServiceControlBaseService FindInstance(string name); + + protected override void BeginProcessing() + { + Account.TestIfAdmin(); + } + + protected override void ProcessRecord() + { + foreach (var name in Name) + { + var instance = FindInstance(name); + if (instance == null) + { + WriteWarning($"No action taken. An instance called {name} was not found"); + continue; + } + + if (!instance.TryStopService()) + { + ThrowTerminatingError(new ErrorRecord(new Exception($"Failed to stop {instance.Name}"), "StopFailure", ErrorCategory.InvalidResult, null)); + } + + if (Enable) + { + instance.EnableMaintenanceMode(); + } + else + { + instance.DisableMaintenanceMode(); + } + + if (!instance.TryStartService()) + { + ThrowTerminatingError(new ErrorRecord(new Exception($"Failed to start {instance.Name}"), "StartFailure", ErrorCategory.InvalidResult, null)); + } + } + } + } +} diff --git a/src/ServiceControl.Management.PowerShell/Cmdlets/MaintenanceMode/MaintenanceModeCmdlets.cs b/src/ServiceControl.Management.PowerShell/Cmdlets/MaintenanceMode/MaintenanceModeCmdlets.cs new file mode 100644 index 0000000000..220eae237d --- /dev/null +++ b/src/ServiceControl.Management.PowerShell/Cmdlets/MaintenanceMode/MaintenanceModeCmdlets.cs @@ -0,0 +1,43 @@ +namespace ServiceControl.Management.PowerShell +{ + using System; + using System.Linq; + using System.Management.Automation; + using ServiceControlInstaller.Engine.Instances; + + [Cmdlet(VerbsLifecycle.Enable, "ServiceControlMaintenanceMode")] + public class EnableServiceControlMaintenanceMode : MaintenanceModeCmdletBase + { + protected override bool Enable => true; + + protected override ServiceControlBaseService FindInstance(string name) => + InstanceFinder.ServiceControlInstances().FirstOrDefault(i => i.Name.Equals(name, StringComparison.OrdinalIgnoreCase)); + } + + [Cmdlet(VerbsLifecycle.Disable, "ServiceControlMaintenanceMode")] + public class DisableServiceControlMaintenanceMode : MaintenanceModeCmdletBase + { + protected override bool Enable => false; + + protected override ServiceControlBaseService FindInstance(string name) => + InstanceFinder.ServiceControlInstances().FirstOrDefault(i => i.Name.Equals(name, StringComparison.OrdinalIgnoreCase)); + } + + [Cmdlet(VerbsLifecycle.Enable, "ServiceControlAuditMaintenanceMode")] + public class EnableServiceControlAuditMaintenanceMode : MaintenanceModeCmdletBase + { + protected override bool Enable => true; + + protected override ServiceControlBaseService FindInstance(string name) => + InstanceFinder.ServiceControlAuditInstances().FirstOrDefault(i => i.Name.Equals(name, StringComparison.OrdinalIgnoreCase)); + } + + [Cmdlet(VerbsLifecycle.Disable, "ServiceControlAuditMaintenanceMode")] + public class DisableServiceControlAuditMaintenanceMode : MaintenanceModeCmdletBase + { + protected override bool Enable => false; + + protected override ServiceControlBaseService FindInstance(string name) => + InstanceFinder.ServiceControlAuditInstances().FirstOrDefault(i => i.Name.Equals(name, StringComparison.OrdinalIgnoreCase)); + } +} diff --git a/src/ServiceControl.Management.PowerShell/Particular.ServiceControl.Management.psm1 b/src/ServiceControl.Management.PowerShell/Particular.ServiceControl.Management.psm1 index 5c06c1b9e7..ad74167141 100644 --- a/src/ServiceControl.Management.PowerShell/Particular.ServiceControl.Management.psm1 +++ b/src/ServiceControl.Management.PowerShell/Particular.ServiceControl.Management.psm1 @@ -41,6 +41,10 @@ New-Alias -Value Get-ServiceControlManagementCommands -Name sc-help New-Alias -Value Get-ServiceControlRemotes -Name sc-remotes New-Alias -Value Add-ServiceControlRemote -Name sc-addremote New-Alias -Value Remove-ServiceControlRemote -Name sc-deleteremote +New-Alias -Value Enable-ServiceControlMaintenanceMode -Name sc-maintenance-on +New-Alias -Value Disable-ServiceControlMaintenanceMode -Name sc-maintenance-off +New-Alias -Value Enable-ServiceControlAuditMaintenanceMode -Name audit-maintenance-on +New-Alias -Value Disable-ServiceControlAuditMaintenanceMode -Name audit-maintenance-off Export-ModuleMember * -Alias * diff --git a/src/ServiceControl.Management.PowerShell/ServiceControl.Management.PowerShell.dll-help.xml b/src/ServiceControl.Management.PowerShell/ServiceControl.Management.PowerShell.dll-help.xml index 42ad697714..e67aa91929 100644 --- a/src/ServiceControl.Management.PowerShell/ServiceControl.Management.PowerShell.dll-help.xml +++ b/src/ServiceControl.Management.PowerShell/ServiceControl.Management.PowerShell.dll-help.xml @@ -190,6 +190,374 @@ + + + + Disable-ServiceControlAuditMaintenanceMode + + Takes a ServiceControl Audit instance out of maintenance mode. + + + + + Disable + ServiceControlAuditMaintenanceMode + + + + Stops the ServiceControl Audit service, removes the maintenance mode setting from its configuration file, and starts the service again. The instance resumes ingesting audit messages. Requires an elevated session. Alias: audit-maintenance-off + + + + Disable-ServiceControlAuditMaintenanceMode + + Name + + Name of the ServiceControl Audit Service. This corresponds to the Windows Service Name for the service not the Display Name. + + String[] + + + + + + Name + + Name of the ServiceControl Audit Service. This corresponds to the Windows Service Name for the service not the Display Name. + + String[] + + String[] + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -------------------------- Example 1 -------------------------- + + PS C:\> + + Disable-ServiceControlAuditMaintenanceMode -Name Particular.ServiceControl.Audit + + Restarts the Particular.ServiceControl.Audit service without maintenance mode. + + + + + + + + + + Disable-ServiceControlMaintenanceMode + + Takes a ServiceControl instance out of maintenance mode. + + + + + Disable + ServiceControlMaintenanceMode + + + + Stops the ServiceControl service, removes the maintenance mode setting from its configuration file, and starts the service again. The instance resumes ingesting error messages. Requires an elevated session. Alias: sc-maintenance-off + + + + Disable-ServiceControlMaintenanceMode + + Name + + Name of the ServiceControl Service. This corresponds to the Windows Service Name for the service not the Display Name. + + String[] + + + + + + Name + + Name of the ServiceControl Service. This corresponds to the Windows Service Name for the service not the Display Name. + + String[] + + String[] + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -------------------------- Example 1 -------------------------- + + PS C:\> + + Disable-ServiceControlMaintenanceMode -Name Particular.ServiceControl + + Restarts the Particular.ServiceControl service without maintenance mode. + + + + + + + + + + Enable-ServiceControlAuditMaintenanceMode + + Puts a ServiceControl Audit instance into maintenance mode. + + + + + Enable + ServiceControlAuditMaintenanceMode + + + + Stops the ServiceControl Audit service, writes the maintenance mode setting to its configuration file, and starts the service again. In maintenance mode the instance does not ingest audit messages and its RavenDB database stays available. Run Disable-ServiceControlAuditMaintenanceMode to clear the setting. Requires an elevated session. Alias: audit-maintenance-on + + + + Enable-ServiceControlAuditMaintenanceMode + + Name + + Name of the ServiceControl Audit Service. This corresponds to the Windows Service Name for the service not the Display Name. + + String[] + + + + + + Name + + Name of the ServiceControl Audit Service. This corresponds to the Windows Service Name for the service not the Display Name. + + String[] + + String[] + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -------------------------- Example 1 -------------------------- + + PS C:\> + + Enable-ServiceControlAuditMaintenanceMode -Name Particular.ServiceControl.Audit + + Restarts the Particular.ServiceControl.Audit service in maintenance mode. + + + + + + + + + + Enable-ServiceControlMaintenanceMode + + Puts a ServiceControl instance into maintenance mode. + + + + + Enable + ServiceControlMaintenanceMode + + + + Stops the ServiceControl service, writes the maintenance mode setting to its configuration file, and starts the service again. In maintenance mode the instance does not ingest error messages and its RavenDB database stays available. Run Disable-ServiceControlMaintenanceMode to clear the setting. Requires an elevated session. Alias: sc-maintenance-on + + + + Enable-ServiceControlMaintenanceMode + + Name + + Name of the ServiceControl Service. This corresponds to the Windows Service Name for the service not the Display Name. + + String[] + + + + + + Name + + Name of the ServiceControl Service. This corresponds to the Windows Service Name for the service not the Display Name. + + String[] + + String[] + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + -------------------------- Example 1 -------------------------- + + PS C:\> + + Enable-ServiceControlMaintenanceMode -Name Particular.ServiceControl + + Restarts the Particular.ServiceControl service in maintenance mode. + + + + + + From eb60d6b7bbd22c1811b2174801ff19704fe0381e Mon Sep 17 00:00:00 2001 From: Nate Pink Date: Fri, 20 Nov 2026 13:26:30 +0000 Subject: [PATCH 2/3] Rename maintenance mode cmdlets to Start and Stop Start- stops the service, enables maintenance mode, and starts the service. Stop- stops the service and clears maintenance mode, leaving the service stopped, as ServiceControl Management does. Aliases become sc-maintenance-start/stop and audit-maintenance-start/stop. --- .../MaintenanceModeCmdletBase.cs | 11 ++-- .../MaintenanceMode/MaintenanceModeCmdlets.cs | 24 ++++----- .../Particular.ServiceControl.Management.psm1 | 8 +-- ...Control.Management.PowerShell.dll-help.xml | 52 +++++++++---------- 4 files changed, 47 insertions(+), 48 deletions(-) diff --git a/src/ServiceControl.Management.PowerShell/Cmdlets/MaintenanceMode/MaintenanceModeCmdletBase.cs b/src/ServiceControl.Management.PowerShell/Cmdlets/MaintenanceMode/MaintenanceModeCmdletBase.cs index 4f5fc9c9f6..8b1428c9ad 100644 --- a/src/ServiceControl.Management.PowerShell/Cmdlets/MaintenanceMode/MaintenanceModeCmdletBase.cs +++ b/src/ServiceControl.Management.PowerShell/Cmdlets/MaintenanceMode/MaintenanceModeCmdletBase.cs @@ -10,7 +10,7 @@ public abstract class MaintenanceModeCmdletBase : PSCmdlet [Parameter(Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0, HelpMessage = "Specify the instance name")] public string[] Name { get; set; } - protected abstract bool Enable { get; } + protected abstract bool StartInMaintenanceMode { get; } protected abstract ServiceControlBaseService FindInstance(string name); @@ -35,15 +35,14 @@ protected override void ProcessRecord() ThrowTerminatingError(new ErrorRecord(new Exception($"Failed to stop {instance.Name}"), "StopFailure", ErrorCategory.InvalidResult, null)); } - if (Enable) - { - instance.EnableMaintenanceMode(); - } - else + if (!StartInMaintenanceMode) { instance.DisableMaintenanceMode(); + continue; } + instance.EnableMaintenanceMode(); + if (!instance.TryStartService()) { ThrowTerminatingError(new ErrorRecord(new Exception($"Failed to start {instance.Name}"), "StartFailure", ErrorCategory.InvalidResult, null)); diff --git a/src/ServiceControl.Management.PowerShell/Cmdlets/MaintenanceMode/MaintenanceModeCmdlets.cs b/src/ServiceControl.Management.PowerShell/Cmdlets/MaintenanceMode/MaintenanceModeCmdlets.cs index 220eae237d..48d41316db 100644 --- a/src/ServiceControl.Management.PowerShell/Cmdlets/MaintenanceMode/MaintenanceModeCmdlets.cs +++ b/src/ServiceControl.Management.PowerShell/Cmdlets/MaintenanceMode/MaintenanceModeCmdlets.cs @@ -5,37 +5,37 @@ namespace ServiceControl.Management.PowerShell using System.Management.Automation; using ServiceControlInstaller.Engine.Instances; - [Cmdlet(VerbsLifecycle.Enable, "ServiceControlMaintenanceMode")] - public class EnableServiceControlMaintenanceMode : MaintenanceModeCmdletBase + [Cmdlet(VerbsLifecycle.Start, "ServiceControlMaintenanceMode")] + public class StartServiceControlMaintenanceMode : MaintenanceModeCmdletBase { - protected override bool Enable => true; + protected override bool StartInMaintenanceMode => true; protected override ServiceControlBaseService FindInstance(string name) => InstanceFinder.ServiceControlInstances().FirstOrDefault(i => i.Name.Equals(name, StringComparison.OrdinalIgnoreCase)); } - [Cmdlet(VerbsLifecycle.Disable, "ServiceControlMaintenanceMode")] - public class DisableServiceControlMaintenanceMode : MaintenanceModeCmdletBase + [Cmdlet(VerbsLifecycle.Stop, "ServiceControlMaintenanceMode")] + public class StopServiceControlMaintenanceMode : MaintenanceModeCmdletBase { - protected override bool Enable => false; + protected override bool StartInMaintenanceMode => false; protected override ServiceControlBaseService FindInstance(string name) => InstanceFinder.ServiceControlInstances().FirstOrDefault(i => i.Name.Equals(name, StringComparison.OrdinalIgnoreCase)); } - [Cmdlet(VerbsLifecycle.Enable, "ServiceControlAuditMaintenanceMode")] - public class EnableServiceControlAuditMaintenanceMode : MaintenanceModeCmdletBase + [Cmdlet(VerbsLifecycle.Start, "ServiceControlAuditMaintenanceMode")] + public class StartServiceControlAuditMaintenanceMode : MaintenanceModeCmdletBase { - protected override bool Enable => true; + protected override bool StartInMaintenanceMode => true; protected override ServiceControlBaseService FindInstance(string name) => InstanceFinder.ServiceControlAuditInstances().FirstOrDefault(i => i.Name.Equals(name, StringComparison.OrdinalIgnoreCase)); } - [Cmdlet(VerbsLifecycle.Disable, "ServiceControlAuditMaintenanceMode")] - public class DisableServiceControlAuditMaintenanceMode : MaintenanceModeCmdletBase + [Cmdlet(VerbsLifecycle.Stop, "ServiceControlAuditMaintenanceMode")] + public class StopServiceControlAuditMaintenanceMode : MaintenanceModeCmdletBase { - protected override bool Enable => false; + protected override bool StartInMaintenanceMode => false; protected override ServiceControlBaseService FindInstance(string name) => InstanceFinder.ServiceControlAuditInstances().FirstOrDefault(i => i.Name.Equals(name, StringComparison.OrdinalIgnoreCase)); diff --git a/src/ServiceControl.Management.PowerShell/Particular.ServiceControl.Management.psm1 b/src/ServiceControl.Management.PowerShell/Particular.ServiceControl.Management.psm1 index ad74167141..fc2b17fee5 100644 --- a/src/ServiceControl.Management.PowerShell/Particular.ServiceControl.Management.psm1 +++ b/src/ServiceControl.Management.PowerShell/Particular.ServiceControl.Management.psm1 @@ -41,10 +41,10 @@ New-Alias -Value Get-ServiceControlManagementCommands -Name sc-help New-Alias -Value Get-ServiceControlRemotes -Name sc-remotes New-Alias -Value Add-ServiceControlRemote -Name sc-addremote New-Alias -Value Remove-ServiceControlRemote -Name sc-deleteremote -New-Alias -Value Enable-ServiceControlMaintenanceMode -Name sc-maintenance-on -New-Alias -Value Disable-ServiceControlMaintenanceMode -Name sc-maintenance-off -New-Alias -Value Enable-ServiceControlAuditMaintenanceMode -Name audit-maintenance-on -New-Alias -Value Disable-ServiceControlAuditMaintenanceMode -Name audit-maintenance-off +New-Alias -Value Start-ServiceControlMaintenanceMode -Name sc-maintenance-start +New-Alias -Value Stop-ServiceControlMaintenanceMode -Name sc-maintenance-stop +New-Alias -Value Start-ServiceControlAuditMaintenanceMode -Name audit-maintenance-start +New-Alias -Value Stop-ServiceControlAuditMaintenanceMode -Name audit-maintenance-stop Export-ModuleMember * -Alias * diff --git a/src/ServiceControl.Management.PowerShell/ServiceControl.Management.PowerShell.dll-help.xml b/src/ServiceControl.Management.PowerShell/ServiceControl.Management.PowerShell.dll-help.xml index e67aa91929..febef66b78 100644 --- a/src/ServiceControl.Management.PowerShell/ServiceControl.Management.PowerShell.dll-help.xml +++ b/src/ServiceControl.Management.PowerShell/ServiceControl.Management.PowerShell.dll-help.xml @@ -193,23 +193,23 @@ - Disable-ServiceControlAuditMaintenanceMode + Stop-ServiceControlAuditMaintenanceMode - Takes a ServiceControl Audit instance out of maintenance mode. + Stops a ServiceControl Audit instance and clears maintenance mode. - Disable + Stop ServiceControlAuditMaintenanceMode - Stops the ServiceControl Audit service, removes the maintenance mode setting from its configuration file, and starts the service again. The instance resumes ingesting audit messages. Requires an elevated session. Alias: audit-maintenance-off + Stops the ServiceControl Audit service and removes the maintenance mode setting from its configuration file. The service stays stopped; start it normally to resume ingesting audit messages. Requires an elevated session. Alias: audit-maintenance-stop - Disable-ServiceControlAuditMaintenanceMode + Stop-ServiceControlAuditMaintenanceMode Name @@ -273,9 +273,9 @@ PS C:\> - Disable-ServiceControlAuditMaintenanceMode -Name Particular.ServiceControl.Audit + Stop-ServiceControlAuditMaintenanceMode -Name Particular.ServiceControl.Audit - Restarts the Particular.ServiceControl.Audit service without maintenance mode. + Stops the Particular.ServiceControl.Audit service and clears maintenance mode. @@ -285,23 +285,23 @@ - Disable-ServiceControlMaintenanceMode + Stop-ServiceControlMaintenanceMode - Takes a ServiceControl instance out of maintenance mode. + Stops a ServiceControl instance and clears maintenance mode. - Disable + Stop ServiceControlMaintenanceMode - Stops the ServiceControl service, removes the maintenance mode setting from its configuration file, and starts the service again. The instance resumes ingesting error messages. Requires an elevated session. Alias: sc-maintenance-off + Stops the ServiceControl service and removes the maintenance mode setting from its configuration file. The service stays stopped; start it normally to resume ingesting error messages. Requires an elevated session. Alias: sc-maintenance-stop - Disable-ServiceControlMaintenanceMode + Stop-ServiceControlMaintenanceMode Name @@ -365,9 +365,9 @@ PS C:\> - Disable-ServiceControlMaintenanceMode -Name Particular.ServiceControl + Stop-ServiceControlMaintenanceMode -Name Particular.ServiceControl - Restarts the Particular.ServiceControl service without maintenance mode. + Stops the Particular.ServiceControl service and clears maintenance mode. @@ -377,23 +377,23 @@ - Enable-ServiceControlAuditMaintenanceMode + Start-ServiceControlAuditMaintenanceMode - Puts a ServiceControl Audit instance into maintenance mode. + Starts a ServiceControl Audit instance in maintenance mode. - Enable + Start ServiceControlAuditMaintenanceMode - Stops the ServiceControl Audit service, writes the maintenance mode setting to its configuration file, and starts the service again. In maintenance mode the instance does not ingest audit messages and its RavenDB database stays available. Run Disable-ServiceControlAuditMaintenanceMode to clear the setting. Requires an elevated session. Alias: audit-maintenance-on + Stops the ServiceControl Audit service, writes the maintenance mode setting to its configuration file, and starts the service again. In maintenance mode the instance does not ingest audit messages and its RavenDB database stays available. Run Stop-ServiceControlAuditMaintenanceMode to clear the setting and stop the service. Requires an elevated session. Alias: audit-maintenance-start - Enable-ServiceControlAuditMaintenanceMode + Start-ServiceControlAuditMaintenanceMode Name @@ -457,7 +457,7 @@ PS C:\> - Enable-ServiceControlAuditMaintenanceMode -Name Particular.ServiceControl.Audit + Start-ServiceControlAuditMaintenanceMode -Name Particular.ServiceControl.Audit Restarts the Particular.ServiceControl.Audit service in maintenance mode. @@ -469,23 +469,23 @@ - Enable-ServiceControlMaintenanceMode + Start-ServiceControlMaintenanceMode - Puts a ServiceControl instance into maintenance mode. + Starts a ServiceControl instance in maintenance mode. - Enable + Start ServiceControlMaintenanceMode - Stops the ServiceControl service, writes the maintenance mode setting to its configuration file, and starts the service again. In maintenance mode the instance does not ingest error messages and its RavenDB database stays available. Run Disable-ServiceControlMaintenanceMode to clear the setting. Requires an elevated session. Alias: sc-maintenance-on + Stops the ServiceControl service, writes the maintenance mode setting to its configuration file, and starts the service again. In maintenance mode the instance does not ingest error messages and its RavenDB database stays available. Run Stop-ServiceControlMaintenanceMode to clear the setting and stop the service. Requires an elevated session. Alias: sc-maintenance-start - Enable-ServiceControlMaintenanceMode + Start-ServiceControlMaintenanceMode Name @@ -549,7 +549,7 @@ PS C:\> - Enable-ServiceControlMaintenanceMode -Name Particular.ServiceControl + Start-ServiceControlMaintenanceMode -Name Particular.ServiceControl Restarts the Particular.ServiceControl service in maintenance mode. From 2ba25bce0f4eb82951f68bdf86afc840c20136e4 Mon Sep 17 00:00:00 2001 From: Nate Pink Date: Fri, 20 Nov 2026 14:05:30 +0000 Subject: [PATCH 3/3] Replace the maintenance mode base class with separate cmdlet classes Each cmdlet now lives in the ServiceControlInstances or AuditInstances folder and carries its own parameter, admin check, and instance lookup. --- ...tartServiceControlAuditMaintenanceMode.cs} | 18 +++----- .../StopServiceControlAuditMaintenanceMode.cs | 40 +++++++++++++++++ .../MaintenanceMode/MaintenanceModeCmdlets.cs | 43 ------------------ .../StartServiceControlMaintenanceMode.cs | 45 +++++++++++++++++++ .../StopServiceControlMaintenanceMode.cs | 40 +++++++++++++++++ 5 files changed, 130 insertions(+), 56 deletions(-) rename src/ServiceControl.Management.PowerShell/Cmdlets/{MaintenanceMode/MaintenanceModeCmdletBase.cs => AuditInstances/StartServiceControlAuditMaintenanceMode.cs} (74%) create mode 100644 src/ServiceControl.Management.PowerShell/Cmdlets/AuditInstances/StopServiceControlAuditMaintenanceMode.cs delete mode 100644 src/ServiceControl.Management.PowerShell/Cmdlets/MaintenanceMode/MaintenanceModeCmdlets.cs create mode 100644 src/ServiceControl.Management.PowerShell/Cmdlets/ServiceControlInstances/StartServiceControlMaintenanceMode.cs create mode 100644 src/ServiceControl.Management.PowerShell/Cmdlets/ServiceControlInstances/StopServiceControlMaintenanceMode.cs diff --git a/src/ServiceControl.Management.PowerShell/Cmdlets/MaintenanceMode/MaintenanceModeCmdletBase.cs b/src/ServiceControl.Management.PowerShell/Cmdlets/AuditInstances/StartServiceControlAuditMaintenanceMode.cs similarity index 74% rename from src/ServiceControl.Management.PowerShell/Cmdlets/MaintenanceMode/MaintenanceModeCmdletBase.cs rename to src/ServiceControl.Management.PowerShell/Cmdlets/AuditInstances/StartServiceControlAuditMaintenanceMode.cs index 8b1428c9ad..eeaad435df 100644 --- a/src/ServiceControl.Management.PowerShell/Cmdlets/MaintenanceMode/MaintenanceModeCmdletBase.cs +++ b/src/ServiceControl.Management.PowerShell/Cmdlets/AuditInstances/StartServiceControlAuditMaintenanceMode.cs @@ -1,19 +1,17 @@ namespace ServiceControl.Management.PowerShell { using System; + using System.Linq; using System.Management.Automation; using ServiceControlInstaller.Engine.Instances; - public abstract class MaintenanceModeCmdletBase : PSCmdlet + [Cmdlet(VerbsLifecycle.Start, "ServiceControlAuditMaintenanceMode")] + public class StartServiceControlAuditMaintenanceMode : PSCmdlet { [ValidateNotNullOrEmpty] - [Parameter(Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0, HelpMessage = "Specify the instance name")] + [Parameter(Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0, HelpMessage = "Specify the ServiceControl Audit instance name")] public string[] Name { get; set; } - protected abstract bool StartInMaintenanceMode { get; } - - protected abstract ServiceControlBaseService FindInstance(string name); - protected override void BeginProcessing() { Account.TestIfAdmin(); @@ -23,7 +21,7 @@ protected override void ProcessRecord() { foreach (var name in Name) { - var instance = FindInstance(name); + var instance = InstanceFinder.ServiceControlAuditInstances().FirstOrDefault(i => i.Name.Equals(name, StringComparison.OrdinalIgnoreCase)); if (instance == null) { WriteWarning($"No action taken. An instance called {name} was not found"); @@ -35,12 +33,6 @@ protected override void ProcessRecord() ThrowTerminatingError(new ErrorRecord(new Exception($"Failed to stop {instance.Name}"), "StopFailure", ErrorCategory.InvalidResult, null)); } - if (!StartInMaintenanceMode) - { - instance.DisableMaintenanceMode(); - continue; - } - instance.EnableMaintenanceMode(); if (!instance.TryStartService()) diff --git a/src/ServiceControl.Management.PowerShell/Cmdlets/AuditInstances/StopServiceControlAuditMaintenanceMode.cs b/src/ServiceControl.Management.PowerShell/Cmdlets/AuditInstances/StopServiceControlAuditMaintenanceMode.cs new file mode 100644 index 0000000000..1a09df9ff2 --- /dev/null +++ b/src/ServiceControl.Management.PowerShell/Cmdlets/AuditInstances/StopServiceControlAuditMaintenanceMode.cs @@ -0,0 +1,40 @@ +namespace ServiceControl.Management.PowerShell +{ + using System; + using System.Linq; + using System.Management.Automation; + using ServiceControlInstaller.Engine.Instances; + + [Cmdlet(VerbsLifecycle.Stop, "ServiceControlAuditMaintenanceMode")] + public class StopServiceControlAuditMaintenanceMode : PSCmdlet + { + [ValidateNotNullOrEmpty] + [Parameter(Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0, HelpMessage = "Specify the ServiceControl Audit instance name")] + public string[] Name { get; set; } + + protected override void BeginProcessing() + { + Account.TestIfAdmin(); + } + + protected override void ProcessRecord() + { + foreach (var name in Name) + { + var instance = InstanceFinder.ServiceControlAuditInstances().FirstOrDefault(i => i.Name.Equals(name, StringComparison.OrdinalIgnoreCase)); + if (instance == null) + { + WriteWarning($"No action taken. An instance called {name} was not found"); + continue; + } + + if (!instance.TryStopService()) + { + ThrowTerminatingError(new ErrorRecord(new Exception($"Failed to stop {instance.Name}"), "StopFailure", ErrorCategory.InvalidResult, null)); + } + + instance.DisableMaintenanceMode(); + } + } + } +} diff --git a/src/ServiceControl.Management.PowerShell/Cmdlets/MaintenanceMode/MaintenanceModeCmdlets.cs b/src/ServiceControl.Management.PowerShell/Cmdlets/MaintenanceMode/MaintenanceModeCmdlets.cs deleted file mode 100644 index 48d41316db..0000000000 --- a/src/ServiceControl.Management.PowerShell/Cmdlets/MaintenanceMode/MaintenanceModeCmdlets.cs +++ /dev/null @@ -1,43 +0,0 @@ -namespace ServiceControl.Management.PowerShell -{ - using System; - using System.Linq; - using System.Management.Automation; - using ServiceControlInstaller.Engine.Instances; - - [Cmdlet(VerbsLifecycle.Start, "ServiceControlMaintenanceMode")] - public class StartServiceControlMaintenanceMode : MaintenanceModeCmdletBase - { - protected override bool StartInMaintenanceMode => true; - - protected override ServiceControlBaseService FindInstance(string name) => - InstanceFinder.ServiceControlInstances().FirstOrDefault(i => i.Name.Equals(name, StringComparison.OrdinalIgnoreCase)); - } - - [Cmdlet(VerbsLifecycle.Stop, "ServiceControlMaintenanceMode")] - public class StopServiceControlMaintenanceMode : MaintenanceModeCmdletBase - { - protected override bool StartInMaintenanceMode => false; - - protected override ServiceControlBaseService FindInstance(string name) => - InstanceFinder.ServiceControlInstances().FirstOrDefault(i => i.Name.Equals(name, StringComparison.OrdinalIgnoreCase)); - } - - [Cmdlet(VerbsLifecycle.Start, "ServiceControlAuditMaintenanceMode")] - public class StartServiceControlAuditMaintenanceMode : MaintenanceModeCmdletBase - { - protected override bool StartInMaintenanceMode => true; - - protected override ServiceControlBaseService FindInstance(string name) => - InstanceFinder.ServiceControlAuditInstances().FirstOrDefault(i => i.Name.Equals(name, StringComparison.OrdinalIgnoreCase)); - } - - [Cmdlet(VerbsLifecycle.Stop, "ServiceControlAuditMaintenanceMode")] - public class StopServiceControlAuditMaintenanceMode : MaintenanceModeCmdletBase - { - protected override bool StartInMaintenanceMode => false; - - protected override ServiceControlBaseService FindInstance(string name) => - InstanceFinder.ServiceControlAuditInstances().FirstOrDefault(i => i.Name.Equals(name, StringComparison.OrdinalIgnoreCase)); - } -} diff --git a/src/ServiceControl.Management.PowerShell/Cmdlets/ServiceControlInstances/StartServiceControlMaintenanceMode.cs b/src/ServiceControl.Management.PowerShell/Cmdlets/ServiceControlInstances/StartServiceControlMaintenanceMode.cs new file mode 100644 index 0000000000..20f2a7fa00 --- /dev/null +++ b/src/ServiceControl.Management.PowerShell/Cmdlets/ServiceControlInstances/StartServiceControlMaintenanceMode.cs @@ -0,0 +1,45 @@ +namespace ServiceControl.Management.PowerShell +{ + using System; + using System.Linq; + using System.Management.Automation; + using ServiceControlInstaller.Engine.Instances; + + [Cmdlet(VerbsLifecycle.Start, "ServiceControlMaintenanceMode")] + public class StartServiceControlMaintenanceMode : PSCmdlet + { + [ValidateNotNullOrEmpty] + [Parameter(Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0, HelpMessage = "Specify the ServiceControl instance name")] + public string[] Name { get; set; } + + protected override void BeginProcessing() + { + Account.TestIfAdmin(); + } + + protected override void ProcessRecord() + { + foreach (var name in Name) + { + var instance = InstanceFinder.ServiceControlInstances().FirstOrDefault(i => i.Name.Equals(name, StringComparison.OrdinalIgnoreCase)); + if (instance == null) + { + WriteWarning($"No action taken. An instance called {name} was not found"); + continue; + } + + if (!instance.TryStopService()) + { + ThrowTerminatingError(new ErrorRecord(new Exception($"Failed to stop {instance.Name}"), "StopFailure", ErrorCategory.InvalidResult, null)); + } + + instance.EnableMaintenanceMode(); + + if (!instance.TryStartService()) + { + ThrowTerminatingError(new ErrorRecord(new Exception($"Failed to start {instance.Name}"), "StartFailure", ErrorCategory.InvalidResult, null)); + } + } + } + } +} diff --git a/src/ServiceControl.Management.PowerShell/Cmdlets/ServiceControlInstances/StopServiceControlMaintenanceMode.cs b/src/ServiceControl.Management.PowerShell/Cmdlets/ServiceControlInstances/StopServiceControlMaintenanceMode.cs new file mode 100644 index 0000000000..c75007ccc7 --- /dev/null +++ b/src/ServiceControl.Management.PowerShell/Cmdlets/ServiceControlInstances/StopServiceControlMaintenanceMode.cs @@ -0,0 +1,40 @@ +namespace ServiceControl.Management.PowerShell +{ + using System; + using System.Linq; + using System.Management.Automation; + using ServiceControlInstaller.Engine.Instances; + + [Cmdlet(VerbsLifecycle.Stop, "ServiceControlMaintenanceMode")] + public class StopServiceControlMaintenanceMode : PSCmdlet + { + [ValidateNotNullOrEmpty] + [Parameter(Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, Position = 0, HelpMessage = "Specify the ServiceControl instance name")] + public string[] Name { get; set; } + + protected override void BeginProcessing() + { + Account.TestIfAdmin(); + } + + protected override void ProcessRecord() + { + foreach (var name in Name) + { + var instance = InstanceFinder.ServiceControlInstances().FirstOrDefault(i => i.Name.Equals(name, StringComparison.OrdinalIgnoreCase)); + if (instance == null) + { + WriteWarning($"No action taken. An instance called {name} was not found"); + continue; + } + + if (!instance.TryStopService()) + { + ThrowTerminatingError(new ErrorRecord(new Exception($"Failed to stop {instance.Name}"), "StopFailure", ErrorCategory.InvalidResult, null)); + } + + instance.DisableMaintenanceMode(); + } + } + } +}