mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Add ValidateNullOrEmpty to -Name parameter of Get-Service (#2542)
* Adding validation to Get-Service -Name parameter
This commit is contained in:
committed by
Sergei Vorobev
parent
baeec066d9
commit
67638548fc
@@ -608,6 +608,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// since it is optional for GetService and mandatory otherwise.
|
||||
/// </remarks>
|
||||
[Parameter(Position = 0, ParameterSetName = "Default", ValueFromPipelineByPropertyName = true, ValueFromPipeline = true)]
|
||||
[ValidateNotNullOrEmpty()]
|
||||
[Alias("ServiceName")]
|
||||
public string[] Name
|
||||
{
|
||||
|
||||
@@ -0,0 +1,43 @@
|
||||
Describe "Get-Service cmdlet tests" -Tags "CI" {
|
||||
# Service cmdlet is currently working on windows only
|
||||
# So skip the tests on non-Windows
|
||||
BeforeAll {
|
||||
$originalDefaultParameterValues = $PSDefaultParameterValues.Clone()
|
||||
if ( -not $IsWindows ) {
|
||||
$PSDefaultParameterValues["it:skip"] = $true
|
||||
}
|
||||
}
|
||||
# Restore the defaults
|
||||
AfterAll {
|
||||
$global:PSDefaultParameterValues = $originalDefaultParameterValues
|
||||
}
|
||||
|
||||
$testCases =
|
||||
@{ data = $null; value = 'null' },
|
||||
@{ data = [String]::Empty; value = 'empty string' }
|
||||
|
||||
Context 'Check null or empty value to the -Name parameter' {
|
||||
It 'Should throw if <value> is passed to -Name parameter' -TestCases $testCases {
|
||||
param($data)
|
||||
try {
|
||||
$null = Get-Service -Name $data -ErrorAction Stop
|
||||
throw 'Expected error on previous command'
|
||||
}
|
||||
catch {
|
||||
$_.FullyQualifiedErrorId | Should Be 'ParameterArgumentValidationError,Microsoft.Powershell.Commands.GetServiceCommand'
|
||||
}
|
||||
}
|
||||
}
|
||||
Context 'Check null or empty value to the -Name parameter via pipeline' {
|
||||
It 'Should throw if <value> is passed through pipeline to -Name parameter' -TestCases $testCases {
|
||||
param($data)
|
||||
try {
|
||||
$null = Get-Service -Name $data -ErrorAction Stop
|
||||
throw 'Expected error on previous command'
|
||||
}
|
||||
catch {
|
||||
$_.FullyQualifiedErrorId | Should Be 'ParameterArgumentValidationError,Microsoft.Powershell.Commands.GetServiceCommand'
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user