我正在尝试编写一个 PowerShell 脚本,它将生成一个包含两列的信息表:名称和日期(将从第三方应用程序中检索 - 在本例中为 svn.exe)。
整个脚本运行良好,但我正在努力获取服务器发送回 DataTable 的日期。这是我的脚本的简化版本:
# Set up a DataTable and initialise columns
$table = New-Object system.Data.DataTable “Test Table”
$col1 = New-Object system.Data.DataColumn Name,([string])
$col2 = New-Object system.Data.DataColumn DateFromServer,([DateTime])
$table.columns.add($col1)
$table.columns.add($col2)
# Create a new row and add the data
$row = $table.NewRow()
$row.Name = "Test"
$lastCommit = GetDateFromExternalApp
$lastCommit.GetType() # this returns DateTime as I would expect
$row.DateFromServer = $lastCommit # this throws up an error
$table.Rows.Add($row)
# Output the table
$table | Format-Table -AutoSize
# This function simulates what the actual function does
# (the real one goes to SVN and pulls down data, but it
# ends up with the same resulting date)
Function GetDateFromExternalApp
{
$externalAppDate = "2012-09-17T16:33:57.177516Z"
return [DateTime]($externalAppDate)
}
问题(在上面脚本的注释中指出)是,虽然该函数似乎很高兴返回一个 DateTime 实例,但当我尝试将其添加到表行的 DateFromServer 列中时,它会抛出一个错误:
Exception setting "DateFromServer": "Unable to cast object of type 'System.Management.Automation.PSObject' to type 'System.IConvertible'.Couldn't store <18/09/2012 2:33:57 AM> in DateFromServer Column. Expected type is DateTime." At line:13 char:6
+ $row. <<<< DateFromServer = $lastCommit
+ CategoryInfo : InvalidOperation: (:) [], RuntimeException
+ FullyQualifiedErrorId : PropertyAssignmentException
但是,对 $lastCommit.GetType() 的调用表明这确实是一个 DateTime - 事实上,如果我在脚本中的某处添加这一行:
$lastCommit
然后我可以看到一个格式很好的日期时间,表明它确实在解析它并正确转换它:
Date : 18/09/2012 12:00:00 AM
Day : 18
DayOfWeek : Tuesday
DayOfYear : 262
Hour : 2
Kind : Local
Millisecond : 177
Minute : 33
Month : 9
Second : 57
Ticks : 634835324371775160
TimeOfDay : 02:33:57.1775160
Year : 2012
DateTime : Tuesday, 18 September 2012 2:33:57 AM
因此,我对为什么会出现上述异常感到非常困惑。我意识到 PowerShell 的函数返回值与 C# 不同,但在我看来,函数返回的是正确的类型!