clarification about parameter binding

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[]

 

 

This is a educated guess, but most likely it attempts a conversion with ToString().

PS C:\Users\rasim> $file = Get-Item -Path C:\Scripts\apps.xml


PS C:\Users\rasim> $file.GetType()


IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     FileInfo                                 System.IO.FileSystemInfo



PS C:\Users\rasim> $file.ToString()

C:\Scripts\apps.xml

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:
StackOverflow Thread: https://tinyurl.com/y4v4veum

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.

yes exactly, its a string conversion and AFAIK, done with this internally [System.Management.Automation.LanguagePrimitives]::ConvertTo($Path,‘string’)

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.

I can’t edit the last post.

@js2010 Fixed. There was a long url text in Rob’s post making the posts non editable. I just removed it.