我有具有以下值的csv文件:
User,TimeStamp
Pinky,11/4/2015 5:00
Brain,
Leo,never
Don,unspecified我希望确保TimeStamp列的文件具有日期或$null值。为此,我使用以下代码:
Function HealthCheckTimeStampColumn
{
Param ($userInputCsvFileWithPath)
Write-Host "Checking if TimeStamp column has invalid values..."
Import-Csv $userInputCsvFileWithPath | %{
if ($_.TimeStamp)
{
Try
{
([datetime]$_.TimeStamp).Ticks | Out-Null
}
Catch [system.exception]
{
$Error.Clear()
$invalidValue = $_.TimeStamp
Write-Error "Invalid Value found `"$_.TimeStamp`"; Value expected Date or `"`""
Exit
}
}
}
Write-Host "All values were found valid."
Write-Host "TimeStamp Healthcheck Column Passed"
Write-Host ""
}通过这段代码,我得到了以下错误:
Invalid Value found "Cannot convert value "never" to type "System.DateTime". Error: "The string was not recognized as
a valid DateTime. There is an unknown word starting at index 0.".TimeStamp"; Value expected Date or ""
At C:\Scripts\Tests\TestTime.ps1:247 char:42
+ Import-Csv $userInputCsvFileWithPath | %{
+ ~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException如果我尝试这一行代码,而不是:
Write-Error "Invalid Value found `"$invalidValue`"; Value expected Date or `"`""我知道这个错误:
Invalid Value found ""; Value expected Date or ""
At C:\Scripts\Tests\TestTime.ps1:247 char:42
+ Import-Csv $userInputCsvFileWithPath | %{
+ ~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException我希望看到的错误是:
Invalid Value found "never"; Value expected Date or ""
At C:\Scripts\Tests\TestTime.ps1:247 char:42
+ Import-Csv $userInputCsvFileWithPath | %{
+ ~
+ CategoryInfo : NotSpecified: (:) [Write-Error], WriteErrorException
+ FullyQualifiedErrorId : Microsoft.PowerShell.Commands.WriteErrorException有人能告诉我我做错了什么吗?
发布于 2016-01-12 22:16:46
您也不需要try/catch块来实现这一功能。他们是好的,意外和不可避免的错误。然而,从运算符的角度来看,您将看到-as和-is非常优雅地处理了这种情况。
-is:当输入是指定.NET框架类型的实例时返回TRUE。 -as:将输入转换为指定的.NET框架类型。
当-as遇到一个字符串或一些不可日期转换的东西时,它将返回一个null。更重要的是,它不会出错。我建议您检查非空日期和无效日期时间的所有值。捕获变量中的所有这些。然后检查变量是否有任何值。立刻打印出来!如果你愿意就退出。我也支持用户2460798的答案关于出口的使用的说法。
Function HealthCheckTimeStampColumn{
Param ($userInputCsvFileWithPath)
$badRows = Import-Csv $userInputCsvFileWithPath |
Where-Object{-not [string]::IsNullOrEmpty($_.TimeStamp) -and ($_.TimeStamp -as [datetime]) -eq $null}
if($badRows){
$badRows | ForEach-Object{
Write-Host "'$($_.Timestamp)' is not a valid datetime" -ForegroundColor Red
}
Write-Error "$($badRows.Count) Invalid Value(s) found"
} else {
"All values were found valid.","TimeStamp Healthcheck Column Passed","" | Write-Host
}
}发布于 2016-01-12 19:11:31
将PerSerAl的观察转化为一个答案:
$_的含义从它在foreach对象循环中的时候(但外部catch块)更改到它在集水区块中的时候。在第一种情况下,它是当前对象(行),它的时间戳显然具有“从不”的值。但是在catch块中,是由于错误而生成的错误记录。因此,要修复:
Function HealthCheckTimeStampColumn
{
Param ($userInputCsvFileWithPath)
Write-Host "Checking if TimeStamp column has invalid values..."
Import-Csv $userInputCsvFileWithPath | %{
$row = $_
if ($_.TimeStamp)
{
Try
{
([datetime]$_.TimeStamp).Ticks | Out-Null
}
Catch [system.exception]
{
$Error.Clear()
$invalidValue = $_.TimeStamp
Write-Error "Invalid Value found `"$row.TimeStamp`"; Value expected Date or `"`""
Exit
}
}
}
Write-Host "All values were found valid."
Write-Host "TimeStamp Healthcheck Column Passed"
Write-Host ""
}顺便说一句,如果您想处理整个文件,就需要从catch块中删除exit。
https://stackoverflow.com/questions/34751101
复制相似问题