2

I'm trying to run commands on a remote host and use the results. In this case, "Get-Item". (In my "real" code, I call Get-Item several times and return a custom object with several items - but that doesn't matter for this example).

I've run into this problem where if I return the results from Get-Item (A System.IO.FileSystemInfo.FileInfo), the "VersionInfo" gets turned into a string, so I cannot reference $result.VersionInfo.FileVersion.

Example below -- "$b" is the whole get-item result, while "$a" is (Get-Item).VersionInfo.

"$a.FileVersion" is a string as expected. But, why is "$b.VersionInfo" a string instead of an object with some properties?

PS C:\Tests> $b=invoke-command -Session $Session -ScriptBlock { get-item "C:\SomeThing.dll"}
PS C:\Tess> $b.versioninfo.gettype()
IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     String                                   System.Object




PS C:\Tests> $a=invoke-command -Session $Session -ScriptBlock { (get-item "C:\SomeThing.dll").VersionINfo}
PS C:\Tests> $a
ProductVersion   FileVersion      FileName
--------------   -----------      --------
2.9.0            2.9.0            C:\SomeThing.dll

PS C:\Tests> $a.gettype()
IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     PSObject                                 System.Object


PS C:\Tests> $a | get-member
   TypeName: Deserialized.System.Diagnostics.FileVersionInfo

Name               MemberType   Definition
----               ----------   ----------
FileVersion        Property     System.String {get;set;}
joeking
  • 2,006
  • 18
  • 29
  • 3
    In short, yes. The object is no longer live. For example: capture the process of your processes running and export to XML. Now re-import them. Powershell (*behind the scenes*) does the exact same thing. It *serializes* the data, then returns it back to you *deserialized* as shown. Basically a snapshot with no direct methods pertaining to the object itself. – Abraham Zinala Jul 07 '21 at 00:37
  • 3
    To elaborate on @AbrahamZinala's comment: I hope the [accepted answer](https://stackoverflow.com/a/59180367/45375) to the linked duplicate answers all questions. – mklement0 Jul 07 '21 at 00:46

0 Answers0