2010-12-15 87 views
1

在嘗試啓用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沒有任何信心,所以沒有任何損失)。

+0

Powershell + Sharepoint錯誤處理。太好了! – 2011-03-01 08:37:05

回答

7

這是一種WAG,我從來沒有使用任何SharePoint cmdlet。

嘗試向Enable-SPFeature命令添加「-ea stop」(不帶引號)。這可能會導致錯誤實際上被拋出。

IMO使用-ea Stop強制錯誤終止錯誤,因此可以被捕獲,這是PowerShell更直觀的特性之一。

+1

作品!非常感謝!當添加-ErrorAction停止時,$?現在是false,$ Error與Error/catch中的$ _一樣具有ErrorRecord。我不能阻止文本輸出到控制檯(通過任何重定向),也不會填充ErrorVariable。只要我有錯誤信息,沒什麼大不了的。再次感謝 - 使用-EA這種方式是違反直覺的! – DanW 2010-12-16 19:39:56