diff --git a/src/Microsoft.PowerShell.Commands.Management/commands/management/Service.cs b/src/Microsoft.PowerShell.Commands.Management/commands/management/Service.cs index 568d1fe44ad..293778b74d5 100644 --- a/src/Microsoft.PowerShell.Commands.Management/commands/management/Service.cs +++ b/src/Microsoft.PowerShell.Commands.Management/commands/management/Service.cs @@ -1436,7 +1436,7 @@ public class SetServiceCommand : ServiceOperationBaseCommand /// service name /// /// - [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 @@ public class SetServiceCommand : ServiceOperationBaseCommand } internal String serviceName = null; + /// + /// 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. + /// + [Parameter(Mandatory = true, ParameterSetName = "InputObject", Position = 0, ValueFromPipeline = true)] + public new ServiceController InputObject { get; set; } /// /// The following is the definition of the input parameter "DisplayName". @@ -1539,16 +1547,6 @@ public string Status } internal string serviceStatus = null; - /// - /// 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. - /// - [Parameter(ValueFromPipeline = true, - ParameterSetName = "InputObject")] - public new ServiceController InputObject { get; set; } - /// /// This is not a parameter for this cmdlet. /// @@ -1688,7 +1686,7 @@ protected override void ProcessRecord() // 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)) diff --git a/test/powershell/Modules/Microsoft.PowerShell.Management/Set-Service.Tests.ps1 b/test/powershell/Modules/Microsoft.PowerShell.Management/Set-Service.Tests.ps1 index 87312d9ae7d..3776ad5d1b8 100644 --- a/test/powershell/Modules/Microsoft.PowerShell.Management/Set-Service.Tests.ps1 +++ b/test/powershell/Modules/Microsoft.PowerShell.Management/Set-Service.Tests.ps1 @@ -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 '' where '' = ''" -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 }