1

I'm trying to delete all the second stage bins of all Sites in Sharepoint Online available. My idea is to get all the sites via Get-SPOSite | Select URL and process them with a foreach loop:


    #Load SharePoint Online Assemblies
    
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.dll"
    Add-Type -Path "C:\Program Files\Common Files\Microsoft Shared\Web Server Extensions\16\ISAPI\Microsoft.SharePoint.Client.Runtime.dll"
    
    
    ##Global Vars
    
    $UserName = "XXX"
    $Password = "XXX"
    $Credentials = New-Object -TypeName System.Management.Automation.PSCredential -argumentlist $UserName, $(convertto-securestring $Password -asplaintext -force)
    
    ##Connect to SPOnline
    
    Connect-SPOService -Url https://XXX-admin.sharepoint.com -Credential $Credentials
       
    
    ##Variables for Processing
    
    $Sites = Get-SPOSite | Select URL
    
    
    ##Go for every item.
    
    foreach ($Site in $Sites) {   
        
        Try {
        $Context = New-Object Microsoft.SharePoint.Client.ClientContext($Site) ##Context for deletion = SiteURL
        $Context.Credentials = $Credentials
        $Context.Site.RecycleBin.DeleteAllSecondStageItems()  
        $Context.ExecuteQuery()
        Write-Host "Done: " + $Site
        }
    
        catch {
        write-host "Error: $($_.Exception.Message)" -foregroundcolor Yellow
        }
    
    }

Now, this throws an Error in catch (Sorry for german):

Error: Für "ClientContext" und die folgende Argumenteanzahl kann keine Überladung gefunden werden: "1"

Trying to translate into english: Error: There can't be an overload for "ClientContext" and the following number of arguments: 1

I think overload is a translation error in german. Maybe "overlap"?

I think it's basically saying that $Site is not an Uri. What can I do?

Greetz

  • 2
    Try `Select -ExpandProperty URL`. That way you get a **string** array of urls, not **objects** with a property called `.URL` – Theo Apr 07 '22 at 20:12
  • In short: [`Select-Object`](https://learn.microsoft.com/powershell/module/microsoft.powershell.utility/select-object) (`select`) by default returns _a `[pscustomobject]` instance_ that has the _requested properties_ - even when you're only asking for a _single_ property. To get only that property's _value_, use `-ExpandProperty ` instead - see the duplicate for details and alternatives. – mklement0 Apr 07 '22 at 20:38

0 Answers0