Archive for May, 2017

SharePoint Online – List of Sites & Webs

Scenario:

Need to create a PowerShell script that iterates through all SharePoint Online site collections and webs and generate info. regarding site collection size, last time site/web update and other.

Resolution:

Tweaked script by Jose Quinto @ https://blog.josequinto.com/2016/03/17/using-powershell-to-retrieve-all-sites-web-object-recursively-from-sharepoint-online/.

Output is two CSV files in temp folder on C drive root. One with the details and other with site collection on which account gets 401 error.

$Global:webinfo = @()

function AuthenticateUserProfile($siteUrl, $tenantAdmin, $secureAdminPassword)
{
$ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
$credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($tenantAdmin, $secureAdminPassword)
$ctx.Credentials = $credentials
return $ctx
}

function RecursiveWebs($ctx, $web){

foreach($w in $web.Webs)
{
$ctx.Load($w.Webs);
$ctx.ExecuteQuery();

$Properties = @{
Title = $w.Title
URL = $w.Url
NoOfSubsites = $w.Webs.Count
LastUserModifiedDate = $w.LastItemUserModifiedDate
LastItemModifiedDate = $w.LastItemModifiedDate
SiteCollection = ‘No’
SiteOwner = $w.Author.Title
}

$Global:webinfo += New-Object psobject -Property $properties

if ($w.Webs.Count -gt 0)
{
RecursiveWebs $ctx $w
}
}
}

$username = “adm@test.com”
$password = ConvertTo-SecureString “PASSWORD” -AsPlainText -Force
$O365Credential = New-Object System.Management.Automation.PsCredential($username, $password)

#SharePoint Online Admin site URL
Connect-SPOService -Url https://test-admin.sharepoint.com -Credential $O365Credential
$sites = Get-SPOSite -Limit All -Detailed

foreach ($site in $sites)
{
if(
$site.Url.Equals(“https://test.sharepoint.com/sites/new”) -or
$site.Url.Equals(“https://test.sharepoint.com/search”)
)
{
continue;
}

Write-Host $site.Url
try
{
$ctx = AuthenticateUserProfile $site.Url $username $password;
$web = $ctx.Web
$ctx.Load($web)
$ctx.ExecuteQuery()
$ctx.Load($web.Webs)
$ctx.ExecuteQuery()

$Properties = @{

Title = $site.Title
URL = $site.Url
SiteSizeLimitInMB = $site.StorageQuota
SiteUsageInMB = $site.StorageUsageCurrent
NoOfSubsites = $web.Webs.Count
SiteOwner = $site.Owner
LastContentModifiedDate = $site.LastContentModifiedDate
LastUserModifiedDate = $web.LastItemUserModifiedDate
SiteCollection = ‘Yes’
LastItemModifiedDate = $web.LastItemModifiedDate
}

$Global:webinfo += New-Object psobject -Property $properties

if ($web.Webs.Count -gt 0)
{
RecursiveWebs $ctx $web
}
}
catch
{
Write-Host $_ -ForegroundColor Red
$Properties1 = @{

Title = $site.Title
URL = $site.Url

}

$site401 += New-Object psobject -Property $properties1

}

}

$webinfo | Select-Object Title, URL, SiteSizeLimitInMB, SiteUsageInMB, LastItemModifiedDate,
LastUserModifiedDate, LastContentModifiedDate, NoOfSubsites,
SiteOwner, SiteCollection | Export-Csv -notypeinformation -Path ‘C:\temp\SPOinfo.csv’

$site401 | Select-Object Title, URL | Export-Csv -notypeinformation -Path ‘C:\temp\SPOinfo401.csv’

 

Leave a Comment