0

I have the following powershellscript, and I would like to rewrite it so if it finds this exception it should continue to the next web.

$webapplication =  Get-SPWebApplication  "https://notadummsite.com"
foreach($site in $webapplication.Sites)
{         

    foreach($subWeb in $site.AllWebs)
    {  
        if( $subWeb.AllProperties["WebTemplate"] -eq "Job" -or $subWeb.AllProperties["WebTemplate"] -eq "Opportunity")
        {
            try {
                Write-Host $subWeb.Url
                $SPPubWeb = [Microsoft.SharePoint.Publishing.PublishingWeb]::GetPublishingWeb($subWeb)
                $SPPubWeb.Navigation.InheritGlobal = $true
                $SPPubWeb.Update()
                Write-Host 'Updated'
            }
            catch{
                Write-Host $subWeb.Url
                Write-Host $error[0]
            }
        } 

        $subweb.Dispose()   
    }

    $site.Dispose()
}
2

Try using ErrorAction -SilentlyContinue in the script,

-ErrorAction kind of explains itself when you think of it. Basically it instructs PowerShell what action to take when an error occurs. The actions you can set are Continue (which is default), Stop, SilentlyContinue and Inquire.

Have a look at the below code also,

  1. How to handle Access denied exceptions in powershell

  2. ErrorAction and -ErrorVariable

4
  • I am doing this and still stops .\ChangeNavigationInheritance.ps -EA SilentlyContinue – Luis Valencia Jan 28 '14 at 12:50
  • @LuisValenciaMunoz In which line of your code, you are getting the error.? – Karthik Jaganathan Jan 28 '14 at 13:07
  • in the foreach, it goes through many webs, and suddently stop at one. – Luis Valencia Jan 28 '14 at 13:13
  • I noticed your $subweb.Dispose() was the incorrect case for $subWeb. If this isn't a typo just on here, then there is an outside shot that the resources aren't correctly being disposed and that could be causing an issue. Just a hunch. – tyshock Jan 28 '14 at 19:37

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.