I use foreach in my scripting to make sure I get every web in every site in a specific webapp to, in this case, list (and eventually update) all Pages libraries in the SiteCollection. I've been adviced to take a look at SPSiteDataQuery, which doesn't take as long and use much less resources than my method. The only problem is that I can't seem to get it to work even if I follow blogs:
- Using powershell to quickly debug SPSiteDataQuery
- Using SPSiteDataQuery with Powershell
- Powershell script to get Site Collection Pages and List Items count using SPSiteDataQuery object.
I get no result, too many rows, Exception calling GetSiteData with 1 argument and the like, but nothing really useful. So I wonder if its possible to list all Pages libraries in all web in all sites in a specific webapps using SPSiteDataQuery?
This is my current resource heavy script (which actually works):
# Add PS Snapin if not loaded
$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
if ($snapin -eq $null)
{
Write-Host "Loading SharePoint Powershell Snapin"
Add-PSSnapin "Microsoft.SharePoint.Powershell" -EA SilentlyContinue
}
#Iterate all webs and print List URL
$SPWebApp = Get-SPWebApplication "http://mywebapp/"
foreach ($SPSite in $SPWebApp.Sites)
{
if ($SPSite -ne $null)
{
foreach ($SPWeb in $SPSite.AllWebs)
{
foreach ($list in $SPWeb.Lists)
{
if ($list.Title.Contains("Pages"))
{
$ListURL = $SPWeb.url + "/" + $list.RootFolder.Url
Write-Host $ListURL
}
if ($SPWeb -ne $null)
{
$SPWeb.Dispose()
}
}
}
}
if ($SPSite -ne $null)
{
$SPSite.Dispose()
}
}