I have a SharePoint library in an O365 tenant with a large number of files/folders. The files have been shared externally with customers over the years and now we have reached to threshold limit for sharing.
We need to remove any shares for external guests that are older than 1 month
Using PowerShell, here's what I have so far
###### Declare and Initialize Variables ######
$url="<siteURL>"
$listName="Sharing Links"
$currentTime= $(get-date).ToString("yyyyMMddHHmmss")
$logFilePath=".\logs"+$currentTime+".txt"
# Fields that has to be retrieved
$Global:selectProperties=@("SharingDocId", "AvailableLinks","ShareId","AuthKey","RoleDefinitionId","Invitees","Created");
$DaysCutOff = 31
## Start the Transcript
Start-Transcript -Path $logFilePath
## Export List to CSV ##
function ExportList
{
try
{
# Get all list items using PnP cmdlet
$listItems=(Get-PnPListItem -List $listName -Fields $Global:selectProperties).FieldValues
$outputFilePath="C:\scripts\results-"+$currentTime+".csv"
$hashTable=@()
# Loop through the list items
foreach($listItem in $listItems)
{
$obj=New-Object PSObject
$listItem.GetEnumerator() | Where-Object { $_.Key -in $Global:selectProperties }| ForEach-Object{ $obj | Add-Member Noteproperty $_.Key $_.Value}
$hashTable+=$obj;
$obj=$null;
if ($listItem.Created -lt (Get-Date).AddDays(-$DaysCutOff) ){
write-host "Share Older that $DaysCutOff days"
#Delete the share <-- not sure what goes here to delete the share
}
}
$hashtable | export-csv $outputFilePath -NoTypeInformation
}
catch [Exception]
{
$ErrorMessage = $_.Exception.Message
Write-Host "Error: $ErrorMessage" -ForegroundColor Red
}
}
## Connect to SharePoint Online site
Connect-PnPOnline -Url $url -UseWebLogin
## Call the Function
ExportList
## Disconnect the context
Disconnect-PnPOnline
## Stop Transcript
Stop-Transcript
Not all global variable get values but it works so far i just don't know how to delete the share
If it look at the variable for $listitem I can see the values
Is it possible to delete base on the ShareingDocID or UniqueID fields?
Any help would be appreciated.