PowerShell - 遍历所有SharePoint网站&发生错误时继续

时间:2014-04-08 17:53:38

标签: sharepoint powershell

$ErrorActionPreference = "Continue"
Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction "SilentlyContinue"
$webApp = "Https://SharePointSite.com"
$wa = Get-SPWebApplication -identity $webApp
foreach ($site in $wa.Sites) {
   foreach ($web in $site.AllWebs) {
       $siteURL = $web.Url
     Write-Host $siteURL
   }
}

问题是,当它遇到语句 foreach($ site.Sites中的$ site) 时,由于拒绝访问或任何错误而无法访问该网站,它会停止。我想继续。我试图最终尝试捕获,它在遇到错误时仍然停止。

我尝试输入 -ErrorAction Continue ,但它给出了一条错误消息 你必须在' - '的右边提供一个价值表达。运营商

如何绕过它以便继续使用该网站?

我非常感谢你的帮助。 感谢

1 个答案:

答案 0 :(得分:0)

如果要使用try..catch,则必须抛出终止错误。非终止错误不是try..catch块的处理。

try {
    Add-PSSnapin Microsoft.SharePoint.Powershell -ErrorAction Stop;
    $webApp = "Https://SharePointSite.com"
    $wa = Get-SPWebApplication -identity $webApp -ErrorAction Stop;
    foreach ($site in $wa.Sites) {
       foreach ($web in $site.AllWebs) {
           $siteURL = $web.Url
         Write-Host $siteURL
       }
    }
}
catch {
    Write-Host -Object ('Error occurred: {0}' -f $_);
}