이름과 날짜 (타사 응용 프로그램 (이 경우 svn)에서 검색되는 두 개의 열이있는 정보 테이블을 생성하는 PowerShell 스크립트를 작성하려고합니다. exe).PowerShell에서 DateTime 구문 분석
전체 스크립트가 제대로 작동하지만 서버가 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)
}
(위의 스크립트에 주석에서 언급) 문제는 그 기능이 나는 테이블 행의이 점을 추가하려고하면 행복하게하는 날짜 시간 인스턴스를 반환 할 것 같다 동안 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()에 대한 호출이 참으로 날짜 시간임을 보여줍니다 - 사실 나는 스크립트 어딘가에 줄을 추가하는 경우 :
$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#과 다르게 반환 값을 함수로 처리한다는 것을 알지만, 함수가 올바른 형식을 반환하는 것처럼 보입니다.
고마워요! 그 일은 훌륭했습니다. 예, 나에게도 논리적으로 보이지는 않지만, PowerShell을 처음 접했을 때 표준에 대한 확신이 없었습니다. – John