Skip to content

Commit

Permalink
Merge pull request #735 from reshmee011/patch-2
Browse files Browse the repository at this point in the history
Add Enhanced Version Control sample to trim version
  • Loading branch information
pkbullock authored Sep 4, 2024
2 parents 7439697 + 43edafb commit 49b7b7e
Show file tree
Hide file tree
Showing 2 changed files with 103 additions and 37 deletions.
136 changes: 101 additions & 35 deletions scripts/spo-file-version-trimmer/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -166,65 +166,131 @@ $arraylist | Export-Csv -Path "C:\temp\versiontrimmer.csv" -NoTypeInformation -F
```
[!INCLUDE [More about PnP PowerShell](../../docfx/includes/MORE-PNPPS.md)]


# [CLI for Microsoft 365](#tab/cli-m365-ps)

```powershell
#Log in to Microsoft 365
Write-Host "Connecting to Tenant" -f Yellow
$m365Status = m365 status
if ($m365Status -match "Logged Out") {
m365 login
# [PnP PowerShell Enhanced Version Control Batch Delete Job](#tab/pnpps2)

```PowerShell
param (
[string]$siteURL,
[string]$library,
[int]$deleteOlderThanDays,
[int]$majorVersionsToKeep,
[int]$majorWithMinorVersionsToKeep,
[switch]$Automatic
)
if (-not $siteURL) {
$siteURL = Read-Host "Please enter Site URL"
}
$siteURL = Read-Host "Please enter Site URL"
$folderUrl = Read-Host "Please enter the server- or site-relative URL of the parent folder"
$versionsToKeep = Read-Host "Please enter the number of versions to keep"
$filesProcessed = @()
if (-not $library) {
$library = Read-Host "Please enter the library name, i.e. Documents or leave blank for the whole site"
}
# Get all files in the list
$files = m365 spo file list --webUrl $siteURL --folderUrl $folderUrl --recursive --output json | ConvertFrom-Json
foreach ($file in $files) {
$fileVersions = m365 spo file version list --webUrl $siteURL --fileUrl $file.ServerRelativeUrl | ConvertFrom-Json
if (-not $Automatic) {
if (-not $deleteOlderThanDays) {
$deleteOlderThanDays = Read-Host "Enter the number of days to keep versions for or leave blank or 0 to keep major minor versions"
if ($deleteOlderThanDays -eq "") {
$deleteOlderThanDays = 0
}
if ($deleteOlderThanDays -eq 0) {
$majorVersionsToKeep = Read-Host "Enter the number of major versions to keep"
$majorWithMinorVersionsToKeep = Read-Host "Enter the number of major versions with minor versions to keep"
}
} else {
Write-Host "DeleteOlderThanDays is specified. Skipping prompts for major versions to keep."
}
} else {
Write-Host "Automatic is specified. Skipping other prompts."
}
if ($fileVersions.Count -gt $versionsToKeep) {
$number = $fileVersions.Count - $versionsToKeep - 1
$removeVersionList = ($fileversions[0..$number])
Connect-PnPOnline -url $siteURL -Interactive
foreach ($versionToDelete in $removeVersionList) {
Write-Host "Removing version $($versionToDelete.VersionLabel) from the file $($file.Name)..."
m365 spo file version remove --webUrl $siteURL --fileUrl $file.ServerRelativeUrl --label $versionToDelete.VersionLabel --confirm
if ($library) {
if ($Automatic) {
New-PnPLibraryFileVersionBatchDeleteJob -Identity $library -Automatic -force
} else {
if ($deleteOlderThanDays -and $deleteOlderThanDays -gt 0) {
New-PnPLibraryFileVersionBatchDeleteJob -Identity $library -deletebeforedays $deleteOlderThanDays -force
} else {
New-PnPLibraryFileVersionBatchDeleteJob -Identity $library -MajorVersionLimit $majorVersionsToKeep -MajorWithMinorVersionsLimit $majorWithMinorVersionsToKeep -force
}
$filesProcessed += [PSCustomObject]@{
SiteUrl = $siteURL
FolderUrl = $folderUrl
FileName = $file.Name
FileUrl = $file.ServerRelativeUrl
Versions = $fileVersions.Count
}
Get-PnPLibraryFileVersionBatchDeleteJobStatus -Identity $library
} else {
if ($Automatic) {
New-PnPSiteFileVersionBatchDeleteJob -Automatic -force
} else {
if ($deleteOlderThanDays) {
New-PnPSiteFileVersionBatchDeleteJob -deletebeforedays $deleteOlderThanDays -force
} else {
New-PnPSiteFileVersionBatchDeleteJob -MajorVersionLimit $majorVersionsToKeep -MajorWithMinorVersionsLimit $majorWithMinorVersionsToKeep -force
}
}
Get-PnPSiteFileVersionBatchDeleteJobStatus
}
```

$filesProcessed | Export-Csv -Path ".\VersionTrimmer.csv" -NoTypeInformation -Encoding utf8
# [CLI for Microsoft 365](#tab/cli-m365-ps)

m365 logout
Write-Host "Finished"
```powershell
#Log in to Microsoft 365
Write-Host "Connecting to Tenant" -f Yellow
$m365Status = m365 status
if ($m365Status -match "Logged Out") {
m365 login
}
$siteURL = Read-Host "Please enter Site URL"
$folderUrl = Read-Host "Please enter the server- or site-relative URL of the parent folder"
$versionsToKeep = Read-Host "Please enter the number of versions to keep"
$filesProcessed = @()
# Get all files in the list
$files = m365 spo file list --webUrl $siteURL --folderUrl $folderUrl --recursive --output json | ConvertFrom-Json
foreach ($file in $files) {
$fileVersions = m365 spo file version list --webUrl $siteURL --fileUrl $file.ServerRelativeUrl | ConvertFrom-Json
if ($fileVersions.Count -gt $versionsToKeep) {
$number = $fileVersions.Count - $versionsToKeep - 1
$removeVersionList = ($fileversions[0..$number])
foreach ($versionToDelete in $removeVersionList) {
Write-Host "Removing version $($versionToDelete.VersionLabel) from the file $($file.Name)..."
m365 spo file version remove --webUrl $siteURL --fileUrl $file.ServerRelativeUrl --label $versionToDelete.VersionLabel --confirm
}
$filesProcessed += [PSCustomObject]@{
SiteUrl = $siteURL
FolderUrl = $folderUrl
FileName = $file.Name
FileUrl = $file.ServerRelativeUrl
Versions = $fileVersions.Count
}
}
}
$filesProcessed | Export-Csv -Path ".\VersionTrimmer.csv" -NoTypeInformation -Encoding utf8
m365 logout
Write-Host "Finished"
```

[!INCLUDE [More about CLI for Microsoft 365](../../docfx/includes/MORE-CLIM365.md)]

***
## Source Credit

The 'PnP PowerShell Enhanced Version Control Batch Delete Job' sample first appeared on [Enhanced Version Controls/Intelligent Versioning Trim with PowerShell](https://reshmeeauckloo.com/posts/powershell-enhanced-versioning-controls-trim/)

## Contributors

| Author(s) |
|-----------|
| Kasper Larsen |
| [Nanddeep Nachan](https://github.com/nanddeepn) |
| [Reshmee Auckloo (script)](https://github.com/reshmee011)|

[!INCLUDE [DISCLAIMER](../../docfx/includes/DISCLAIMER.md)]
<img src="https://m365-visitor-stats.azurewebsites.net/script-samples/scripts/spo-file-version-trimmer" aria-hidden="true" />
4 changes: 2 additions & 2 deletions scripts/spo-file-version-trimmer/assets/sample.json
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
""
],
"creationDateTime": "2023-05-12",
"updateDateTime": "2023-06-18",
"updateDateTime": "2024-09-01",
"products": [
"SharePoint",
"Graph",
Expand All @@ -18,7 +18,7 @@
"metadata": [
{
"key": "PNP-POWERSHELL",
"value": "2.1.1"
"value": "2.5.0"
},
{
"key": "CLI-FOR-MICROSOFT365",
Expand Down

0 comments on commit 49b7b7e

Please sign in to comment.