1- function Get-DeletedItems
2- {
3- param (
4- [Parameter (Mandatory = $true , Position = 1 )]
1+ function Get-DeletedItems {
2+ param (
3+ [Parameter (Mandatory = $true , Position = 1 )]
54[string ]$Username ,
65[Parameter (Mandatory = $true , Position = 2 )]
76$AdminPassword ,
8- [Parameter (Mandatory = $true , Position = 3 )]
7+ [Parameter (Mandatory = $true , Position = 3 )]
98[string ]$Url
10- )
11- # $password = ConvertTo-SecureString -string $AdminPassword -AsPlainText -Force
12- $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($Url )
13- $ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username , $AdminPassword )
14-
15- try
16- {
17- $ctx.ExecuteQuery ()
18- } catch [Net.WebException ]
19- {
20-
21- Write-Host $Url " failed to connect to the site" $_.Exception.Message.ToString () - ForegroundColor Red
22- }
23-
24- $ctx.Load ($ctx.Site )
25- $ctx.Load ($ctx.Web.Webs )
26- $rb = $ctx.Site.RecycleBin
27- $ctx.Load ($rb )
28- try
29- {
30- $ctx.ExecuteQuery ()
31- Write-Host $ctx.Site.Url " Items in the recycle bin: " $rb.Count.ToString ()
32- } catch [Net.WebException ]
33- {
34-
35- Write-Host $ctx.Site.Url " failed" $_.Exception.Message.ToString () - ForegroundColor Red
36-
37- }
38-
39- $myarray = @ ()
40- for ($i = 0 ;$i -lt $rb.Count ;$i ++ )
41- {
42-
43- $obj = $rb [$i ]
44- $myarray += $obj
45-
46- }
47-
48- Write-Host " Items to process: " $myarray.Count
49-
50- for ($i = 0 ;$i -lt $myarray.Count ; $i ++ ){
51-
52- $myarray [$i ].Restore()
53- try {
54- $ctx.ExecuteQuery ()
55- Write-Host $myarray [$i ].LeafName " restored" - ForegroundColor Green
56- }
57- catch [Net.WebException ]
58- {
59- Write-Host $myarray [$i ].LeafName " failed" $_.Message.ToString () - ForegroundColor Red
60- }
61-
62- }
63-
64-
65-
66-
67-
9+ )
10+ # $password = ConvertTo-SecureString -string $AdminPassword -AsPlainText -Force
11+ $ctx = New-Object Microsoft.SharePoint.Client.ClientContext($Url )
12+ $ctx.Credentials = New-Object Microsoft.SharePoint.Client.SharePointOnlineCredentials($Username , $AdminPassword )
13+
14+ try {
15+ $ctx.ExecuteQuery ()
16+ }
17+ catch [Net.WebException ] {
18+ Write-Host $Url " failed to connect to the site" $_.Exception.Message.ToString () - ForegroundColor Red
19+ }
20+
21+ $ctx.Load ($ctx.Site )
22+ $ctx.Load ($ctx.Web.Webs )
23+ $rb = $ctx.Site.RecycleBin
24+ $ctx.Load ($rb )
25+
26+ try {
27+ $ctx.ExecuteQuery ()
28+ Write-Host $ctx.Site.Url " Items in the recycle bin: " $rb.Count.ToString ()
29+ }
30+ catch [Net.WebException ] {
31+ Write-Host $ctx.Site.Url " failed" $_.Exception.Message.ToString () - ForegroundColor Red
32+ }
33+
34+ $myarray = @ ()
35+
36+ for ($i = 0 ;$i -lt $rb.Count ;$i ++ ){
37+ $obj = $rb [$i ]
38+ $myarray += $obj
39+ }
40+
41+ Write-Host " Items to process: " $myarray.Count
42+
43+ for ($i = 0 ;$i -lt $myarray.Count ; $i ++ ){
44+ $myarray [$i ].Restore()
45+
46+ try {
47+ $ctx.ExecuteQuery ()
48+ Write-Host $myarray [$i ].LeafName " restored" - ForegroundColor Green
49+ }
50+ catch [Net.WebException ]{
51+ Write-Host $myarray [$i ].LeafName " failed" $_.Message.ToString () - ForegroundColor Red
52+ }
53+ }
6854}
6955
7056
@@ -81,11 +67,8 @@ $AdminUrl="https://trial765-admin.sharepoint.com"
8167Connect-SPOService - Url $adminUrl
8268$sites = (Get-SPOSite ).Url
8369
84- foreach ($site in $sites )
85- {
86-
87- Get-DeletedItems - Username $Username - AdminPassword $AdminPassword - Url $site
88-
70+ foreach ($site in $sites ){
71+ Get-DeletedItems - Username $Username - AdminPassword $AdminPassword - Url $site
8972}
9073
9174
0 commit comments