I write the powershell script below to check if a webpart exists on some page on all mysites. I got an exception on the root site collection of the mysite webapplication because the page does not exists.
How to avoid this exception? I would like to check for example if the page exists but it looks like it dont work. I check already if the variable $page is not null.
Add-PSSnapin Microsoft.SharePoint.PowerShell -EA silentlycontinue
$webAppUrl = "http://MyWebApplication.com"
$spWebApp = Get-SPWebApplication $webAppUrl
foreach($SpSite in $spWebApp.Sites)
{
$resultsUrl = $SpSite.Url +"/Social/Sites.aspx"
$page = $SpSite.RootWeb.GetFile($resultsUrl);
if($page -ne $null)
{
# Get the web part manager for the page
$webPartManager = $SpSite.RootWeb.GetLimitedWebPartManager($resultsUrl, [System.Web.UI.WebControls.WebParts.PersonalizationScope]::Shared)
# And pull the web part we want to work on
$webpart = $webpartmanager.WebParts | ? { $_.GetType().Name -eq 'MyCustomWebpart' }
if($webpart -ne $null)
{
"Found webpart '" + $webpart.Title + "' " + "on page $resultsUrl"
}
}
$SpSite.RootWeb.Dispose();
$SpSite.Dispose();
}