Advertisement
Guest User

Untitled

a guest
Jan 24th, 2017
136
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.79 KB | None | 0 0
  1. function Get-SPOFolderFiles
  2. {
  3. param (
  4. [Parameter(Mandatory=$true,Position=1)]
  5. [string]$Username,
  6. [Parameter(Mandatory=$true,Position=2)]
  7. [string]$Url,
  8. [Parameter(Mandatory=$true,Position=3)]
  9. $password,
  10. [Parameter(Mandatory=$true,Position=4)]
  11. [string]$ListTitle
  12. )
  13.  
  14.  
  15. $ctx=New-Object Microsoft.SharePoint.Client.ClientContext($Url)
  16. $ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username, $password)
  17. $ctx.Load($ctx.Web)
  18. $ctx.ExecuteQuery()
  19. $ll=$ctx.Web.Lists.GetByTitle($ListTitle)
  20. $ctx.Load($ll)
  21. $ctx.ExecuteQuery()
  22. $spqQuery = New-Object Microsoft.SharePoint.Client.CamlQuery
  23. $spqQuery.ViewXml ="<View Scope='RecursiveAll' />";
  24. $itemki=$ll.GetItems($spqQuery)
  25. $ctx.Load($itemki)
  26. $ctx.ExecuteQuery()
  27.  
  28. foreach($item in $itemki)
  29. {
  30.  
  31. Write-Host $item["FileRef"] -ForegroundColor DarkGreen
  32. $file =
  33. $ctx.Web.GetFileByServerRelativeUrl($item["FileRef"]);
  34. $ctx.Load($file)
  35. $ctx.Load($file.Versions)
  36. try{
  37. $ctx.ExecuteQuery() }
  38. catch
  39. {
  40. continue;
  41. }
  42. if ($file.Versions.Count -eq 0)
  43. {
  44. $obj=New-Object PSObject
  45. $obj | Add-Member NoteProperty ServerRelativeUrl($file.ServerRelativeUrl)
  46. $obj | Add-Member NoteProperty FileLeafRef($item["FileLeafRef"])
  47. $obj | Add-Member NoteProperty Versions("No Versions Available")
  48.  
  49. #$obj | export-csv -Path $CSVPath -Append
  50.  
  51. Write-Output $obj
  52. }
  53. elseif($file.TypedObject.ToString() -eq "Microsoft.SharePoint.Client.File")
  54. {
  55.  
  56. foreach ($vv in $file.Versions){
  57. Write-Host $vv.Created $vv.Size $vv.VersionLabel $vv.IsCurrentVersion $file.Versions.Count
  58.  
  59. # Write-Output $vv
  60.  
  61. }
  62. if($file.Versions[($file.Versions.Count-1)].IsCurrentVersion)
  63. {
  64. $vLabel=$file.Versions[($file.Versions.Count-2)].VersionLabel
  65. Write-Host "Version to be restored: " $vLabel
  66. }
  67. else{ $vLabel=$file.Versions[($file.Versions.Count-1)].VersionLabel
  68. Write-Host "Version to be restored: " $vLabel }
  69. $file.Versions.RestoreByLabel($vLabel)
  70. $ctx.ExecuteQuery()
  71. }
  72.  
  73. else
  74. {
  75. $obj = New-Object PSObject
  76. $obj| Add-Member NoteProperty FileLeafRef($item["FileLeafRef"])
  77. $obj | Add-Member NoteProperty Versions("No Versions Available")
  78. Write-Output $obj
  79. }
  80. <#
  81. try { $ctx.ExecuteQuery()
  82. $obj=New-Object PSObject
  83. $obj | Add-Member NoteProperty ServerRelativeUrl($file.ServerRelativeUrl)
  84. $obj | Add-Member NoteProperty FileLeafRef($item["FileLeafRef"])
  85. $obj | Add-Member NoteProperty Versions($file.Versions.Count + " versions were deleted")
  86.  
  87. $obj | export-csv -Path $CSVPath -Append
  88. }
  89. catch {
  90. $obj=New-Object PSObject
  91. $obj | Add-Member NoteProperty ServerRelativeUrl($file.ServerRelativeUrl)
  92. $obj | Add-Member NoteProperty FileLeafRef($item["FileLeafRef"])
  93. $obj | Add-Member NoteProperty Versions($file.Versions.Count + " versions. Failed to delete")
  94.  
  95. $obj | export-csv -Path $CSVPath -Append
  96.  
  97. }#>
  98.  
  99.  
  100.  
  101.  
  102. }
  103.  
  104.  
  105.  
  106.  
  107.  
  108.  
  109.  
  110. }
  111.  
  112. #Paths to SDK
  113. Add-Type -Path "c:Program FilesCommon Filesmicrosoft sharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.dll"
  114. Add-Type -Path "c:Program FilesCommon Filesmicrosoft sharedWeb Server Extensions15ISAPIMicrosoft.SharePoint.Client.Runtime.dll"
  115.  
  116. #Enter the data
  117. $AdminPassword=Read-Host -Prompt "Enter password" -AsSecureString
  118. $username="t@trial456.onmicrosoft.com"
  119. $Url="https://trial456.sharepoint.com/sites/teamsitewithlibraries"
  120. $ListTitle="uyyu"
  121.  
  122.  
  123.  
  124. Get-sPOFolderFiles -Username $username -Url $Url -password $AdminPassword -ListTitle $ListTitle
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement