为什么Get Date似乎返回DateTime对象,但BinarySerializer指示它返回PSObject?

为什么Get Date似乎返回DateTime对象,但BinarySerializer指示它返回PSObject?,datetime,powershell,binary-serialization,psobject,Datetime,Powershell,Binary Serialization,Psobject,举个简单的例子: 键StartDate似乎包含一个 然而,如果我试图对它执行二进制序列化,我会收到一个异常,它抱怨不能序列化 $ms = New-Object System.IO.MemoryStream $bf = New-Object System.Runtime.Serialization.Formatters.Binary.BinaryFormatter $bf.Serialize($ms, $data) $ms.Close() 抛出: DocumentsException calli

举个简单的例子:

键StartDate似乎包含一个

然而,如果我试图对它执行二进制序列化,我会收到一个异常,它抱怨不能序列化

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

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
因此,Get Date是否真的没有返回日期时间,或者其他一些powershell奇怪之处是否也在起作用。

基于:

PSO对象类:

封装object或PSCustomObject类型的基本对象,以允许Windows PowerShell环境中任何对象的一致视图

( 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

PowerShell中的每个对象实际上都透明地包装在一个文件中。我说的大部分是透明的,因为PowerShell中有不止几个bug在将对象泄漏到另一个API之前忽略了移除包装器。这会引起各种各样的问题,就像你现在看到的那样。搜索connect.microsoft.com/powershell以查找PSObject包装器。我相信这在基于新引擎的v3中不再是一个问题。

我认为PS中的所有内容都默认为
PSObject
,除非您明确定义类型。它进行动态数据类型转换,因此它可能只是返回一个字符串,在运行时它将该字符串解释为
System.Datetime
。@JNK但为什么Object.GetType()将某个对象解释为二进制格式化程序解释为PSObject的日期时间?这只是猜测,但可能是因为它在运行时被解释(因为您正在运行PSObject的
GetType()
方法)-二进制格式化程序是一个系统类,而不是PS类(获取日期“2002-03-23”)-是[PSObject]真--(获取日期“2002-03-23”)-是[datetime]真--[datetime](获取日期“2002-03-23”)-是[datetime]真--[datetime](获取日期“2002-03-23”)-是[PSObject] False@Christian我想这是一个答案,发布它,我会投票。刚刚启动VirtualBox,代码就可以了。谢谢你的澄清。这里是另一个psobject搞糟事情的例子;
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
$data = @{
    First = 'Justin';
    Last = 'Dearing';
    StartDate = [DateTime] (Get-Date '2002-03-23');
}
( 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