Get-Service parameter confusion

Hi all,

I noticed something strange. How is that I can do the following

Get-Service -Name ‘Alg’ (the parameter name accepts datatype string so this works)

But why does

Get-Service -InputObject ‘alg’ also work? (this parameter should not accept a string but a servicecontroller but still it works fine? What is happening under the hood here?

 

Kind regards,

Leo

 

You can coerce a string into a servicecontroller:

[System.ServiceProcess.ServiceController]'appxsvc'

Status   Name               DisplayName
------   ----               -----------
Running  appxsvc            AppX Deployment Service (AppXSVC)

Thank you a lot js but are you saying that is happening? Where is this documented I wonder. According to website Microsoft it just wants servicecontroller?

-InputObject should just be accepting a string right? Get-Process gives an error when you try this with -inputobject as expected.

So can you or anyone else tell me what is happening?

Thank you

Leo

 

 

 

… and … Powershell is made for administrators - not software developers. Powershell always “tries” to give an administrator what he/she/it probably expects. It does a lot of stuff implicitly under the hood to give a satisfying experience.

The docu says:
Specifies ServiceController objects representing the services to be retrieved. Enter a variable that contains the objects, or type a command or expression that gets the objects. You can pipe a service object to this cmdlet.

Hi Olaf,

So that means it does something it’s not supposed to do according to website but does it anyway? I’m grateful for your answer but if someone would tell me that it’s not supposed to do that but we don’t know why it does this and noone knows then I can accept that as an answer. Is there a command that I can see what it’s doing? Like a debug command? Maybe what js is describing that it does some coercion?

thanks again,

Leo

Powershell is very fluid with types. If it’s expecting one type, and it can convert what you give it to what it expects, it will do that. You can’t cast a string to a process in the same way.

[System.Diagnostics.Process]'cmd'

InvalidArgument: Cannot convert the "cmd" value of type "System.String" to type "System.Diagnostics.Process".

“COERCE arg to [System.ServiceProcess.ServiceController]”

Trace-Command -Name ParameterBinding -Expression { get-service -InputObject appxsvc } -PSHost

DEBUG: 2020-04-03 17:56:50.3916 ParameterBinding Information: 0 : BIND NAMED cmd line args [Get-Service]
DEBUG: 2020-04-03 17:56:50.3956 ParameterBinding Information: 0 :     BIND arg [appxsvc] to parameter [InputObject]
DEBUG: 2020-04-03 17:56:50.3976 ParameterBinding Information: 0 :         COERCE arg to [System.ServiceProcess.ServiceController[]]
DEBUG: 2020-04-03 17:56:50.3995 ParameterBinding Information: 0 :             Trying to convert argument value from System.String to System.ServiceProcess.ServiceController[]
DEBUG: 2020-04-03 17:56:50.4015 ParameterBinding Information: 0 :             ENCODING arg into collection
DEBUG: 2020-04-03 17:56:50.4033 ParameterBinding Information: 0 :             Binding collection parameter InputObject: argument type [String], parameter type [System.ServiceProcess.ServiceController[]], collection type Array, element type [System.ServiceProcess.ServiceController], coerceElementType
DEBUG: 2020-04-03 17:56:50.4048 ParameterBinding Information: 0 :             Creating array with element type [System.ServiceProcess.ServiceController] and 1 elements
DEBUG: 2020-04-03 17:56:50.4065 ParameterBinding Information: 0 :             Argument type String is not IList, treating this as scalar
DEBUG: 2020-04-03 17:56:50.4116 ParameterBinding Information: 0 :             COERCE arg to [System.ServiceProcess.ServiceController]
DEBUG: 2020-04-03 17:56:50.4135 ParameterBinding Information: 0 :                 Trying to convert argument value from System.String to System.ServiceProcess.ServiceController
DEBUG: 2020-04-03 17:56:50.4150 ParameterBinding Information: 0 :                 CONVERT arg type to param type using LanguagePrimitives.ConvertTo
DEBUG: 2020-04-03 17:56:50.4177 ParameterBinding Information: 0 :                 CONVERT SUCCESSFUL using LanguagePrimitives.ConvertTo: [System.ServiceProcess.ServiceController]
DEBUG: 2020-04-03 17:56:50.4218 ParameterBinding Information: 0 :             Adding scalar element of type ServiceController to array position 0
DEBUG: 2020-04-03 17:56:50.4244 ParameterBinding Information: 0 :         Executing VALIDATION metadata: [System.Management.Automation.ValidateNotNullOrEmptyAttribute]
DEBUG: 2020-04-03 17:56:50.4267 ParameterBinding Information: 0 :         BIND arg [System.ServiceProcess.ServiceController[]] to param [InputObject] SUCCESSFUL
DEBUG: 2020-04-03 17:56:50.4293 ParameterBinding Information: 0 : BIND POSITIONAL cmd line args [Get-Service]
DEBUG: 2020-04-03 17:56:50.4317 ParameterBinding Information: 0 : MANDATORY PARAMETER CHECK on cmdlet [Get-Service]
DEBUG: 2020-04-03 17:56:50.4357 ParameterBinding Information: 0 : CALLING BeginProcessing
DEBUG: 2020-04-03 17:56:50.8644 ParameterBinding Information: 0 : CALLING EndProcessing

Status   Name               DisplayName
------   ----               -----------
Running  appxsvc            AppX Deployment Service (AppXSVC)

aaaah so basically if it can convert to a type it needs it will do that. This makes sense! Is there a command that shows the conversion it is doing like a debug command?

But thanks JS that is already a big part of the puzzle. Would be just great if we could see it in action as to say.

thanks

leo

 

perfect! I get it thanks a lot!