Pages

Tuesday, September 25, 2018

SharePoint Migration: Farm Checked Out Files Report

During SharePoint migration, it is helpful to review checked out files that have never been published or files that were checked out but were never checked in by end users. If you are using a SharePoint migration tool for e.g.; Sharegate or Metalogix Content Matrix, these checked out files will not be migrated. The script generates a report of the checked out files on the sub-site level and will come handy to prepare for the migration. You can provide this report to site owners so that they can take action on it. :)

Add-PSSnapin microsoft.sharepoint.powershell -ErrorAction SilentlyContinue

$date = Get-Date -f 'yyyyMMdd-hhmmss'
$curDirectory = (Get-Item -Path ".\").FullName

if( -Not (Test-Path -Path $curDirectory"\Output" ) )
{
    New-Item -ItemType directory -Path $curDirectory"\Output"
}

$Output = $curDirectory + '\Output\FarmCheckedoutFilesReport_'+$date+".csv"

function GetCheckedOutItems($spWeb)
{
Write-Host "Scanning Site: $($spWeb.Url)"
foreach ($list in ($spWeb.Lists | ? {$_ -is [Microsoft.SharePoint.SPDocumentLibrary]})) {
    Write-Host "Scanning List: $($list.RootFolder.ServerRelativeUrl)"
    foreach ($item in $list.CheckedOutFiles) {
       
        $itemURL = $spWeb.Site.MakeFullUrl("$($spWeb.ServerRelativeUrl.TrimEnd('/'))/$($item.Url)")
        #Uncomment the following for debugging
        #Write-Host $itemURL
        #Write-Host "Author: $($item.File.ModifiedBy)"
        #Write-Host "Author: $($item["Editor"].Split('#')[1])"
        #Write-Host "Name: $($item.CheckedOutByName)"
        #Write-Host "Email: $($item.CheckedOutByEmail)"
        #Write-Host "Checked Out Date: $($item.TimeLastModified)"
        #Read-Host
            

        Add-Content $output -Value """$($spWeb.Url)"",""$($list.RootFolder.ServerRelativeUrl)"",""$itemURL"",""$($item.CheckedOutByName)"",""$($item.CheckedOutByEmail)"",$($item.Length/1000),""$($item.TimeLastModified.ToString())"","""","""",Never Checked In"
       
    }
    foreach ($item in $list.Items) {
        if ($item.File.CheckOutStatus -ne "None") {
         $itemURL = $spWeb.Site.MakeFullUrl("$($spWeb.ServerRelativeUrl.TrimEnd('/'))/$($item.Url)")
            if (($list.CheckedOutFiles | where {$_.ListItemId -eq $item.ID}) -ne $null) { continue }
            Add-Content $output -Value """$($spWeb.Url)"",""$($list.RootFolder.ServerRelativeUrl)"",""$itemURL"",""$($item.File.CheckedOutByUser.Name)"",""$($item.File.CheckedOutByUser.Email)"",$($item.File.Length/1000),""$($item.File.CheckedOutDate.ToString())"",""$($item["Author"].Split('#')[1])"",""$($item["Editor"].Split('#')[1])"",Checked Out"
            #Uncomment the following for debugging
            #Write-Host $itemURL
            # Write-Host "Author: $($item["Author"].Split('#')[1])"
            # Write-Host "Author: $($item["Editor"].Split('#')[1])"
            #Write-Host "CheckedOutBy: $($item.File.CheckedOutByUser)"
            #Write-Host "Name: $($item.File.CheckedOutByUser.Name)"
            #Write-Host "Email: $($item.File.CheckedOutByUser.Email)"
            #Write-Host "Checked Out Date: $($item.File.CheckedOutDate.ToString())"
            #Read-Host
            
        }
    }
}

$spWeb.Dispose()
}


Add-Content $output -Value "SiteURL,DocumentLibrary,FileURL,CheckedOutBy,CheckedOutByUserEmail,FileSize,CheckedOutSince,Author,Editor,Check In Status"

$webApp = Get-SPWebApplication 
foreach($webapps in $webapp)
{

    foreach ($SiteCollection in $webApps.Sites)
    {
     if ($SiteCollection.ReadOnly -eq $false -and $SiteCollection.ReadLocked -eq $false -and $SiteCollection.WriteLocked -eq $false) 
        {
             $weburl = $SiteCollection.OpenWeb()

              foreach($web in $SiteCollection.AlLWebs)
              {
                GetCheckedOutItems($Web)
                $web.Dispose()
              }
        }
    }
}

Write-Host "Processing completed. Please see the report at $($Output)."



No comments:

Post a Comment