捕获Powershell中变量方法的终止错误
我试图捕获调用方法时引起的错误。在下面的try块中,捕获Powershell中变量方法的终止错误,powershell,Powershell,我试图捕获调用方法时引起的错误。在下面的try块中,Set Aclcmdlet支持ErrorAction并允许我捕获。方法.AddAccessRule不支持-ErrorAction,但如果用户不存在,则有时会失败。我怎样才能抓住这个错误 Try{ $acl.AddAccessRule($rule) Set-Acl $homeDirectory $acl -ErrorAction Stop -ErrorVariable errvar }Catch{ Log $NewUserL
Set Acl
cmdlet支持ErrorAction
并允许我捕获。方法.AddAccessRule
不支持-ErrorAction
,但如果用户不存在,则有时会失败。我怎样才能抓住这个错误
Try{
$acl.AddAccessRule($rule)
Set-Acl $homeDirectory $acl -ErrorAction Stop -ErrorVariable errvar
}Catch{
Log $NewUserLog $errvar
$Response = [System.Windows.Forms.MessageBox]::Show("Unable to set security on user Folder. Please set NTFS permisions manually." + $ErrorText, "Error")
}
catch块应捕获AddAccessRule引发的.NET异常。由于您尚未在catch块上指定筛选异常,因此它应该捕获所有“catchable”.NET异常。有一些异常没有捕获IIRC,比如StackOverflowException