jueves, 29 de septiembre de 2016

Find All Documents Created or Modified by a Particular User in Specific Date Time

Requirement is to find all documents which are uploaded to the SharePoint environment during the past one Month. PowerShell can do the reporting well. Lets see the code:

?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
Add-PSSnapin Microsoft.SharePoint.PowerShell -ErrorAction SilentlyContinue
 
# Set the date Filter
$dateFilter = (Get-Date).AddMonths(-1) #Past Month
"File, Created Time, File Size" | out-file NewDocuments.csv
 
# Get all Webs
$webs = Get-SPWebApplication "http://sharepoint.crescent.com" | Get-SPSite -Limit All | Get-SPWeb -Limit All
 
#Iterate through webs
ForEach ($web in $webs)
{
   #Iterate through All Lists
    ForEach ($list in $web.Lists)
 {
  #Check for Document Libraries 
        If ($list.BaseType -eq "DocumentLibrary")
  {
     #Iterate through All documents
                   ForEach ($item in $list.Items)
   {
                  If ($item.URL.StartsWith("_")) {Break} #Skip _catalogs, etc
                  If ($item.URL.EndsWith(".aspx")) {Break} #Skip Form pages
                    If ($item.File.TimeCreated -ge $dateFilter)
    {
                      $result = """$($web.URL)/$($item.URL)"", $($item.File.TimeCreated), $( [Math]::Round($item.File.Length/1024,2))"
                      $result | Out-File NewDocuments.csv -Append
                 }
                }
        }
    }
}


Same way, we can find all documents uploaded to SharePoint sites based on Date criteria by just checking $item.File.TimeLastModified property. Say For instance,
  • You may be interested to see all old documents created an year back 
  • All documents which are not changed during the past one year.

Find All Documents Created or Modified by a Particular User:
In an another situation, the request is to find all documents either created by  modified by a particular user in SharePoint 2007.
?
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
[void][System.Reflection.Assembly]::LoadWithPartialName("Microsoft.SharePoint")
 
#Region MOSS2007-CmdLets
Function global:Get-SPWebApplication($WebAppURL)
{
 if($WebAppURL -eq $null#Get All Web Applications
    {
  #Sharepoint 2007 powershell spfarm
  $Farm = [Microsoft.SharePoint.Administration.SPFarm]::Local
  $websvcs = $farm.Services | where -FilterScript {$_.GetType() -eq [Microsoft.SharePoint.Administration.SPWebService]}
  $WebApps = @()
  foreach ($websvc in $websvcs) {
      foreach ($WebApp in $websvc.WebApplications) {
          $WebApps += $WebApp
      }
  }
  return $WebApps
 }
  else #Get Web Application for given URL
 {
    return [Microsoft.SharePoint.Administration.SPWebApplication]::Lookup($WebAppURL)
  }
 }
  
Function global:Get-SPSite()
{
  Param( [Parameter(Mandatory=$true)] [string]$SiteCollURL )
  
   if($SiteCollURL -ne '')
    {
    return new-Object Microsoft.SharePoint.SPSite($SiteCollURL)
   }
}
 
Function global:Get-SPWeb()
{
 Param( [Parameter(Mandatory=$true)] [string]$SiteURL )
  $site = Get-SPSite($SiteURL)
        if($site -ne $null)
            {
               $web=$site.OpenWeb();
            }
    return $web
}
#EndRegion
 
 
# Set the date Filter
"File Name `t Created By `t Modified By `t Created `t Last Modified `t File Size `t URL" | out-file AllDocsByUser.csv
  
# Get the Web Application
$WebApp = Get-SPWebApplication "http://sharepoint.company.com"
 
#Iterate through site collections
ForEach ($site in $WebApp.Sites)
    {
     #Iterate through webs
     ForEach ($web in $site.AllWebs)
        {
            #Iterate through All Lists
            ForEach ($list in $web.Lists)
            {
            #Check for Document Libraries
            If ($list.BaseType -eq "DocumentLibrary")
               {
                #Iterate through All documents
                ForEach ($item in $list.Items)
                    {
                        If ($item.URL.StartsWith("_")) {Break} #Skip _catalogs, etc
                        If ($item.URL.EndsWith(".aspx")) {Break} #Skip Form pages
                       
                        If ( ($item.File.Author -like "*domain\user*") -or ($item.File.ModifiedBy -like "*domain\user*"))
                           {
                             $result = "$($item.File.Name) `t $($item.File.Author) `t $($item.File.ModifiedBy) `t $($item.File.TimeCreated) `t $($item.File.TimeLastModified) `t  $( [Math]::Round($item.File.Length/1024,2)) `t $($web.URL)/$($item.URL)"
                              $result | Out-File AllDocsByUser.csv -Append
                           }
                     }
                }
           }
      }
  }


Read more: http://www.sharepointdiary.com/2012/09/find-all-documents-created-modified-by-specific-users-datetime.html#ixzz4LfsYdiFH

No hay comentarios:

Publicar un comentario