Skip to content

Instantly share code, notes, and snippets.

@pmatthews05
Last active December 21, 2023 15:14
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pmatthews05/bf918f76f8fd06f3b0629327d246317a to your computer and use it in GitHub Desktop.
Save pmatthews05/bf918f76f8fd06f3b0629327d246317a to your computer and use it in GitHub Desktop.
[CmdletBinding(SupportsShouldProcess)]
param(
# The URL of the Sitecollection where the recycle bin is.
[Parameter(Mandatory)]
[string]
$SiteUrl,
# Full Path of CSV file of Get-AllRecycleBin.ps1
[Parameter(Mandatory)]
[string]
$Path
)
function Restore-RecycleBinItem {
param(
[Parameter(Mandatory)]
[String]
$Id
)
$siteUrl = (Get-PnPSite).Url
$apiCall = $siteUrl + "/_api/site/RecycleBin/RestoreByIds"
$body = "{""ids"":[""$Id""]}"
Write-Verbose "Performing API Call to Restore item from RecycleBin..."
try {
Invoke-PnPSPRestMethod -Method Post -Url $apiCall -Content $body | Out-Null
}
catch {
Write-Error "Unable to Restore ID {$Id}"
}
}
$ErrorActionPreference = 'Continue'
$InformationPreference = 'Continue'
Connect-PnPOnline -Url:$SiteUrl -UseWebLogin
@($(Import-Csv -Path:"$Path")).ForEach({
$csv = $PSItem
Write-Information -MessageData:"Restore item $($csv.Title)"
Restore-RecycleBinItem -Id $($csv.ID)
})
@ZeeGy-net
Copy link

ZeeGy-net commented Apr 20, 2020

Firstly, I wanted to thank you for writing this. this has been a massive help - it is not working for me, do you have any ideas?

`

Get-PnPAccessToken : No Azure AD connection/Access Token. Please connect first with Connect-PnPOnline -Url [url] -ClientId [clientid] -CertificatePath [path] -Tenant [tenant],
Connect-PnPOnline -Graph, Connect-PnPOnline -Scopes or Connect-PnPOnline -AppId -AppSecret -AADDomain
At C:\Temp\Restore-RecycleBinItems.ps1:50 char:16

  • $AccessToken = Get-PnPAccessToken
  •            ~~~~~~~~~~~~~~~~~~
    
    • CategoryInfo : ConnectionError: (:) [Get-PnPAccessToken], InvalidOperationException
    • FullyQualifiedErrorId : NO_OAUTH_TOKEN,SharePointPnP.PowerShell.Commands.Base.GetPnPAccessToken
      `

@pmatthews05
Copy link
Author

@ZeeGy-net thank you for getting in contact to me about this. I was not aware it wasn't working correctly, and now re-running it I have encountered the same issue as you.

I have found that if you have the SharePoint Online Management Shell installed, and run that as Administrator, then you should be able to run this script with no issues. As I have just tested that out.

I tried to simplify the script as normally I use pipelines, and AAD Client ID and Certificate, and that requires quite bit of setup, therefore I went with the Connect-PnPOnline using the pnpManagementShell instead.

Alternatively, you could create a SharePoint Client ID and Secret https://docs.microsoft.com/en-us/sharepoint/dev/solution-guidance/security-apponly-azureacs and then change line 49 to Connect-PnpOnline -url: -AppId: -AppSecret:. Again, untested, but it should work. Please come back and let me know.

@ZeeGy-net
Copy link

@pmatthews05 - Thank you so much for the speedy response, I was not expecting it, but it is very, very much appreciated.

I have tried both options, Sharepoint Online Management shell yielded the same results - This was ran on a separate VM to ensure that i didn't have anything installed that I shouldn't

script still seems to error out, even with line 49 changed. :(

@pmatthews05
Copy link
Author

AccessToken
I'm not sure what to suggest further. As you can see from the screenshot, I opened SharePoint Online Management Shell as administrator, and logged into my site, and then called the next line to get the Access Token.

Have you tried using Client ID and Secret? Although AppOnly authentiction would use Get-PnPAppAuthAccessToken.

@ZeeGy-net
Copy link

@pmatthews05 - you can tell me to do you one you know :)
Ok, I can confirm that I am now connecting via an appid and secret, I am able to connect and call Get-PNPAccessToken which returns the token.

