MegaBites-AI/Windows-powershell
0372
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "Control Service cmdlet tests" -Tags "Feature","RequireAdminOnWindows" {4 BeforeAll {5 $originalDefaultParameterValues = $PSDefaultParameterValues.Clone()6 if ( -not $IsWindows ) {7 $PSDefaultParameterValues["it:skip"] = $true8 }9 }10 AfterAll {11 $global:PSDefaultParameterValues = $originalDefaultParameterValues12 }13 14 It "StopServiceCommand can be used as API for '<parameter>' with '<value>'" -TestCases @(15 @{parameter="Force";value=$true},16 @{parameter="Force";value=$false},17 @{parameter="NoWait";value=$true},18 @{parameter="NoWait";value=$false}19 ) {20 param($parameter, $value)21 $stopservicecmd = [Microsoft.PowerShell.Commands.StopServiceCommand]::new()22 $stopservicecmd.$parameter = $value23 $stopservicecmd.$parameter | Should -Be $value24 }25 26 It "RestartServiceCommand can be used as API for '<parameter>' with '<value>'" -TestCases @(27 @{parameter="Force";value=$true},28 @{parameter="Force";value=$false}29 ) {30 param($parameter, $value)31 $restartservicecmd = [Microsoft.PowerShell.Commands.RestartServiceCommand]::new()32 $restartservicecmd.$parameter = $value33 $restartservicecmd.$parameter | Should -Be $value34 }35 36 It "Stop/Start/Restart-Service works" {37 $wasStopped = $false38 try {39 $spooler = Get-Service Spooler40 $spooler | Should -Not -BeNullOrEmpty41 if ($spooler.Status -ne "Running") {42 $wasStopped = $true43 $spooler = Start-Service Spooler -PassThru44 }45 $spooler.Status | Should -BeExactly "Running"46 $spooler = Stop-Service Spooler -PassThru47 $spooler.Status | Should -BeExactly "Stopped"48 (Get-Service Spooler).Status | Should -BeExactly "Stopped"49 $spooler = Start-Service Spooler -PassThru50 $spooler.Status | Should -BeExactly "Running"51 (Get-Service Spooler).Status | Should -BeExactly "Running"52 Stop-Service Spooler53 (Get-Service Spooler).Status | Should -BeExactly "Stopped"54 $spooler = Restart-Service Spooler -PassThru55 $spooler.Status | Should -BeExactly "Running"56 (Get-Service Spooler).Status | Should -BeExactly "Running"57 } finally {58 if ($wasStopped) {59 Stop-Service Spooler60 }61 }62 }63 64 It "Suspend/Resume-Service works" {65 try {66 $originalState = "Running"67 $serviceName = "WerSvc"68 $service = Get-Service $serviceName69 if ($service.Status -ne $originalState) {70 $originalState = $service.Status71 Start-Service $serviceName72 }73 $service | Should -Not -BeNullOrEmpty74 Suspend-Service $serviceName75 (Get-Service $serviceName).Status | Should -BeExactly "Paused"76 Resume-Service $serviceName77 (Get-Service $serviceName).Status | Should -BeExactly "Running"78 } finally {79 Set-Service $serviceName -Status $originalState80 }81 }82 83 It "Failure to control service with '<script>'" -TestCases @(84 @{script={Stop-Service dcomlaunch -ErrorAction Stop};errorid="ServiceHasDependentServices,Microsoft.PowerShell.Commands.StopServiceCommand"},85 @{script={Suspend-Service winrm -ErrorAction Stop};errorid="CouldNotSuspendServiceNotSupported,Microsoft.PowerShell.Commands.SuspendServiceCommand"},86 @{script={Resume-Service winrm -ErrorAction Stop};errorid="CouldNotResumeServiceNotSupported,Microsoft.PowerShell.Commands.ResumeServiceCommand"},87 @{script={Stop-Service $(New-Guid) -ErrorAction Stop};errorid="NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.StopServiceCommand"},88 @{script={Start-Service $(New-Guid) -ErrorAction Stop};errorid="NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.StartServiceCommand"},89 @{script={Resume-Service $(New-Guid) -ErrorAction Stop};errorid="NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.ResumeServiceCommand"},90 @{script={Suspend-Service $(New-Guid) -ErrorAction Stop};errorid="NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.SuspendServiceCommand"},91 @{script={Restart-Service $(New-Guid) -ErrorAction Stop};errorid="NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.RestartServiceCommand"}92 ) {93 param($script,$errorid)94 { & $script } | Should -Throw -ErrorId $errorid95 }96 97}98 