是否可以检查 powershell 命令是否成功?
例子:
Set-CASMailbox -Identity:blocks.5 -OWAMailboxPolicy "DoNotExists"
导致错误:
Outlook Web App mailbox policy "DoNotExists" wasn't found. Make sure you typed the policy name correctly.
+ CategoryInfo : NotSpecified: (0:Int32) [Set-CASMailbox], ManagementObjectNotFoundException
+ FullyQualifiedErrorId : 9C5D12D1,Microsoft.Exchange.Management.RecipientTasks.SetCASMailbox
我认为应该可以获取FullyQualifiedErrorId,所以我尝试了以下方法:
$test = Set-CASMailbox -Identity:blocks.5 -OWAMailboxPolicy "DoNotExists"
但看起来错误没有转移到测试变量中。
那么这里执行以下操作的正确方法是什么:
$test = Set-CASMailbox -Identity:blocks.5 -OWAMailboxPolicy "DoNotExists"
if ($test -eq "error")
{
Write-Host "The Set-CASMailbox command failed"
}
else
{
Write-Host "The Set-CASMailbox command completed correctly"
}