Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/powershell/13.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Datetime PowerShell中的日期时间解析_Datetime_Powershell - Fatal编程技术网

Datetime PowerShell中的日期时间解析

Datetime PowerShell中的日期时间解析,datetime,powershell,Datetime,Powershell,我正在尝试编写一个PowerShell脚本,该脚本将生成一个包含两列的信息表:名称和日期(将从第三方应用程序检索,本例中为svn.exe) 整个脚本运行得很好,但我很难获得服务器发送回数据表的日期。以下是我的脚本的简化版本: # Set up a DataTable and initialise columns $table = New-Object system.Data.DataTable “Test Table” $col1 = New-Object system.Data.DataCol

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

整个脚本运行得很好,但我很难获得服务器发送回数据表的日期。以下是我的脚本的简化版本:

# 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
然后我可以看到一个格式很好的datetime,表明它确实在解析它并正确地转换它:

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#不同,但在我看来,函数返回的类型是正确的

我也想知道这种行为的原因

无论如何,您可以通过将日期变量强制转换为DateTime或显式声明为DateTime来解决这个问题。这样,

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


我怀疑原因如错误消息中所述:

“无法将类型为'System.Management.Automation.PSObject'的对象强制转换为类型为'System.IConvertible'…预期类型为DateTime。”第13行字符:6

这说明PowerShell试图将$lastCommit转换为日期和时间,但失败了。最好的方法是,正如vonPryz所建议的那样,将它投给[日期时间]

要更清楚地看到问题,请尝试查看以下两个方面: $lastcommit.gettype()和 $row.DateFromServer.gettype()


我没有GetDateFromExternalApp来检查

这里的问题是由于PowerShell的类型自适应系统中存在错误造成的(如果您还没有这样做,我建议您这样做)

在PowerShell中使用对象时,实际上是在使用包装器。大多数调用(如)都转发到基础对象,不过您可以添加不与对象本身交互的其他成员:

PS>Get Date |添加成员NoteProperty Yowza'例如'-PassThru |格式列表y*
尤扎:比如说
年份:2012年

通常这不是问题,因为PowerShell具有广泛的类型强制功能。但是,在的情况下,用于支持属性访问语法的
DataRowAdapter
类型似乎存在错误。如果直接使用,
$row['DateFromServer']=$lastCommit
而不是
$row.DateFromServer
,则在将值发送到.NET类型之前,该值将正确展开


您还可以通过使用强制转换(如:
[DateTime]$lastCommit
)或通过检索(
$lastCommit.PSObject.BaseObject
)自行展开值来解决此问题。

谢谢!这很有效。是的,这对我来说似乎也不符合逻辑,但我对PowerShell还是新手,因此不确定标准等等。问题的示例代码包括一个复制问题的伪GetDateFromExternalApp函数。使用此GetDateFromExternalApp不会在此处复制问题。运行这段代码会给我以下信息:Name DateFromServer--------------------Test 9/17/2012 5:33:57 PMIt在我的系统上也会这样做。你用的是哪个PSH版本?函数运行正常,您是否也尝试了$row.DateFromServer=$lastCommit部分?谢谢!把get date放入数据表的问题一直困扰着我,这就解决了这个问题。
...
$row.DateFromServer = [DateTime]$lastCommit # Doesn't throw exception
...
...
[DateTime]$lastCommit = GetDateFromExternalApp
$row.DateFromServer = $lastCommit # Doesn't throw exception
...