在尝试启用SharePoint 2010中的自定义功能时,我收到了无法在PowerShell v2中以编程方式捕获,捕获或检测到的异常。我知道错误是什么以及如何在Central Admin中修复它,但是我担心PowerShell部署脚本无法识别此错误情况。PowerShell无法捕获SharePoint Enable-SPFeature异常
# This correctly gets the site and feature:
PS C:\> $Error.Clear()
PS C:\> $SiteUrl = 'http://thesite'
PS C:\> $FeatureId = 'D3BF12C5-D342-4F8A-8E86-BB4308699359'
PS C:\> $Site = Get-SPSite | Where { $_.Url -eq $SiteUrl }
PS C:\> $Feature = Get-SPFeature | Where { $_.Id.ToString() -eq $FeatureId }
# But this line produces the error information below it in the console window - in plain color, not red:
PS C:\> $Feature | Enable-SPFeature -url $SiteUrl
Source: Microsoft.Office.Server.UserProfiles
Message: Users cannot override privacy while the property is replicable.
Trace: at Microsoft.Office.Server.UserProfiles.ProfileSubtypeProperty.set_UserOverridePrivacy(Boolean value)
at IHI.Springs.GAC.UserProfile.UserProfilePropertyManager.EnableUserOverridePrivacy(String propertyName)
at IHI.Springs.GAC.EventReceivers.FeatureReceivers.ProfilePropertyFeatureReceiver.FeatureActivated(SPFeatureReceiverProperties properties)
上述错误信息被写入到控制台,但无法检测程序:
运行Enable-SPFeature命令,$后?是$ true,$ Error没有错误,$ LastExitCode没有任何内容。
试图用try/catch或trap捕获异常失败。对于下面的代码行,错误消息仍然被转储到控制台,但“Error caught!”永远不会显示:
try { $Feature | Enable-SPFeature -url $SiteUrl } catch { "Error caught!" }
trap { "Error caught!" } $Feature | Enable-SPFeature -url $SiteUrl
而且无论我做什么,我都无法捕获该文本;没有这些工作:显示在控制台
错误消息,$ ERRORINFO为空:显示在控制台
$ErrorInfo = $Feature | Enable-SPFeature -url $SiteUrl 2>&1
$ErrorInfo = $null; $Feature | Enable-SPFeature -url $SiteUrl -ErrorVariable ErrorInfo
错误消息,ErrorFile.txt创建(预期),但空:
$Feature | Enable-SPFeature -url $SiteUrl > c:\temp\ErrorFile.txt
我们正在研究我们的功能激活代码,并且我们知道如何在服务器上修复此问题,但是我无法以编程方式检测到此错误,这实在太疯狂了。该异常是由Enable-SPFeature cmdlet引发的,但PowerShell未将该异常封装在ErrorRecord中,不会将其写入错误管道或执行任何有用的操作。自从Monad Beta 2发布以来,我一直在使用PowerShell,并且看到了很多时髦的东西,但这震动了我对它的信心。 (我对SharePoint没有任何信心,所以没有任何损失)。
Powershell + Sharepoint错误处理。太好了! – 2011-03-01 08:37:05