问题
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