为什么 Get-SPWeb 中的错误不会传播到 powershell? [英] Why don't errors rasied inside Get-SPWeb get propagated to powershell?

查看:21
本文介绍了为什么 Get-SPWeb 中的错误不会传播到 powershell?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

如果我有以下内容

$webs = Get-SPWeb -Limit all -ErrorAction Stop

foreach($web in $webs) 
{
    Write-Host $web.SiteUsers.xml 
}

我知道 .NET 中的相同代码将从 SPWeb.SiteUsers.xml 获取我服务器上一个站点的异常.

I know that the same code in .NET will get an exception from SPWeb.SiteUsers.xml for one site on my server.

实际异常或为什么不重要 - 但运行上面的代码我没有看到任何异常传播或报告给 PowerShell,$web.SiteUsers.xml 只是在出现错误的站点上返回 null.

The actual exception or why isn't important - but running the code above I don't see any exceptions propogated or reported to PowerShell, $web.SiteUsers.xml just ruturns null on the site that errors.

这是一个强大的东西还是 Get-SPWeb 的一个怪癖?

Is this a powershell thing or a quirk of Get-SPWeb?

推荐答案

使用属性语法时,PowerShell 将捕获所有异常.如果要查看异常,则需要使用方法语法.例如,代替:

When using property syntax, PowerShell will catch all exceptions. If you want to see the exceptions, you'll need to use method syntax. For example, instead of:

$web.SiteUsers

你会使用:

$web.get_SiteUsers()

如果 Set-StrictMode 允许异常通过就好了,但事实并非如此.

It would be nice if Set-StrictMode would let exceptions through, but it doesn't.

这种行为的主要原因与格式有关.有许多常用的属性会使用默认格式抛出异常,并且用错误消息使输出混乱,这绝对不是正确的做法.

The primary reason for this behavior is related to formatting. There are many commonly used properties that throw exceptions using the default formatting and cluttering up the output with error messages is definitely not the right thing to do.

也就是说,PowerShell 在格式化输出时只捕获异常似乎是合理的.您可以使用 Microsoft Connect 站点来提供反馈.例如,这个项目抱怨这个确切的问题:http://connect.microsoft.com/PowerShell/feedback/details/533233/exceptions-thrown-in-property-getters-are-silently-ignored

That said, it seems reasonable for PowerShell to only catch exceptions while formatting output. You can use the Microsoft Connect site to provide feedback. For example, this item complains about this exact issue: http://connect.microsoft.com/PowerShell/feedback/details/533233/exceptions-thrown-in-property-getters-are-silently-ignored

这篇关于为什么 Get-SPWeb 中的错误不会传播到 powershell?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