Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Why does Get-Date seem to return DateTime objects, but the BinarySerializer indicate it returns a PSObject?

Take the simple HashTable:

$data = @{
    First = 'Justin';
    Last = 'Dearing';
    StartDate = Get-Date '2002-03-23';
}

The key StartDate seems to contain a DateTime.

C:\Users\zippy\Documents> $data.StartDate.GetType().FullName
System.DateTime

However, if I attempt to perform binary serialization on it, I get an exception complaining that PSObject is not serializable.

$ms = New-Object System.IO.MemoryStream
$bf = New-Object System.Runtime.Serialization.Formatters.Binary.BinaryFormatter
$bf.Serialize($ms, $data)
$ms.Close()

Throws:

DocumentsException calling "Serialize" with "2" argument(s): "Type 'System.Management.Automation.PSObject' in Assembly 'System.Management.Automation, Versio
n=1.0.0.0, Culture=neutral, PublicKeyToken=31bf3856ad364e35' is not marked as serializable."
At C:\Users\jdearing\AppData\Local\Temp\b8967f99-0a24-41f7-9c97-dad2bc288bd9.ps1:12 char:14
+ $bf.Serialize <<<< ($ms, $data)
    + CategoryInfo          : NotSpecified: (:) [], MethodInvocationException
    + FullyQualifiedErrorId : DotNetMethodException

This message goes away and everything works if I use an explicit cast to [DateTime] like so:

$data = @{
    First = 'Justin';
    Last = 'Dearing';
    StartDate = [DateTime] (Get-Date '2002-03-23');
}

So is Get-Date not really returning a DateTime, or is some other powershell oddity at work here.

like image 876
Justin Dearing Avatar asked Mar 09 '12 15:03

Justin Dearing


2 Answers

Based on MSDN:

PSOobject Class:

Encapsulates a base object of type Object or type PSCustomObject to allow for a consistent view of any object within the Windows PowerShell environment.

( get-Date '2002-03-23' ) -IS [psobject]
True

( get-Date '2002-03-23' ) -IS [datetime]
True

[datetime]( get-Date '2002-03-23' ) -IS [datetime]
True

[datetime]( get-Date '2002-03-23' ) -IS [psobject]
False
like image 164
CB. Avatar answered Nov 13 '22 01:11

CB.


Every object in PowerShell is actually wrapped mostly transparently in a PSObject. I say mostly transparently, because there are more than a few bugs in PowerShell that omit to remove the wrapper before leaking the object to another API. This causes all sorts of issues, much like the one you see now. Search connect.microsoft.com/powershell for PSObject wrapper. I believe this is no longer an issue in v3 with the new DLR-based engine.

like image 34
x0n Avatar answered Nov 13 '22 02:11

x0n