Friday, 4 July 2014

PowerShell - List out all checked out documents in library

PowerShell - List out all checked out documents in library:-
Note:-
Library names needs to be hard coded inside code, at present dummy entries are made, replace it with actual library name before script execution.

Input:-
1. Enter web app url
2. Create a csv file with all site collection url and pass the path of the same.
Code:-
$UrlDochubWebApp = Read-Host "Enter destination web app url"
$SiteCollFilePath = Read-Host "Enter SIte collection csv details Path(folder path)"
#Loading Snap in for Microsoft.Sharepoint.Powershell
$snapin = Get-PSSnapin | Where-Object {$_.Name -eq 'Microsoft.SharePoint.Powershell'}
if ($snapin -eq $null)
{

Write-Host "Loading SharePoint Powershell Snapin" -ForegroundColor Green
$snapin = Add-PSSnapin "Microsoft.Sharepoint.Powershell"

Write-Host $snapin
}


$Webapplication =GEt-SPWebAPplication $UrlDochubWebApp
$SiteCollFilePath = "$SiteCollFilePath\ItemsCheckedOut_$(get-date -format MMddyyHHmmss).csv";

$AllLibraryNames = New-Object "System.Collections.Specialized.StringCollection"
$AllLibraryNames.Add("<LibraryName-1>") | Out-Null;
$AllLibraryNames.Add("<LibraryName-2>") | Out-Null ;
$AllLibraryNames.Add("<LibraryName-3>") | Out-Null ;

$strToBeInserted = "Site URL,Web Url,FileName,FileUrl,Created By,Created";

function LogData()
{
 Param ([string]$logstring)
Add-content $SiteCollFilePath -value $logstring
}

LogData $strToBeInserted;
if($Webapplication -ne $null)
{
$Allsites = $Webapplication.Sites
foreach($Site in $Allsites)
{
$siteUrl = $Site.Url;
$siteTitle = $site.Title;
foreach($web in $site.AllWebs)
{
try
{
$WebURl = $web.Url;
if(-not($web.Url.toLOwer() -match ("/scrapsite")))
{
foreach($list in $web.Lists)
{
$ListTitle = $list.Title
$LIstITemCount = $list.ItemCount;
if(($list.BaseType -eq "DocumentLibrary") -and ($AllLibraryNames.Contains("$ListTitle") ) -and ($LIstITemCount -gt 0))
{
$files = $list.CheckedOutFiles;
foreach($file in $files)
{
$filename = $file.LeafName;
#$fileURl = $file.URl;
$fileDIr = $file.DirName ;
$fileURl="$fileDIr/$filename";
$itemCreated = $file.TimeLastModified;
$itemCreatedBY = $file.CheckedOutByName;
$strToBeInserted = "$siteUrl,$WebURl,$filename,$fileURl,$itemCreatedBY,$itemCreated";
LogData $strToBeInserted;
}
}
}
}
}
finally
{
$web.Dispose();
}
}
}
}




No comments:

Post a Comment