PowerShell中的DateTime解析 [英] DateTime parsing in PowerShell

查看:459
本文介绍了PowerShell中的DateTime解析的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试编写一个PowerShell脚本,该脚本将生成一个包含两列的信息表:名称和日期(将从第三方应用程序中检索-在本例中为svn.exe)。 / p>

总体脚本运行良好,但我一直在努力获取服务器发送回DataTable的日期。这是我的脚本的简化版本:

 #设置数据表并初始化列
$ table = New-Object system.Data.DataTable测试表
$ col1 =新对象system.Data.DataColumn名称,([字符串])
$ col2 =新对象system.Data.DataColumn DateFromServer,([ DateTime])
$ table.columns.add($ col1)
$ table.columns.add($ col2)

#创建一个新行并添加数据
$ row = $ table.NewRow()
$ row.Name = Test
$ lastCommit = GetDateFromExternalApp
$ lastCommit.GetType()#这会返回DateTime,正如我期望的那样
$ row.DateFromServer = $ lastCommit#这将引发错误
$ table.Rows.Add($ row)

#输出表
$ table | Format-Table -AutoSize

#此函数模拟实际函数执行的功能
#(实际的函数转到SVN并提取数据,但它的
#最终以相同的结果日期)
函数GetDateFromExternalApp
{
$ externalAppDate = 2012-09-17T16:33:57.177516Z

return [DateTime]($ externalAppDate)
}

问题(上面脚本的注释中已注意到)是似乎很高兴返回DateTime实例,当我尝试将其添加到表行的DateFromServer列中时,抛出了错误:

 异常设置 DateFromServer:无法将类型为'System.Management.Automation.PSObject'的对象转换为类型为'System.IConvertible'。无法在< 18/09/2012 2:33:57 AM>中存储DateFromServer列。期望的类型为DateTime。在第13行:char:6 
+ $ row。 <<< DateFromServer = $ lastCommit
+ CategoryInfo:InvalidOperation:(:) [],RuntimeException
+ FullyQualifiedErrorId:PropertyAssignmentException

但是,对$ lastCommit.GetType()的调用显示这确实是一个DateTime-实际上,如果我在脚本中的以下位置添加此行:

  $ lastCommit 

然后我可以看到一个漂亮的格式化日期时间,表明它确实是在对其进行解析并正确转换:

 日期:18/09/2012 12:00: 00 AM 
日:18
DayOfWeek:星期二
DayOfYear:262
小时:2
种类:当地
毫秒:177
分钟: 33
月份:9
第二:57
刻度:634835324371775160
TimeOfDay:02:33:57.1775160
年份:2012
DateTime:9月18日,星期二2012 2:33:57 AM

对于为什么我遇到上述例外情况,我们深感困惑。我意识到PowerShell的函数返回值与C#有所不同,但是在我看来,函数正在返回正确的类型!

解决方案

我也想知道这种现象的原因。



无论如何,您可以通过将date变量转换为DateTime或将其明确声明为成为一个。像这样,

  ... 
$ row.DateFromServer = [DateTime] $ lastCommit#不抛出异常
...

  ... 
[DateTime] $ lastCommit = GetDateFromExternalApp
$ row.DateFromServer = $ lastCommit#不抛出异常
...


I'm trying to write a PowerShell script that will generate a table of information with two columns: a name, and a date (which will be retrieved from a third-party application - in this case svn.exe).

The overall script works well, but I'm struggling to get the date that the server sends back into the DataTable. Here's a simplified version of my script:

# 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)
}

The problem (noted in comments in the script above) is that while the function seems to be happily returning a DateTime instance, when I try to add this into the table row's DateFromServer column it's throwing up an error:

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

However, the call to $lastCommit.GetType() shows that this is indeed a DateTime - and in fact if I add this line somewhere in the script:

$lastCommit

then I can see a nicely formatted datetime, suggesting that it is indeed parsing it and converting it correctly:

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

As such I'm quite puzzled as to why I'm getting the exception above. I realise that PowerShell does function return values differently than C#, but it looks to me like the function is returning the right type!

解决方案

I'd like to know the reason for this behaviour too.

Anyway, you can work around the problem by casting the date variable as DateTime or by explicitly declaring it to be one. Like so,

...
$row.DateFromServer = [DateTime]$lastCommit # Doesn't throw exception
...

Or

...
[DateTime]$lastCommit = GetDateFromExternalApp
$row.DateFromServer = $lastCommit # Doesn't throw exception
...

这篇关于PowerShell中的DateTime解析的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