Azure Automation Account Runbook Error - Powershell PNP -

Copper Contributor

Hello All,

for our archiving concept we have set up for each of our SharePoints a retention policy as meta data. When the archiving starts the script is being started which checks for all library items which have "Temporary" set and deletes them. Also we are having libaries which exceed the treshold of the SharePoint (>5000 items). I have tried such things as adjusting the batch, adding sleep timers ... but nothing worked. Anyone has an idea on this?

In my test I have created 36000 dummy files. Locally the script starts and deletes everything but within azure automation account I get following error:
Get-PnPListItem: Line | 17 | $FilteredItems = Get-PnPListItem -List $ListName -PageSize 5 | Where- … | ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~ | Exception of type 'System.OutOfMemoryException' was thrown.

Here's the script:

 

#Config Variables
param(
    [string]$SiteURL
)

$TenantURL = "YourAdminURL"
$ListName = "Dokumente"
$RetentionPolicyValue = "Temporary"
$ConfURLDE = $SiteURL + "-01_Ablage_vertraulich"
$ConfURLEN = $SiteURL + "-01_Repository_confidential"
$ConfListName = "Dokumente"

#Connect to PnP Online
Connect-PnPOnline -Url $SiteURL -ManagedIdentity

#Get all list items from list in batches and filter for documents with Retention Policy set to "Temporary"
$FilteredItems = Get-PnPListItem -List $ListName -PageSize 5 | Where-Object { $_["Retention_x0020_Policy"] -eq $RetentionPolicyValue -and $_.FileSystemObjectType -eq "File" }

#Loop through each Item
ForEach($Item in $FilteredItems)
    #Delete the item
    Remove-PnPListItem -List $ListName -Identity $Item.Id -Force -Recycle
    #Clear memory
    Remove-Variable Item
}

Disconnect-PnPOnline

#Pause for 60 seconds
Start-Sleep -Seconds 60

Connect-PnPOnline -Url $TenantURL -ManagedIdentity

#Confidential Channel Read Only
if (Get-PnPTenantSite -Url $ConfURLDE -ErrorAction SilentlyContinue)
{
    Connect-PnPOnline -Url $ConfURLDE -ManagedIdentity
}
if (Get-PnPTenantSite -Url $ConfURLEN -ErrorAction SilentlyContinue)
{
    Connect-PnPOnline -Url $ConfURLEN -ManagedIdentity
}

#Get All Items and filter for documents with Retention Policy set to "Temporary"
$ConfFilteredItems = Get-PnPListItem -List $ConfListName -PageSize 500 | Where-Object { $_["Retention_x0020_Policy"] -eq $RetentionPolicyValue -and $_.FileSystemObjectType -eq "File" }


#Loop through each Item
ForEach($Item in $ConfFilteredItems)
    #Delete the item
    Remove-PnPListItem -List $ConfListName -Identity $Item.Id -Force -Recycle
    #Clear memory
    Remove-Variable Item
}

Disconnect-PnPOnline

 

0 Replies