Welcome › Forums › General PowerShell Q&A › clarification about parameter binding
- This topic has 6 replies, 4 voices, and was last updated 3 months, 1 week ago by
Senior Moderator.
-
AuthorPosts
-
-
October 13, 2020 at 10:29 am #262958
both $source and $dest are System.IO.FileInfo objects. I can see through trace-command that it’s taking the value of “C:\foo.log” and converting it to string (the input type for -path). the question is how does it get the value of c:\foo.log. How does it know which of $source’s parameters to use.
Trace-Command ParameterBinding -Expression {Copy-Item -Path $source -Destination $dest -force} -PSHost
DEBUG: ParameterBinding Information: 0 : BIND NAMED cmd line args [Copy-Item]
DEBUG: ParameterBinding Information: 0 : BIND arg [C:\foo.log] to parameter [Path]
DEBUG: ParameterBinding Information: 0 : COERCE arg to [System.String[]]
DEBUG: ParameterBinding Information: 0 : Trying to convert argument value from System.IO.FileInfo to
System.String[] -
October 13, 2020 at 1:39 pm #263004
This is a educated guess, but most likely it attempts a conversion with ToString().
PowerShell123456789101112131415PS C:\Users\rasim> $file = Get-Item -Path C:\Scripts\apps.xmlPS C:\Users\rasim> $file.GetType()IsPublic IsSerial Name BaseType-------- -------- ---- --------True True FileInfo System.IO.FileSystemInfoPS C:\Users\rasim> $file.ToString()C:\Scripts\apps.xmlAs to what ToString() is doing, it appears to get the Fully Qualified Name, which in this case is the FullName. More information about ToString can be found here:
StackOverflow Thread: https://tinyurl.com/y4v4veum -
October 13, 2020 at 4:15 pm #263049
As to what ToString() is doing, it appears to get the Fully Qualified Name, which in this case is the FullName. More information about ToString can be found here
https://docs.microsoft.com/en-us/dotnet/api/system.object.tostring?view=netcore-3.1 talks more about it.
I never did real dev so don’t know how methods are implemented, overloading, overriding etc. But I got the gist of the idea. It wasn’t binding to any specific parameter but converting the object to a string.
thanks for the help.
-
October 14, 2020 at 10:32 am #263301
yes exactly, its a string conversion and AFAIK, done with this internally [System.Management.Automation.LanguagePrimitives]::ConvertTo($Path,’string’)
-
October 15, 2020 at 11:46 am #263579
The words are cutoff in my browser.
I think you mean which of the $source properties. “-path” of of type string, so powershell casts or coerces $source to type string.
-
October 15, 2020 at 11:48 am #263585
I can’t edit the last post.
-
October 16, 2020 at 12:33 am #263786
-
-
AuthorPosts
- The topic ‘clarification about parameter binding’ is closed to new replies.