Thu. Jun 16th, 2022
Function Get-SiteInventory([String]$Sprint,[String]$WebDomain,[String]$SiteURL,[String]$SiteTitle,[String]$SummaryOuputFile, $Credentials)
{
     Try{
        $Timestamp = GetTimeStamp
        $ReportOutputFilePath = $ReportOutput + '\' + $SiteTitle +'_SiteInventory ' + $Timestamp + '.xlsx'

        if (Test-Path $ReportOutputFilePath) { Remove-Item $ReportOutputFilePath }
        if (Test-Path $ReportOutputFilePath) 
        { 
            write-host $ReportOutputFilePath - "Site Inventory File already exists" -f Yellow
             
        }else{

        $global:SiteInventorySheetRow = 2
        $Global:SiteTotalSize = 0
        $Global:SiteListItemTotalSize = 0
        $Global:SiteOnetoc2 = 0
        #Create Output Workbook
        $excel = New-Object -comobject Excel.Application
        $workbook = $excel.Workbooks.Add()

        $SiteInventorySheet = $workbook.Worksheets.Item(1);
        ContructInventoryWorksheets -siteInventorySheet $SiteInventorySheet
                
        $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl);
        $ctx.Credentials = $Credentials
      
        #Get the Web
        $Web = $Ctx.Web
        $Ctx.Load($Web)
        $Ctx.Load($Web.Webs)
        $Ctx.ExecuteQuery()
        
        #Get All Lists from the web
        $Lists = $Web.Lists
        
        $Ctx.Load($Lists)
        $Ctx.ExecuteQuery()

        Write-host -f Yellow "Processing Web:"$Web.Url

        GetListInformation -web $ctx.Web -ctx $ctx -memberSheet $SiteInventorySheet -credentials $psCreds
        $Summaryoutput = "$Sprint,$SiteTitle,$SiteURL,$Global:SiteListItemTotalSize,$Global:SiteTotalSize,$Global:SiteOnetoc2"
        Add-Content $SummaryOuputFile $Summaryoutput 

        $excel.Visible = $true
        Add-Type -AssemblyName Microsoft.Office.Interop.Excel
        $xlFixedFormat = [Microsoft.Office.Interop.Excel.XlFileFormat]::xlWorkbookDefault
        
        #Delete the Output Report, if exists

        write-host Saving to $ReportOutputFilePath -ForegroundColor Green

        $excel.ActiveWorkbook.SaveAs($ReportOutputFilePath, $xlFixedFormat)
        $excel.Workbooks.Close();
        $excel.Quit()    
        UploadFileToList -ListName $Sprint -FolderName $SiteTitle -FilePath $ReportOutputFilePath
        return $ReportOutputFilePath
    }
    }
    Catch {
        write-host -f Red "Error Generating Site Inventory Report!" $_.Exception.Message
    }

}


