lit
lit

Reputation: 16256

Difference in pipeline type

Why does:

Invoke-Command -ScriptBlock { Get-Service } -ComputerName SERVER01 | Get-Member

return TypeName: Deserialized.System.ServiceProcess.ServiceController

But:

Invoke-Command -ScriptBlock { Get-Service } -ComputerName SERVER01 | % { $_.GetType() }

returns a type Name of PSObject and BaseType of System.Object?

Edit: I understand that the object that comes back from Invoke-Command is Deserialized. It just seems odd that the same object is reported as a different type by GetType() and Get-Member.

PS C:\src\t> $gss = Invoke-Command -ScriptBlock { Get-Service } -ComputerName SERVER01

PS C:\src\t> $gss[0].GetType()

IsPublic IsSerial Name                                     BaseType
-------- -------- ----                                     --------
True     True     PSObject                                 System.Object

PS C:\src\t> $gss[0] | Get-Member

   TypeName: Deserialized.System.ServiceProcess.ServiceController

Upvotes: 0

Views: 134

Answers (1)

Persistent13
Persistent13

Reputation: 1562

When you run a command that returns an object over a remote session (which you imply by using the -ComputerName parameter even if it is preformed against the same computer you ran the command from) PowerShell will convert the object to a PSObject that contains all properties of the original object but without any of its methods. You can verify when this is happening due to the Deserialized prefix returned by Get-Member. The reason this happens is because PowerShell cannot run a method on a remote object. You will see methods that can be run locally such as ToString(), however.

Only a handful of primitive types and almost-primitive types are truly brought across and will not receive this conversion. Below is not an extensive list but examples that have been given.

  • Byte, SByte, Byte[]
  • Int16, Int32, Int64, UInt16, UInt32, UInt64
  • Decimal, Single, Double
  • TimeSpan, DateTime, ProgressRecord
  • Char, String, XmlDocument, SecureString
  • Boolean, Guid, Uri, Version
  • Enums

More on this behavior can be found here.

Upvotes: 1

Related Questions