############################################################
#Script to get all Library item count from the sitecollection and subsites
# Required Parameters:
# -> $sUserName: User Name .
# -> $sPassword: Password for the user.
# -> $sDomain: AD Domain for the user.
# -> $sSiteColUrl: Site Collection Url.
##########################################################
$host.Runspace.ThreadOptions = "ReuseThread"
#To get all Library item count from the sitecollection and subsites
function Get-SPAllDocCountInSiteCol
{
param ($sSiteColUrl,$sUserName,$sDomain,$sPassword)
try
{ Clear-Host ""
Write-Host "------------------------------------------------------------" -foregroundcolor Gray
Write-Host "Getting all Library/List Item count in a SharePoint Site" -foregroundcolor Gray
Write-Host "-----------------------------------------------------------" -foregroundcolor Gray
$OpFilePath="Result.csv"
#Adding the Client Object Model Assemblies
Add-Type -Path "Microsoft.SharePoint.Client.dll"
Add-Type -Path "Microsoft.SharePoint.Client.Runtime.dll"
#SPO Client Object Model Context
$spContext = New-Object Microsoft.SharePoint.Client.ClientContext($sSiteColUrl)
$spCredentials = New-Object System.Net.NetworkCredential($sUserName,$sPassword,$sDomain)
$spContext.Credentials = $spCredentials
#Root Web Site
$spRootWebSite = $spContext.Web
#Collecction of Sites under the Root Web Site
$spSites = $spRootWebSite.Webs
#Loading Operations
$spContext.Load($spRootWebSite)
$spContext.Load($spSites)
#Get All Library and List from Root Site
$spRootWebLists=$spRootWebSite.Lists
#Loading Operations
$spContext.Load($spRootWebLists)
#QueryExecution
$spContext.ExecuteQuery()
#Delete File if Exists
If(Test-Path $OpFilePath){
Remove-Item $OpFilePath
}
$text = "Site Title,Site URL ,Library/List Name ,Type,Items Count ,OverAll Count "
#Create New File
$text | Set-Content $OpFilePath
$fCount=0
#Print Statement
Write-Host $spRootWebSite.Title " - " $spRootWebSite.Url -ForegroundColor Cyan
Write-Host "------------------------------------------" -ForegroundColor Magenta
#Append the content into File
$text = $spRootWebSite.Title +","+ $spRootWebSite.Url+", , , , "
$text | Add-Content $OpFilePath
#We need to iterate through the $spRootWebLists Object in order to get individual Library/List information
foreach($spRootWeblist in $spRootWebLists)
{
#Item Count for each Library
Write-Host $spRootWeblist.Title " - " $spRootWeblist.ItemCount -ForegroundColor Green
#Sum of each Library Item Count
$fCount=$fCount+$spRootWeblist.ItemCount
#Append the content into File
$text = " , , "+ $spRootWeblist.Title +","+$spRootWeblist.BaseType+" ,"+ $spRootWeblist.ItemCount+", "
$text | Add-Content $OpFilePath
}
#Overall Item Count from the Site
Write-Host "OverAll Count : " $fCount -ForegroundColor White
#Append the content into File
$text = " , , , , , "+$fCount
$text | Add-Content $OpFilePath
Write-Host "=============================================" -ForegroundColor Yellow
#We need to iterate through the $spSites Object in order to get individual sites information
foreach($spSite in $spSites){
Write-Host $spSite.Title " - " $spSite.Url -ForegroundColor Cyan
Write-Host "------------------------------------------" -ForegroundColor Magenta
#Append the content into File
$text = $spSite.Title +","+ $spSite.Url+", , , , "
$text | Add-Content $OpFilePath
#Get All Library and List from the Site
$spLists=$spSite.Lists
#Loading Operations
$spContext.Load($spLists)
#QueryExecution
$spContext.ExecuteQuery()
$fCount=0
#We need to iterate through the $spRootWebLists Object in order to get individual Library/List information
foreach($splist in $spLists)
{
#Item Count for each Library
Write-Host $splist.Title " - " $splist.ItemCount -ForegroundColor Green
#Sum of each Library Item Count
$fCount=$fCount+$splist.ItemCount
#Append the content into File
$text = " , , "+ $splist.Title +","+$spRootWeblist.BaseType+" ,"+ $splist.ItemCount+", "
$text | Add-Content $OpFilePath
}
Write-Host "OverAll Count : " $fCount -ForegroundColor White
Write-Host "=============================================" -ForegroundColor Yellow
#Append the content into File
$text = " , , , , , "+$fCount
$text | Add-Content $OpFilePath
}
$spContext.Dispose()
}
catch [System.Exception]
{
write-host -f red $_.Exception.ToString()
}
}
#Required Parameters
$sSiteColUrl = "https://www.sample.com"
$sUserName = "username"
$sDomain="domainName"
$sPassword ="Password"
Get-SPAllDocCountInSiteCol -sSiteColUrl $sSiteColUrl -sUserName $sUsername -sDomain $sDomain -sPassword $sPassword
No comments:
Post a Comment