function GetListInformation($web,$ctx, $memberSheet, $psCreds)
{
        #Get the Web
        $Web = $Ctx.Web
        $Ctx.Load($Web)
        $Ctx.Load($Web.Webs)
        $Ctx.ExecuteQuery()

        #Get All Lists from the web
        $Lists = $Web.Lists
        
        $Ctx.Load($Lists)
        $Ctx.ExecuteQuery()

        #Array to hold List information
        $ResultData = @()
        $Global:ListSize=0
        $onetoc2 = $Null
        #Iterate through each List in the web
        ForEach($List in $Lists )
        {
            $ListSize=0
            Invoke-LoadMethod -Object $list -PropertyName "HasUniqueRoleAssignments"
            $ctx.ExecuteQuery()
            $Ctx.Load($List.RootFolder)
            $Ctx.ExecuteQuery()

            #Get List Full URL
            If($Ctx.Web.ServerRelativeUrl -eq "/")
            {
                $ListURL = $("{0}{1}" -f $Ctx.Web.Url, $List.RootFolder.ServerRelativeUrl)
            }
            Else
            {
                $ListURL = $("{0}{1}" -f $Ctx.Web.Url.Replace($Ctx.Web.ServerRelativeUrl,''), $List.RootFolder.ServerRelativeUrl)
            }

            $ListPermisssionType = $null
            $InScope = $null

            if ($list.HasUniqueRoleAssignments )
            {
                $ListPermisssionType = "Unique"
            }else
            {
                $ListPermisssionType = "Inherited"
            }
 
            if ($list.BaseTemplate -eq 100 -or $list.BaseTemplate -eq 101 -or $list.BaseTemplate -eq 106 -or $list.BaseTemplate -eq 107 -or $list.BaseTemplate -eq 119 -or $list.BaseTemplate -eq 171)
            {
                $InScope = "Yes"
            }else
            {
                $InScope = "No"
            }

            $onetoc2 = CheckForOnetoc2 -web $web -ctx $ctx -List $List

            #Prepare the CAML query
            $Query = New-Object Microsoft.SharePoint.Client.CamlQuery 
            $Query.ViewXml = "@
            <View Scope='RecursiveAll'>  
                 <Query> 
                     <Where>
                         <Eq>
                             <FieldRef Name='FSObjType' /><Value Type='Integer'>0</Value>
                         </Eq>
                     </Where> 
                 </Query> 
                <RowLimit>3000</RowLimit> 
            </View>"

            #Batch Process List items
            Do {
                try
                {                
                $ListItems = $List.GetItems($Query)
                $Ctx.Load($ListItems)
                $Ctx.ExecuteQuery()
                $Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition
                Write-host -f Yellow "Processing List: $($List.Title) ($($ListItems.Count))"
                #Get All Checked out files
                $i=1
                ForEach($Item in $ListItems)
                {
                    $TotalSize = 0
 #                   Write-host -f Yellow "Calculating Size - $($List.Title) - of Item $i of $($ListItems.count)..."
 #                  if ($Item.FileSystemObjectType -eq "File")
 #                  {
                     #Get the File
                    $File = $Web.GetFileByServerRelativeUrl($Item["FileRef"])
                    $Ctx.Load($File)
                    $Ctx.Load($File.Versions)
                    $Ctx.ExecuteQuery()
 
                    $FileSize =0; $VersionSize = 0
                    If($File.Versions.Count -ge 1)
                    {
                        $VersionSize = $File.Versions | Measure-Object -Property Size -Sum | Select-Object -expand Sum
                    }
                    $FileSize =  $Item.File.Length  + $VersionSize
                    $TotalSize = $TotalSize + $FileSize
                    $TotalSize =  [Math]::Round($TotalSize/1MB, 2)
                    $ListSize = $ListSize + $TotalSize
                    $i++
#                   }
                }
                }catch
                {
                    $e = $_.Exception
                    $line = $_.InvocationInfo.ScriptLineNumber
                    $msg = $e.Message 
                    Write-Host -ForegroundColor Red "caught exception: $e at $line"
                    Get-FileSizeFromLargeList -web $web -ctx $ctx -List $List
                    $ListSize = $Global:ListSize
                }


            }While($Query.ListItemCollectionPosition -ne $Null)
             
            $Global:SiteListItemTotalSize = $Global:SiteListItemTotalSize + $List.ItemCount
            $Global:SiteOnetoc2 = $Global:SiteOnetoc2 + $onetoc2
            
            $output = $Web.URL + '¦' + $Web.Title + '¦' + "$($ListURL)"  + '¦' + $List.Title + '¦' + $ListPermisssionType + '¦' + $List.BaseType + '¦' + $List.BaseTemplate + '¦' + $InScope + '¦' + $List.ItemCount + '¦' + $ListSize + '¦' + $List.LastItemModifiedDate.ToString() + '¦' + $onetoc2
        #    $output = $Web.URL + '¦' + $Web.Title + '¦' + "$($Web.Url)$($List.RootFolder.ServerRelativeUrl)"  + '¦' + $List.Title + '¦' + $ListPermisssionType + '¦' + $List.BaseType + '¦' + $List.BaseTemplate + '¦' + $InScope + '¦' + $List.ItemCount + '¦' + $ListSize + '¦' + $List.LastItemModifiedDate.ToString() + '¦' + $onetoc2
            WriteRowToExcel -worksheet $memberSheet -output $output -row $global:SiteInventorySheetRow
            $global:SiteInventorySheetRow++;
            $Global:SiteTotalSize = $Global:SiteTotalSize + $ListSize
        }
        
}

This website uses cookies. By continuing to use this site, you accept our use of cookies.