为什么不是错误rasied内获取-的SPWeb得到传播到PowerShell的? [英] Why don't errors rasied inside Get-SPWeb get propagated to powershell?
问题描述
如果我有以下
$网= GET-的SPWeb -limit所有-ErrorAction停止
的foreach(在$网$网)
{
写主机$ web.SiteUsers.xml
}
我知道,同样的code。在.NET会得到 SPWeb.SiteUsers.xml
例外一个网站我的服务器上。
实际的异常,或者为什么不是很重要 - 但在运行code以上我看不出将会传播或报道PowerShell的任何异常,$ web.SiteUsers.xml只是ruturns空在网站上的错误<。 / P>
这是一个PowerShell的东西或一个怪癖获取-的SPWeb?
在使用属性语法,PowerShell的将捕获所有异常。如果你想看到的例外情况,您需要使用方法的语法。代替例如:
$ web.SiteUsers
您可以使用:
$ web.get_SiteUsers()
这将是很好,如果设置StrictMode将允许通过例外,但事实并非如此。
主要的原因行为与格式。有抛出使用默认的格式和塞满了错误信息的输出异常,绝对不是正确的事做很多常用的属性。
这就是说,它似乎是合理的PowerShell来只捕获异常而格式化输出。您可以使用Microsoft Connect站点提供反馈。例如,这个项目会抱怨这个确切的问题:<一href="http://connect.microsoft.com/PowerShell/feedback/details/533233/exceptions-thrown-in-property-getters-are-silently-ignored" rel="nofollow">http://connect.microsoft.com/PowerShell/feedback/details/533233/exceptions-thrown-in-property-getters-are-silently-ignored
If I have the following
$webs = Get-SPWeb -Limit all -ErrorAction Stop
foreach($web in $webs)
{
Write-Host $web.SiteUsers.xml
}
I know that the same code in .NET will get an exception from SPWeb.SiteUsers.xml
for one site on my server.
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.
Is this a powershell thing or a quirk of Get-SPWeb?
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
You would use:
$web.get_SiteUsers()
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.
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
这篇关于为什么不是错误rasied内获取-的SPWeb得到传播到PowerShell的?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!