SetServiceCommand: Add positional parameter attribute (#5017)

* SetServiceCommand: Add positional parameter attribute

Added a positional parameter attribute to the InputObject parameter,
giving Set-Service behavior similar to the other *-Service cmdlets.

* Add test for positional InputObjects [Feature]

Added a test to Set-Service validating InputObjects passed positionally.
This commit is contained in:
Travis Kinney 2017-10-09 09:30:50 -07:00 committed by Aditya Patwardhan
parent 5cec922941
commit 1d5c310897
2 changed files with 35 additions and 17 deletions

View file

@ -1436,7 +1436,7 @@ namespace Microsoft.PowerShell.Commands
/// service name
/// </summary>
/// <value></value>
[Parameter(Position = 0, Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true, ParameterSetName = "Name")]
[Parameter(Mandatory = true, ParameterSetName = "Name", Position = 0, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
[Alias("ServiceName", "SN")]
public new String Name
{
@ -1448,6 +1448,14 @@ namespace Microsoft.PowerShell.Commands
}
internal String serviceName = null;
/// <summary>
/// The following is the definition of the input parameter "InputObject".
/// Specifies a ServiceController object that represents the service to change.
/// Enter a variable that contains the objects or type a command or expression
/// that gets the objects.
/// </summary>
[Parameter(Mandatory = true, ParameterSetName = "InputObject", Position = 0, ValueFromPipeline = true)]
public new ServiceController InputObject { get; set; }
/// <summary>
/// The following is the definition of the input parameter "DisplayName".
@ -1539,16 +1547,6 @@ namespace Microsoft.PowerShell.Commands
}
internal string serviceStatus = null;
/// <summary>
/// The following is the definition of the input parameter "InputObject".
/// Specifies ServiceController object representing the services to be stopped.
/// Enter a variable that contains the objects or type a command or expression
/// that gets the objects.
/// </summary>
[Parameter(ValueFromPipeline = true,
ParameterSetName = "InputObject")]
public new ServiceController InputObject { get; set; }
/// <summary>
/// This is not a parameter for this cmdlet.
/// </summary>
@ -1688,7 +1686,7 @@ namespace Microsoft.PowerShell.Commands
// Modify startup type or display name or credential
if (!String.IsNullOrEmpty(DisplayName)
|| (ServiceStartMode)(-1) != StartupType || null != Credential)
|| (ServiceStartMode)(-1) != StartupType || null != Credential)
{
DWORD dwStartType = NativeMethods.SERVICE_NO_CHANGE;
if (!NativeMethods.TryGetNativeStartupType(StartupType, out dwStartType))

View file

@ -102,7 +102,7 @@ Describe "Set/New/Remove-Service cmdlet tests" -Tags "Feature", "RequireAdminOnW
@{parameter = "StartupType" ; value = "Manual"},
@{parameter = "StartupType" ; value = "System"},
@{parameter = "Credential" ; value = (
[System.Management.Automation.PSCredential]::new("username",
[System.Management.Automation.PSCredential]::new("username",
#[SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Demo/doc/test secret.")]
(ConvertTo-SecureString "PlainTextPassword" -AsPlainText -Force)))
}
@ -214,7 +214,7 @@ Describe "Set/New/Remove-Service cmdlet tests" -Tags "Feature", "RequireAdminOnW
}
}
It "Remove-Service can accept pipeline input of a ServiceController" {
It "Remove-Service can accept a ServiceController as pipeline input" {
try {
$servicename = "testremoveservice"
$parameters = @{
@ -236,7 +236,7 @@ Describe "Set/New/Remove-Service cmdlet tests" -Tags "Feature", "RequireAdminOnW
{ Remove-Service -Name "testremoveservice" -ErrorAction 'Stop' } | ShouldBeErrorId "InvalidOperationException,Microsoft.PowerShell.Commands.RemoveServiceCommand"
}
It "Set-Service can accept pipeline input of a ServiceController" {
It "Set-Service can accept a ServiceController as pipeline input" {
try {
$servicename = "testsetservice"
$newdisplayname = "newdisplayname"
@ -255,9 +255,29 @@ Describe "Set/New/Remove-Service cmdlet tests" -Tags "Feature", "RequireAdminOnW
}
}
It "Set-Service can accept a ServiceController as positional input" {
try {
$servicename = "testsetservice"
$newdisplayname = "newdisplayname"
$parameters = @{
Name = $servicename;
BinaryPathName = "$PSHOME\powershell.exe"
}
$service = New-Service @parameters
$service | Should Not BeNullOrEmpty
$script = { Set-Service $service -DisplayName $newdisplayname }
{ & $script } | Should Not Throw
$service = Get-Service -Name $servicename
$service.DisplayName | Should BeExactly $newdisplayname
}
finally {
Get-CimInstance Win32_Service -Filter "name='$servicename'" | Remove-CimInstance -ErrorAction SilentlyContinue
}
}
It "Using bad parameters will fail for '<name>' where '<parameter>' = '<value>'" -TestCases @(
@{cmdlet="New-Service"; name = 'credtest' ; parameter = "Credential" ; value = (
[System.Management.Automation.PSCredential]::new("username",
[System.Management.Automation.PSCredential]::new("username",
#[SuppressMessage("Microsoft.Security", "CS002:SecretInNextLine", Justification="Demo/doc/test secret.")]
(ConvertTo-SecureString "PlainTextPassword" -AsPlainText -Force)));
errorid = "CouldNotNewService,Microsoft.PowerShell.Commands.NewServiceCommand"},
@ -271,7 +291,7 @@ Describe "Set/New/Remove-Service cmdlet tests" -Tags "Feature", "RequireAdminOnW
param($cmdlet, $name, $parameter, $value, $errorid)
$parameters = @{$parameter = $value; Name = $name; ErrorAction = "Stop"}
if ($cmdlet -eq "New-Service") {
$parameters += @{Binary = "$PSHOME\powershell.exe"};
$parameters += @{Binary = "$PSHOME\powershell.exe"};
}
{ & $cmdlet @parameters } | ShouldBeErrorId $errorid
}