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.
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.
Base on the 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.