# Copyright (c) Microsoft Corporation. # Licensed under the MIT License. [System.Diagnostics.CodeAnalysis.SuppressMessageAttribute('PSAvoidUsingConvertToSecureStringWithPlainText', '')] param() Import-Module (Join-Path -Path $PSScriptRoot '..\Microsoft.PowerShell.Security\certificateCommon.psm1') Describe "Set/New/Remove-Service cmdlet tests" -Tags "Feature", "RequireAdminOnWindows" { BeforeAll { $originalDefaultParameterValues = $PSDefaultParameterValues.Clone() if ( -not $IsWindows ) { $PSDefaultParameterValues["it:skip"] = $true } if ($IsWindows) { $userName = "testuserservices" $testPass = [Net.NetworkCredential]::new("", (New-ComplexPassword)).SecurePassword $creds = [pscredential]::new(".\$userName", $testPass) $SecurityDescriptorSddl = 'D:(A;;CCLCSWRPWPDTLOCRRC;;;SY)(A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;BA)(A;;CCLCSWLOCRRC;;;SU)' $WrongSecurityDescriptorSddl = 'D:(A;;CCLCSWRPWPDTLOCRRC;;;SY)(A;;CCDCLCSWRPWPDTLOCRSDRCWDWO;;;BB)(A;;CCLCSWLOCRRC;;;SU)' net user $userName $creds.GetNetworkCredential().Password /add > $null $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 } Function CheckSecurityDescriptorSddl { Param( [Parameter(Mandatory)] $SecurityDescriptorSddl, [Parameter(Mandatory)] $ServiceName ) $Counter = 0 $ExpectedSDDL = ConvertFrom-SddlString -Sddl $SecurityDescriptorSddl # Selecting the first item in the output array as below command gives plain text output from the native sc.exe. $UpdatedSDDL = ConvertFrom-SddlString -Sddl (sc sdshow $ServiceName)[1] $UpdatedSDDL.Owner | Should -Be $ExpectedSDDL.Owner $UpdatedSDDL.Group | Should -Be $ExpectedSDDL.Group $UpdatedSDDL.DiscretionaryAcl.Count | Should -Be $ExpectedSDDL.DiscretionaryAcl.Count $UpdatedSDDL.DiscretionaryAcl | ForEach-Object -Process { $_ | Should -Be $ExpectedSDDL.DiscretionaryAcl[$Counter] $Counter++ } } } AfterAll { $global:PSDefaultParameterValues = $originalDefaultParameterValues if ($IsWindows) { net user $userName /delete > $null Stop-Service $testservicename2 Stop-Service $testservicename1 Remove-Service $testservicename2 Remove-Service $testservicename1 } } It "SetServiceCommand can be used as API for '' with ''" -TestCases @( @{parameter = "Name" ; value = "bar"}, @{parameter = "DisplayName" ; value = "hello"}, @{parameter = "Description" ; value = "hello world"}, @{parameter = "StartupType" ; value = "Automatic"}, @{parameter = "StartupType" ; value = "Disabled"}, @{parameter = "StartupType" ; value = "Manual"}, @{parameter = "Status" ; value = "Running"}, @{parameter = "Status" ; value = "Stopped"}, @{parameter = "Status" ; value = "Paused"}, @{parameter = "InputObject" ; script = {Get-Service | Select-Object -First 1}}, # cmdlet inherits this property, but it's not exposed as parameter so it should be $null @{parameter = "Include" ; value = "foo", "bar" ; expectedNull = $true}, # cmdlet inherits this property, but it's not exposed as parameter so it should be $null @{parameter = "Exclude" ; value = "foo", "bar" ; expectedNull = $true} ) { param($parameter, $value, $script, $expectedNull) $setServiceCommand = [Microsoft.PowerShell.Commands.SetServiceCommand]::new() if ($script -ne $null) { $value = & $script } $setServiceCommand.$parameter = $value if ($expectedNull -eq $true) { $setServiceCommand.$parameter | Should -BeNullOrEmpty } else { $setServiceCommand.$parameter | Should -Be $value } } It "Set-Service parameter validation for invalid values: