Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes372downloads
Get-Service.Tests.ps1142 linesDownload Raw Back to Microsoft.PowerShell.Management
1# Copyright (c) Microsoft Corporation.2# Licensed under the MIT License.3Describe "Get-Service cmdlet tests" -Tags "CI" {4  # Service cmdlet is currently working on windows only5  # So skip the tests on non-Windows6  BeforeAll {7    $originalDefaultParameterValues = $PSDefaultParameterValues.Clone()8    if ( -not $IsWindows ) {9        $PSDefaultParameterValues["it:skip"] = $true10    }11  }12  # Restore the defaults13  AfterAll {14      $global:PSDefaultParameterValues = $originalDefaultParameterValues15  }16 17  $testCases =18    @{ data = $null          ; value = 'null' },19    @{ data = [String]::Empty; value = 'empty string' }20 21  Context 'Check null or empty value to the -Name parameter' {22    It 'Should throw if <value> is passed to -Name parameter' -TestCases $testCases {23      param($data)24      { $null = Get-Service -Name $data -ErrorAction Stop } |25        Should -Throw -ErrorId 'ParameterArgumentValidationError,Microsoft.Powershell.Commands.GetServiceCommand'26    }27  }28 29  Context 'Check null or empty value to the -Name parameter via pipeline' {30    It 'Should throw if <value> is passed through pipeline to -Name parameter' -TestCases $testCases {31      param($data)32      { $null = Get-Service -Name $data -ErrorAction Stop } |33        Should -Throw -ErrorId 'ParameterArgumentValidationError,Microsoft.Powershell.Commands.GetServiceCommand'34    }35  }36 37  It "GetServiceCommand can be used as API for '<parameter>' with '<value>'" -TestCases @(38    @{ parameter = "DisplayName" ; value = "foo" },39    @{ parameter = "Include"     ; value = "foo","bar" },40    @{ parameter = "Exclude"     ; value = "bar","foo" },41    @{ parameter = "InputObject" ; script = { Get-Service | Select-Object -First 1 } },42    @{ parameter = "Name"        ; value = "foo","bar" }43  ) {44    param($parameter, $value, $script)45    if ($script -ne $null) {46      $value = & $script47    }48 49    $getservicecmd = [Microsoft.PowerShell.Commands.GetServiceCommand]::new()50    $getservicecmd.$parameter = $value51    $getservicecmd.$parameter | Should -BeExactly $value52  }53 54  It "Get-Service filtering works for '<script>'" -TestCases @(55    @{ script = { Get-Service -DisplayName Net* }               ; expected = { Get-Service | Where-Object { $_.DisplayName -like 'Net*' } } },56    @{ script = { Get-Service -Include Net* -Exclude *logon }   ; expected = { Get-Service | Where-Object { $_.Name -match '^net.*?(?<!logon)$' } } }57    @{ script = { Get-Service -Name Net* | Get-Service }        ; expected = { Get-Service -Name Net* } },58    @{ script = { Get-Service -Name "$(New-Guid)*" }            ; expected = $null },59    @{ script = { Get-Service -DisplayName "$(New-Guid)*" }     ; expected = $null },60    @{ script = { Get-Service -DependentServices -Name winmgmt }; expected = { (Get-Service -Name winmgmt).DependentServices } },61    @{ script = { Get-Service -RequiredServices -Name winmgmt } ; expected = { (Get-Service -Name winmgmt).RequiredServices } }62  ) {63    param($script, $expected)64    $services = & $script65    if ($expected -ne $null) {66      $servicesCheck = & $expected67    }68    if ($servicesCheck -ne $null) {69      Compare-Object $services $servicesCheck | Out-String | Should -BeNullOrEmpty70    } else {71      $services | Should -BeNullOrEmpty72    }73  }74 75  It "Get-Service fails for non-existing service using '<script>'" -TestCases @(76    @{ script  = { Get-Service -Name (New-Guid) -ErrorAction Stop}       ;77       ErrorId = "NoServiceFoundForGivenName,Microsoft.PowerShell.Commands.GetServiceCommand" },78    @{ script  = { Get-Service -DisplayName (New-Guid) -ErrorAction Stop};79       ErrorId = "NoServiceFoundForGivenDisplayName,Microsoft.PowerShell.Commands.GetServiceCommand" }80  ) {81    param($script,$errorid)82    { & $script } | Should -Throw -ErrorId $errorid83  }84}85 86Describe 'Get-Service Admin tests' -Tag CI,RequireAdminOnWindows {87  BeforeAll {88    $originalDefaultParameterValues = $PSDefaultParameterValues.Clone()89    if ( -not $IsWindows ) {90        $PSDefaultParameterValues["it:skip"] = $true91    }92  }93  AfterAll {94      $global:PSDefaultParameterValues = $originalDefaultParameterValues95  }96 97  BeforeEach {98    $serviceParams = @{99      Name = "PowerShellTest-$([Guid]::NewGuid().Guid)"100      BinaryPathName = "$env:SystemRoot\System32\cmd.exe"101      StartupType = 'Manual'102    }103    $service = New-Service @serviceParams104  }105  AfterEach {106    $service | Remove-Service107  }108 109  It "Ignores invalid description MUI entry" {110    $serviceRegPath = "HKLM:\SYSTEM\CurrentControlSet\Services\$($service.Name)"111    Set-ItemProperty -LiteralPath $serviceRegPath -Name Description -Value '@Fake.dll,-0'112    $actual = Get-Service -Name $service.Name -ErrorAction Stop113 114    $actual.Name | Should -Be $service.Name115    $actual.Status | Should -Be Stopped116    $actual.Description | Should -BeNullOrEmpty117    $actual.UserName | Should -Be LocalSystem118    $actual.StartupType | Should -Be Manual119  }120 121  It "Ignores no SERVICE_QUERY_CONFIG access" {122    $sddl = ((sc.exe sdshow $service.Name) -join "").Trim()123    $sd = ConvertFrom-SddlString -Sddl $sddl124    $sd.RawDescriptor.DiscretionaryAcl.AddAccess(125      [System.Security.AccessControl.AccessControlType]::Deny,126      [System.Security.Principal.WindowsIdentity]::GetCurrent().User,127      0x1,  # SERVICE_QUERY_CONFIG128      [System.Security.AccessControl.InheritanceFlags]::None,129      [System.Security.AccessControl.PropagationFlags]::None)130    $newSddl = $sd.RawDescriptor.GetSddlForm([System.Security.AccessControl.AccessControlSections]::All)131    $null = sc.exe sdset $service.Name $newSddl132 133    $actual = Get-Service -Name $service.Name -ErrorAction Stop134 135    $actual.Name | Should -Be $service.Name136    $actual.Status | Should -Be Stopped137    $actual.Description | Should -BeNullOrEmpty138    $actual.UserName | Should -BeNullOrEmpty139    $actual.StartupType | Should -Be InvalidValue140  }141}142