I am unable to perform a recovery though, I am still getting errors but can almost taste success
In line 51 you are setting a variable '$AccessToken' should this not be $AppToken as this is what is looping from Line 53.
2020-04-20 17_26_17-Window

@Tony-Law
Copy link

Tony-Law commented May 1, 2020

Hi, first off thanks for all your work on putting this together, it is much appreciated and is helping immensely in getting past the daft limit in the pnponline Powershell module.

I've run into a glitch though in the restore phase and wondered if you could see anything jumping out at you. I am running the Powershell module as an admin, and I can obtain the token by logging in, but when I try the restore I am getting the following error for each item in the recycle bin. It may well be me just being daft and missing something :)

The csv file generates fine, so if you have a couple of minutes and can point me in the right direction, that would be fantastic thanks.

_**Restore-RecycleBinItem : Cannot bind argument to parameter 'AppToken' because it is an empty string.
At D:\Restore-RecycleBinItems.ps1:56 char:71

  • ... e-RecycleBinItem -SiteUrl $SiteUrl -Id $($csv.ID) -AppToken $AppToken
  •                                                             ~~~~~~~~~
    
    • CategoryInfo : InvalidData: (:) [Restore-RecycleBinItem], ParameterBindingValidationException
    • FullyQualifiedErrorId : ParameterArgumentValidationErrorEmptyStringNotAllowed,Restore-RecycleBinItem**_

token

@Tony-Law
Copy link

Tony-Law commented May 1, 2020

And now I have opened my eyes, I have spotted it, sorry to have troubled you.

In the restore-recyclebinitems.ps1 file the variable is $AccessToken but the variable called by line 56 -apptoken had $AppToken as the variable.

Changed the variable and it's working perfectly, and I am sorry to have bothered you.

Superb work by the way, it's a true lifesaver.

Kind Regards

Tony

@pmatthews05
Copy link
Author

@Tony-Law,
Thank you for spotting my mistake, yes I had the wrong parameter passed in. One called $AccessToken and the other one called $AppToken.

I have updated my Gist to correct this.

Regards

Paul

@pmatthews05
Copy link
Author

@pmatthews05 - you can tell me to do you one you know :)
Ok, I can confirm that I am now connecting via an appid and secret, I am able to connect and call Get-PNPAccessToken which returns the token.

I am unable to perform a recovery though, I am still getting errors but can almost taste success
In line 51 you are setting a variable '$AccessToken' should this not be $AppToken as this is what is looping from Line 53.
2020-04-20 17_26_17-Window

Sorry @ZeeGy-net, I've just seen this comment, and noticed you too told me that my variable was incorrect. Thank you to you too.

@SharkEyes93
Copy link

SharkEyes93 commented Dec 20, 2023

This was very useful thank you.

I am currently restoring ~150,000 items. I've used Co-Pilot to add a counter to the my script. You may find it useful

# Get the total number of rows in the CSV file
$TotalRows = (Import-Csv -Path:"$Path").Count

# Initialize a counter for the current row
$CSVRow = 0

# Loop through each row in the CSV file
@($(Import-Csv -Path:"$Path")).ForEach({
    $csv = $PSItem

    # Increment the counter by one
    $CSVRow++

    # Write the information message with the desired format
    Write-Information -MessageData:"Restoring item $CSVRow of $TotalRows. Item $($csv.Title)"

    # Restore the recycle bin item with the given ID
    Restore-RecycleBinItem -Id $($csv.ID)
})

@pmatthews05
Copy link
Author

Hi @SharkEyes93 thank you for your suggestion.

There is a newer better version of this script which can be found at PNP Script Samples. It batches and runs a bit quicker.

https://pnp.github.io/script-samples/bulk-restore-from-recyclebin/README.html?tabs=pnpps

Feel free to Co-pilot the script and improve on it, I think you will get a PNP PowerShell script badge if you make any updates that get accepted into the Pull Request.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment