Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
b2103c1
Fix the Set-Service -Status Stopped issue when the service has depend…
zhenggu Nov 22, 2017
56d2b2b
remove ServiceIsDependentOn for it is not used.
zhenggu Nov 22, 2017
f802e93
remove one blank
zhenggu Nov 22, 2017
e265545
Merge branch 'master' of https://github.com/PowerShell/PowerShell
zhenggu Jul 2, 2018
f1e071f
add test case: Set-Service can run -Status Stopped to stop a service …
zhenggu Jul 3, 2018
e248428
add the status of the service after run "-Status stopped", and change…
zhenggu Jul 3, 2018
d7e3a64
add a TestServie for Set-Service testing
zhenggu Jul 7, 2018
b2f84e9
remove force flag
zhenggu Jul 20, 2018
18e5a58
correct one comment
zhenggu Jul 20, 2018
a12be35
Revert "remove force flag"
zhenggu Jul 20, 2018
ee0b9b3
remove TestService written by c
zhenggu Jul 20, 2018
59605f1
add a existing check for spooler before start testing
zhenggu Jul 21, 2018
fac8483
add skip step when cannot find service spooler
zhenggu Jul 22, 2018
a623b94
add a c# version of TestService
zhenggu Jul 23, 2018
7087f06
add TestService into Publish-PSTestTools
zhenggu Jul 23, 2018
b2added
add new test case with TestService
zhenggu Jul 23, 2018
a672eef
make the code format unique
zhenggu Jul 23, 2018
def8fe2
put testservice setup in BeforeAll and cleanup in AfterAll
zhenggu Jul 23, 2018
ff45184
remove some useless comments
zhenggu Jul 24, 2018
ed14f7f
fix one bug
zhenggu Jul 24, 2018
b31d813
fix one bug
zhenggu Jul 24, 2018
69d014c
Add copyright for csharp files, divide one it statement to three ones
zhenggu Aug 7, 2018
057e640
[Feature]
adityapatwardhan Aug 27, 2018
3379733
[Feature] fix one runtime issue
zhenggu Aug 28, 2018
da52a86
[Feature] fix one runtime issue
zhenggu Aug 28, 2018
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions build.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -957,6 +957,7 @@ function Publish-PSTestTools {
$tools = @(
@{Path="${PSScriptRoot}/test/tools/TestExe";Output="testexe"}
@{Path="${PSScriptRoot}/test/tools/WebListener";Output="WebListener"}
@{Path="${PSScriptRoot}/test/tools/TestService";Output="TestService"}
)

$Options = Get-PSOptions -DefaultToNew
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -1532,6 +1532,15 @@ public string Status
}
internal string serviceStatus = null;

/// <summary>
/// The following is the definition of the input parameter "Force".
/// This parameter is useful only when parameter "Stop" is enabled.
/// If "Force" is enabled, it will also stop the dependent services.
/// If not, it will send an error when this service has dependent ones.
/// </summary>
[Parameter]
public SwitchParameter Force { get; set; }

/// <summary>
/// This is not a parameter for this cmdlet.
/// </summary>
Expand Down Expand Up @@ -1779,24 +1788,17 @@ protected override void ProcessRecord()
{
if (!service.Status.Equals(ServiceControllerStatus.Stopped))
{
//check for the dependent services as set-service dont have force parameter
// Check for the dependent services as set-service dont have force parameter
ServiceController[] dependentServices = service.DependentServices;

if ((dependentServices != null) && (dependentServices.Length > 0))
if ((!Force) && (dependentServices != null) && (dependentServices.Length > 0))
{
WriteNonTerminatingError(service, null, "ServiceHasDependentServicesNoForce", ServiceResources.ServiceHasDependentServicesNoForce, ErrorCategory.InvalidOperation);
return;
}

ServiceController[] servicedependedon = service.ServicesDependedOn;

if ((servicedependedon != null) && (servicedependedon.Length > 0))
{
WriteNonTerminatingError(service, null, "ServiceIsDependentOnNoForce", ServiceResources.ServiceIsDependentOnNoForce, ErrorCategory.InvalidOperation);
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should remove the resource string from RESX file it is no longer used.

return;
}
// Stop service, pass 'true' to the force parameter as we have already checked for the dependent services.
DoStopService(service, force: true, waitForServiceToStop: true);
DoStopService(service, Force, waitForServiceToStop: true);
}
}
else if (Status.Equals("Paused", StringComparison.CurrentCultureIgnoreCase))
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -132,9 +132,6 @@
<data name="ServiceHasDependentServicesNoForce" xml:space="preserve">
<value>Cannot stop service '{1} ({0})' because it has dependent services.</value>
</data>
<data name="ServiceIsDependentOnNoForce" xml:space="preserve">
<value>Cannot stop service '{1} ({0})' because it is dependent on other services.</value>
</data>
<data name="CouldNotStopService" xml:space="preserve">
<value>Service '{1} ({0})' cannot be stopped due to the following error: {2}</value>
</data>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -12,12 +12,28 @@ Describe "Set/New/Remove-Service cmdlet tests" -Tags "Feature", "RequireAdminOnW
net user $userName $testPass /add > $null
$password = ConvertTo-SecureString $testPass -AsPlainText -Force
$creds = [pscredential]::new(".\$userName", $password)

