Quantcast
Channel: PowerShell.org » All Posts
Viewing all articles
Browse latest Browse all 13067

Reply To: positional parameters and parameter sets

$
0
0

Nice catch, but it’s not quite what you think. When you call get-service ‘background intelligent transfer service’ , it really is binding to the Name parameter, not the DisplayName (which you can verify with Trace-Command, as shown below).

Trace-Command -Name ParameterBinding -PSHost -Expression { Get-Service 'Background Intelligent Transfer Service' }

DEBUG: ParameterBinding Information: 0 : BIND NAMED cmd line args [Get-Service]
DEBUG: ParameterBinding Information: 0 : BIND POSITIONAL cmd line args [Get-Service]
DEBUG: ParameterBinding Information: 0 :     BIND arg [Background Intelligent Transfer Service] to parameter [Name]
DEBUG: ParameterBinding Information: 0 :         Binding collection parameter Name: argument type [String], parameter type [System.String[]], collection type Array, element type [System.String], no coerceElementType
DEBUG: ParameterBinding Information: 0 :         Creating array with element type [System.String] and 1 elements
DEBUG: ParameterBinding Information: 0 :         Argument type String is not IList, treating this as scalar
DEBUG: ParameterBinding Information: 0 :         Adding scalar element of type String to array position 0
DEBUG: ParameterBinding Information: 0 :         BIND arg [System.String[]] to param [Name] SUCCESSFUL
DEBUG: ParameterBinding Information: 0 : MANDATORY PARAMETER CHECK on cmdlet [Get-Service]
DEBUG: ParameterBinding Information: 0 : CALLING BeginProcessing
DEBUG: ParameterBinding Information: 0 : CALLING EndProcessing

It’s just that the Get-Service cmdlet is written to work with either value passed to the Name argument, for some reason, but only if the Name argument does not contain any wildcards. You won’t find that in the documentation anywhere, and it may not have even been a conscious design choice by the PowerShell team, as it’s actually behavior from the underlying ServiceController class. See http://msdn.microsoft.com/en-us/library/ssbk2tf3(v=vs.110).aspx; you’ll see that the “name” parameter can be either the name or display name of the service, and this is the constructor that Get-Service calls if you pass it a Name without wildcards.


Viewing all articles
Browse latest Browse all 13067

Trending Articles