2

如果我有以下

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

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

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

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

这是 Powershell 的事情还是 Get-SPWeb 的怪癖?

4

1 回答 1

3

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

于 2013-09-21T03:03:55.340 回答