$testservicename1 = "testservice1"
$testservicename2 = "testservice2"
$svcbinaryname = "TestService"
$svccmd = Get-Command $svcbinaryname
$svccmd | Should -Not -BeNullOrEmpty
$svcfullpath = $svccmd.Path
$testservice1 = New-Service -BinaryPathName $svcfullpath -Name $testservicename1
$testservice1 | Should -Not -BeNullOrEmpty
$testservice2 = New-Service -BinaryPathName $svcfullpath -Name $testservicename2 -DependsOn $testservicename1
$testservice2 | Should -Not -BeNullOrEmpty
}
}
AfterAll {
$global:PSDefaultParameterValues = $originalDefaultParameterValues
if ($IsWindows) {
net user $userName /delete > $null

Stop-Service $testservicename2
Stop-Service $testservicename1
Remove-Service $testservicename2
Remove-Service $testservicename1
}
}

Expand Down Expand Up @@ -329,4 +345,32 @@ Describe "Set/New/Remove-Service cmdlet tests" -Tags "Feature", "RequireAdminOnW
}
{ & $cmdlet @parameters } | Should -Throw -ErrorId $errorid
}

Context "Set-Service test cases on the services with dependent relationship" {
BeforeEach {
{ Set-Service -Status Running $testservicename2 } | Should -Not -Throw
(Get-Service $testservicename1).Status | Should -BeExactly "Running"
(Get-Service $testservicename2).Status | Should -BeExactly "Running"
}

It "Set-Service can stop a service with dependency" {
$script = { Set-Service -Status Stopped $testservicename2 -ErrorAction Stop }
{ & $script } | Should -Not -Throw
(Get-Service $testservicename2).Status | Should -BeExactly "Stopped"
}

It "Set-Service cannot stop a service with running dependent service" {
$script = { Set-Service -Status Stopped $testservicename1 -ErrorAction Stop }
{ & $script } | Should -Throw
(Get-Service $testservicename1).Status | Should -BeExactly "Running"
(Get-Service $testservicename2).Status | Should -BeExactly "Running"
}

It "Set-Service can stop a service with running dependent service by parameter -Force" {
$script = { Set-Service -Status Stopped -Force $testservicename1 -ErrorAction Stop }
{ & $script } | Should -Not -Throw
(Get-Service $testservicename1).Status | Should -BeExactly "Stopped"
(Get-Service $testservicename2).Status | Should -BeExactly "Stopped"
}
}
}
3 changes: 2 additions & 1 deletion test/tools/OpenCover/OpenCover.psm1
Original file line number Diff line number Diff line change
Expand Up @@ -667,7 +667,8 @@ function Invoke-OpenCover

$updatedEnvPath = "${PowerShellExeDirectory}\Modules;$TestToolsModulesPath"
$testToolsExePath = (Resolve-Path(Join-Path $TestPath -ChildPath "..\tools\TestExe\bin")).Path
$updatedProcessEnvPath = "${testToolsExePath};${env:PATH}"
$testServiceExePath = (Resolve-Path(Join-Path $TestPath -ChildPath "..\tools\TestService\bin")).Path
$updatedProcessEnvPath = "${testServiceExePath};${testToolsExePath};${env:PATH}"

$startupArgs = "Set-ExecutionPolicy Bypass -Force -Scope Process; `$env:PSModulePath = '${updatedEnvPath}'; `$env:Path = '${updatedProcessEnvPath}';"
$targetArgs = "${startupArgs}", "Invoke-Pester","${TestPath}","-OutputFormat $PesterLogFormat"
Expand Down
19 changes: 19 additions & 0 deletions test/tools/TestService/Program.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System.ServiceProcess;

namespace TestService
{
static class Program
{
static void Main()
{
ServiceBase[] ServicesToRun;
ServicesToRun = new ServiceBase[]
{
new Service1()
};
ServiceBase.Run(ServicesToRun);
}
}
}
24 changes: 24 additions & 0 deletions test/tools/TestService/Service1.Designer.cs

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

22 changes: 22 additions & 0 deletions test/tools/TestService/Service1.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System.ServiceProcess;

namespace TestService
{
public partial class Service1 : ServiceBase
{
public Service1()
{
InitializeComponent();
}

protected override void OnStart(string[] args)
{
}

protected override void OnStop()
{
}
}
}
16 changes: 16 additions & 0 deletions test/tools/TestService/TestService.csproj
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
<Project Sdk="Microsoft.NET.Sdk">

<Import Project="..\..\Test.Common.props"/>

<PropertyGroup>
<Description>Very tiny windows service to do service testing</Description>
<AssemblyName>TestService</AssemblyName>
<OutputType>Exe</OutputType>
<RuntimeIdentifiers>win7-x86;win7-x64</RuntimeIdentifiers>
</PropertyGroup>

<ItemGroup>
<PackageReference Include="Microsoft.Windows.Compatibility" Version="2.0.0" />
</ItemGroup>

</Project>