Advertisement
Guest User

Untitled

a guest
Apr 4th, 2018
283
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.34 KB | None | 0 0
  1. # Paths to SDK libraries.
  2. Add-Type -Path 'C:WindowsMicrosoft.NETassemblyGAC_MSILMicrosoft.SharePoint.Clientv4.0_16.0.0.0__71e9bce111e9429cMicrosoft.SharePoint.Client.dll'
  3. Add-Type -Path 'C:WindowsMicrosoft.NETassemblyGAC_MSILMicrosoft.SharePoint.Client.Runtimev4.0_16.0.0.0__71e9bce111e9429cMicrosoft.SharePoint.Client.Runtime.dll'
  4. Add-Type -Path 'C:WindowsMicrosoft.NETassemblyGAC_MSILMicrosoft.SharePoint.Client.UserProfilesv4.0_16.0.0.0__71e9bce111e9429cMicrosoft.SharePoint.Client.UserProfiles.dll'
  5.  
  6. $global:counter = 0
  7.  
  8. # Merges two sorted halves of a subarray
  9. # $theArray is an array of comparable objects
  10. # $tempArray is an array to place the merged result
  11. # $leftPos is the left-most index of the subarray
  12. # $rightPos is the index of the start of the second half
  13. # $rightEnd is the right-most index of the subarray
  14. function merge($theArray, $tempArray, [int] $leftPos, [int] $rightPos, [int] $rightEnd)
  15. {
  16. $leftEnd = $rightPos - 1
  17. $tmpPos = $leftPos
  18. $numElements = $rightEnd - $leftPos + 1
  19.  
  20. # Main loop
  21. while (($leftPos -le $leftEnd) -and ($rightPos -le $rightEnd))
  22. {
  23. $global:counter++
  24. if ($theArray[$leftPos].DeletedDate.CompareTo($theArray[$rightPos].DeletedDate) -ge 0)
  25. #if ($theArray[$leftPos].DeletedDate.CompareTo($theArray[$rightPos].DeletedDate) -le 0)
  26. {
  27. $tempArray[$tmpPos++] = $theArray[$leftPos++]
  28. }
  29. else
  30. {
  31. $tempArray[$tmpPos++] = $theArray[$rightPos++]
  32. }
  33. }
  34.  
  35. while ($leftPos -le $leftEnd)
  36. {
  37. $tempArray[$tmpPos++] = $theArray[$leftPos++]
  38. }
  39.  
  40. while ($rightPos -le $rightEnd)
  41. {
  42. $tempArray[$tmpPos++] = $theArray[$rightPos++]
  43. }
  44.  
  45. # Copy $tempArray back
  46. for ($i = 0; $i -lt $numElements; $i++, $rightEnd--)
  47. {
  48. $theArray[$rightEnd] = $tempArray[$rightEnd]
  49. }
  50. }
  51.  
  52. # Makes recursive calls
  53. # $theArray is an array of comparable objects
  54. # $tempArray is an array to place the merged result
  55. # $left is the left-most index of the subarray
  56. # $right is the right-most index of the subarray
  57. function mergesorter( $theArray, $tempArray, [int] $left, [int] $right )
  58. {
  59. if ($left -lt $right)
  60. {
  61. [int] $center = [Math]::Floor(($left + $right) / 2)
  62. mergesorter $theArray $tempArray $left $center
  63. mergesorter $theArray $tempArray ($center + 1) $right
  64. merge $theArray $tempArray $left ($center + 1) $right
  65. }
  66. }
  67.  
  68. $theArray = @()
  69. $theLimitedArray = New-Object Collections.ArrayList
  70.  
  71. function limitRecycleBinItemsCount($arrayToLimit,$dateLimit)
  72. {
  73. $tcount = 0
  74. foreach($item2 in $arrayToLimit) {
  75. $itemFormatedDateLimit = $item2.DeletedDate.ToString('yyyy-MM-dd HH:mm:ss')
  76. if ($itemFormatedDateLimit -ge $dateLimit) {
  77. $theLimitedArray.Add($item2)
  78. }
  79. }
  80. }
  81.  
  82. $User = "UserName@Company.com"
  83. $Password = ConvertTo-SecureString β€˜PASSWORD’ -AsPlainText -Force
  84. $siteUrl ="https://company-my.sharepoint.com/personal/UserName_Company_com"
  85.  
  86. $startDate = "2015-09-03 00:00:00"
  87. $endDate = "2018-04-05 23:59:59"
  88. $csvFileName = "c:tmpreport.csv"
  89.  
  90. $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($siteUrl)
  91. $credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($User, $Password)
  92. $ctx.Credentials = $credentials
  93.  
  94. try {
  95. #Get RecycleBin
  96. $rb=$ctx.Site.RecycleBin
  97. $ctx.Load($rb)
  98. $ctx.ExecuteQuery()
  99.  
  100. #Load recycle bin to array
  101. $theArray = @($rb)
  102. #Sort array recycle bin items with sorting algorithm 'Merge'
  103. $tempArray = New-Object Object[] $theArray.Count
  104. mergesorter $theArray $tempArray 0 ($theArray.Count - 1)
  105.  
  106. Write-Host "Array items: `t" $theArray.Count
  107. Write-Host "Iterations: `t" $global:counter
  108. #Limit recycle bin items count to date
  109. limitRecycleBinItemsCount $theArray $startDate
  110. #Sort limited array recycle bin items with sorting algorithm 'Merge'
  111. $tempArray2 = New-Object Object[] $theLimitedArray.Count
  112. mergesorter $theLimitedArray $tempArray2 0 ($theLimitedArray.Count - 1)
  113. Write-Host "Limited Array items: `t" $theLimitedArray.Count
  114.  
  115. $Target = @()
  116. $csvReport = @()
  117.  
  118. foreach($item in $theLimitedArray) {
  119. #Format item deleted date
  120. $itemFormatedDate = $item.DeletedDate.ToString('yyyy-MM-dd HH:mm:ss')
  121. #Check if item deleted date is greather then search filtre start dat
  122. if ($itemFormatedDate -ge $startDate) {
  123. #Check if item deleted is between start date and end date (timestamp)
  124. if ($itemFormatedDate -ge $startDate -and $itemFormatedDate -le $endDate){
  125. $t = "unknown"
  126. $dirName = $item.DirName
  127. $ctx.Load($item.DeletedBy)
  128. $ctx.ExecuteQuery()
  129. $splitDelimited = $item.DeletedBy.LoginName.split("|")
  130. $atDelimited = $splitDelimited[2].split("@")
  131. #Check the user who deleted items
  132. #if ( $atDelimited[0].ToString() -eq $userWhoDeletedFiles){
  133. $details = @{
  134. DeletedItem = $item;
  135. DeletedItemID = $item.Id;
  136. DeletedItemTitle = $item.Title;
  137. DeletedBy = $atDelimited[0].ToString();
  138. DeletedDate = $item.DeletedDate;
  139. DeletedPath = $item.DirName;
  140. DeletedItemType = $item.ItemType
  141. }
  142. $csvReport += New-Object PSObject -Property $details
  143. Write-Host "Deleted item:" $item.Title, "by " $atDelimited[0].ToString(), "Deleted Date: " $item.DeletedDate, "Location: " $item.DirName, "Item type: " $item.ItemType
  144. # }
  145. $TargetObject = New-Object PSObject -Property @{
  146. DirName = $dirName
  147. }
  148. $Target += $TargetObject
  149. }
  150. }
  151. else { break
  152. }
  153.  
  154. }
  155. #Report output to CSV file
  156. $csvReport | sort-object -property @{Expression="DeletedItemType";Descending=$true}, @{Expression="DeletedDate";Descending=$true}, @{Expression="DeletedPath";Descending=$false} | export-csv -Path $csvFileName -NoTypeInformation -Encoding UTF8
  157. #Sort selected items that were deleted by user. Folders first, then files, Deleted dates descending order and item path to build a hierarhy
  158. $csvReport | sort-object -property @{Expression="DeletedItemType";Descending=$true}, @{Expression="DeletedDate";Descending=$true}, @{Expression="DeletedPath";Descending=$false} | ForEach-Object {
  159. Write-Host "Restored - Deleted Item Type: " $_."DeletedItemType" "| Title: " $_."DeletedItemTitle" "| Deleted by: " $_."DeletedBy" "| Deleted Date: " $_."DeletedDate" "| Location: " $_."DeletedPath"
  160. try {
  161. $_.DeletedItem.Restore()
  162. $ctx.ExecuteQuery()
  163. Write-Host -f yellow "Item was restored"
  164. } catch {
  165. Write-Host -f Red "***The file name" $csvReport."DeletedItemTitle" "is already in location" $csvReport."DeletedPath"
  166. }
  167. }
  168. $Target | Group-Object DirName | %{
  169. New-Object psobject -Property @{
  170. DirName = $_.Name
  171. Count = $_.Group.Count
  172. }
  173. }
  174. }
  175. catch [System.Exception] {
  176. write-host -f red $_.Exception.ToString()
  177. }
  178. Write-Host -ForegroundColor Yellow "Finished"
  179. $ctx.Dispose()
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement