【发布时间】:2013-06-06 18:17:53
【问题描述】:
我正在尝试编写一个 powershell 脚本来读取文件并在它是有效的 JSON 文件时打印“true”。我正在使用 Powershell v3.0,这就是我现在所拥有的:
$text = Get-Content .\filename.txt -Raw
$powershellRepresentation = $text | ConvertFrom-Json
如何查看返回码?我的意思是我想要这样的东西:
if(file not a JSON file){
Write-Host "not JSON"
}
else{
Write-Host "True"
}
【问题讨论】:
-
我可以通过捕获异常来做到这一点。但是有没有其他办法?
-
在 PowerShell 6.0 中,Test-Json Cmdlet 可用。 docs.microsoft.com/en-us/powershell/module/…
标签: json powershell try-catch powershell-3.0