Tue. Jun 7th, 2022
Function Get-CheckedOutFiles([String]$SiteURL,[String]$SiteTitle,[String]$ReportOutput, [String]$SummaryOuputFile, [String]$Choice, $Credentials)
{

     Try{
        $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl);
        $ctx.Credentials = New-Object System.Net.NetworkCredential($src_Username, $Networkpassword)

        #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()
        $BatchSize=3000
        #Prepare the CAML query
        $Query = New-Object Microsoft.SharePoint.Client.CamlQuery
        $Query.ViewXml = "@
        <View Scope='RecursiveAll'>
            <Query> 
               <Where><IsNotNull><FieldRef Name='CheckoutUser' /></IsNotNull></Where> 
            </Query> 
            <RowLimit>$BatchSize</RowLimit> 
        </View>"


        #Array to hold Checked out files
        $Global:CheckedOutCounter=0
        $Global:CheckedOutFiles = @()
        Write-host -f Yellow "Processing Web:"$Web.Url
       
        #Iterate through each document library in the web
        ForEach($List in ($Lists | Where-Object {$_.BaseTemplate -eq 101}) )
        {

        $Global:FileCounter=0
        $Global:FolderCounter=0

            if ($Global:listExclusions.Contains($List.Title) -eq $false)
            {

            Write-host -f Yellow "Processing Document Library:"$List.Title
            #Exclude System Lists
            If($List.Hidden -eq $False)
            {
                $CurrentList = $List.title

               #Batch Process List items
                Do {
                    try
                    {

                    $ListItems = $List.GetItems($Query)
                    $Ctx.Load($ListItems)
                    $Ctx.ExecuteQuery()
 
                    $Query.ListItemCollectionPosition = $ListItems.ListItemCollectionPosition
                    $CheckedOutCounter=0
                    #Get All Checked out files
                    write-host Item count $ListItems.count
                    ForEach($Item in $ListItems)
                    {
                        $CheckedOutByUser = $null

                        try
                        {
                            #Get the Checked out File data
                            $File = $Web.GetFileByServerRelativeUrl($Item["FileRef"])
                            $Ctx.Load($File)
                            $CheckedOutByUser = $File.CheckedOutByUser
                            $Ctx.Load($CheckedOutByUser)

                            try
                            {
                                $Ctx.ExecuteQuery()
        
                                if($CheckedOutByUser.LoginName -ne $null)
                                {
                                    $Global:CheckedOutCounter++    
                                    Write-Host -f Green "Found a Checked out File '$($File.Name)' at $WebDomain$($Item['FileRef']), Checked Out By: $($CheckedOutByUser.LoginName)"

                                    iF($Choice -eq "Check-in")
                                    {
                                    $Global:CheckedOutFiles += New-Object -TypeName PSObject -Property @{
                                                Site = $Web.Title
                                                SiteUrl =  $Web.Url
                                                List = $List.Title
                                                FileName = $File.Name
                                                Checkedoutstatus = "Checked In"
                                                Location = $WebDomain+$Item['FileRef']
                                                CheckedOutBy = $CheckedOutByUser.LoginName
                                                CheckedoutTo = $CheckedOutByUser.Email}
                         
                                    #check in file programmatically     
                                    $File.CheckIn("Checked-in By Administrator through PowerShell!", [Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn) 
                                    $Ctx.ExecuteQuery()
                                    Write-Host -f Green "File '$($File.Name)' Checked-In Successfully!"
                                    }elseif($Choice -eq "Pre-Check")
                                    {
                                          $Global:CheckedOutFiles += New-Object -TypeName PSObject -Property @{
                                                Site = $Web.Title
                                                SiteUrl =  $Web.Url
                                                List = $List.Title
                                                FileName = $File.Name
                                                Checkedoutstatus = "Pre-Check"
                                                Location = $WebDomain+$Item['FileRef']
                                                CheckedOutBy = $CheckedOutByUser.LoginName
                                                CheckedoutTo = $CheckedOutByUser.Email}
                                           Write-Host -f Magenta "File '$($File.Name)' $($Choice) Successfully!"
                                    }else
                                    {
                                        $Global:CheckedOutFiles += New-Object -TypeName PSObject -Property @{
                                                Site = $Web.Title
                                                SiteUrl =  $Web.Url
                                                List = $List.Title
                                                FileName = $File.Name
                                                Checkedoutstatus = "Checked Out"
                                                Location = $Global:WebDomain+$Item['FileRef']
                                                CheckedOutBy = $CheckedOutByUser.LoginName
                                                CheckedoutTo = $CheckedOutByUser.Email }  
                                    }
                                }  
                            
                            }catch
                            {
                                write-host -f Yellow "Error Locating user!" $File.Name $_.Exception.Message  
                                $Global:CheckedOutCounter++    
                                #Write-Host -f Green "2. Found a Checked out File '$($File.Name)' at $WebDomain$($Item['FileRef']), Checked Out By: Unknown user"

                                iF($Choice -eq "Check-in")
                                {
                                    $Global:CheckedOutFiles += New-Object -TypeName PSObject -Property @{
                                                Site = $Web.Title
                                                SiteUrl =  $Web.Url
                                                List = $List.Title
                                                FileName = $File.Name
                                                Checkedoutstatus = "Checked In"
                                                Location = $WebDomain+$Item['FileRef']
                                                CheckedOutBy = "Unknown User"
                                                CheckedoutTo = "Unknown User"}
                         
                                    #check in file programmatically     
                                    $File.CheckIn("Checked-in By Administrator through PowerShell!", [Microsoft.SharePoint.Client.CheckinType]::MajorCheckIn) 
                                    $Ctx.ExecuteQuery()
                                    Write-Host -f Green "File '$($File.Name)' Checked-In Successfully!"
                                }elseif($Choice -eq "Pre-Check")
                                {
                                          $Global:CheckedOutFiles += New-Object -TypeName PSObject -Property @{
                                                Site = $Web.Title
                                                SiteUrl =  $Web.Url
                                                List = $List.Title
                                                FileName = $File.Name
                                                Checkedoutstatus = $Choice
                                                Location = $WebDomain+$Item['FileRef']
                                                CheckedOutBy = $CheckedOutByUser.LoginName
                                                CheckedoutTo = $CheckedOutByUser.Email}
                                           Write-Host -f Magenta "File '$($File.Name)' $($Choice) Successfully!"

                                }else
                                {
                                            $Global:CheckedOutFiles += New-Object -TypeName PSObject -Property @{
                                                Site = $Web.Title
                                                SiteUrl =  $Web.Url
                                                List = $List.Title
                                                FileName = $File.Name
                                                Checkedoutstatus = "Checked Out"
                                                Location = $Global:WebDomain+$Item['FileRef']
                                                CheckedOutBy = "Unknown User"
                                                CheckedoutTo = "Unknown User" }  
                                }
                            }                                                
                    
                         }catch
                         {
                            write-host -f Red "Error finding file!" $File.Name $_.Exception.Message 
                         }
                    }# For Each

                    }catch
                    {
                        Get-CheckOutFilesFromLargeList -web $web -ctx $ctx -List $List -Choice $Choice
                        write-host FolderCounter $Global:FolderCounter FileCounter $Global:FileCounter CheckedOutCounter $Global:CheckedOutCounter $Choice
                     }
                }While($Query.ListItemCollectionPosition -ne $Null)
            }

            }

        }

        If($Global:CheckedOutCounter -gt 0)
        {
            #Export the Findings to CSV File
            if($Choice -eq "Check-in")
            {
                $ReportOutput = $ReportOutput + '\' + $SiteTitle + '_' + $Global:CheckedOutCounter.ToString() +  '_Checked-in Files ' + $Timestamp + '.csv'
            }elseif($Choice -eq "Pre-Check")
            {
                $ReportOutput = $ReportOutput + '\' + $SiteTitle + '_' + $Global:CheckedOutCounter.ToString() +  '_Pre-Check Checked-in Files ' + $Timestamp + '.csv'
            }else
            {
                $ReportOutput = $ReportOutput + '\' + $SiteTitle + '_' + $Global:CheckedOutCounter.ToString() +  '_Checked-Out Files ' + $Timestamp + '.csv'
            }
            
            write-host Saving to $ReportOutput - $Global:CheckedOutCounter -ForegroundColor Green
            $Global:CheckedOutFiles | Export-CSV $ReportOutput -NoTypeInformation -Append
            $Global:CheckedOutFiles| Export-CSV $SummaryOuputFile -NoTypeInformation -Append
        }else{
              $CheckedOutFiles += New-Object -TypeName PSObject -Property @{
                                  Site = $Web.Title
                                  SiteUrl =  $Web.Url
                                  List = "No Checkout Files"
                                  FileName = "" 
                                  Checkedoutstatus = ""
                                  Location = ""
                                  CheckedOutBy = ""
                                  CheckedoutTo = ""}
                     
            #Export the Findings to CSV File
            $ReportOutput = $ReportOutput + '\' + $SiteTitle + '_0_CheckedOutFiles ' + $Timestamp + '.csv'
            $CheckedOutFiles | Export-CSV $ReportOutput -NoTypeInformation -Append
            write-host Saving to $ReportOutput - $CheckedOutCounter -ForegroundColor Yellow
            $CheckedOutFiles| Export-CSV $SummaryOuputFile -NoTypeInformation -Append
            write-host Saving Summary to $SummaryOuputFile - $CheckedOutCounter -ForegroundColor Yellow
        }
        Return $ReportOutput
        #Iterate through each subsite of the current web and call the function recursively
#        ForEach($Subweb in $Web.Webs)
 #       {
            #Call the function recursively to process all subsites underneaththe current web
        #    Get-SPOCheckedOutFiles -SiteURL $Subweb.URL -SiteTitle $Subweb.title -ReportOutput $ReportOutput -Credentials $Credentials
  #      }
      #  }#if    #$list.title -eq "Specification and Delivery")
   
    }
    Catch {
        write-host -f Red "Error Generating Checked Out Files Report!" $_.Exception.Message
    }
}

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