Why don't errors rasied inside Get-SPWeb get propagated to powershell?

你说的曾经没有我的故事 提交于 2019-12-28 07:03:11

问题


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?


回答1:


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



来源:https://stackoverflow.com/questions/18902542/why-dont-errors-rasied-inside-get-spweb-get-propagated-to-powershell

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!