【发布时间】:2019-04-17 03:46:34
【问题描述】:
当get-addomain 成功时,我正在尝试写入输出。
Try/catch 仅在命令失败时写入输出
try {
get-addomain -Identity d.contoso.com
}
catch {
Write-Output "failed"
}
我尝试了以下操作:
if (-not (get-addomain -Identity d.contoso.com))
{
return "failed"
}
else
{
write-output "ok"
}
和
If (get-addomain -Identity d.contoso.com )
{
Write-Output "ok"
}
Else
{
write-output "failed"
}
但在这两种情况下都得到了
get-addomain : Cannot find an object with identity: 'd.contoso.com' under: 'DC=ad,DC=contoso,DC=com'.
【问题讨论】:
标签: powershell error-handling try-catch