|
| 1 | +function Get-CIPPSiteVersionCleanupStatus { |
| 2 | + <# |
| 3 | + .SYNOPSIS |
| 4 | + Get the progress of a file version batch delete (trim) job for a SharePoint site |
| 5 | +
|
| 6 | + .DESCRIPTION |
| 7 | + Queries the progress of the file version batch delete job for a SharePoint site via the |
| 8 | + CSOM GetFileVersionBatchDeleteJobProgress method on the Tenant object, using the same |
| 9 | + ProcessQuery channel as Start-CIPPSiteVersionCleanup. Reports the status of a cleanup |
| 10 | + previously started with Start-CIPPSiteVersionCleanup. |
| 11 | +
|
| 12 | + Unlike NewFileVersionBatchDeleteJob / RemoveFileVersionBatchDeleteJob (which return an |
| 13 | + SpoOperation object that the client serialises with a <Query SelectAllProperties="true">), |
| 14 | + GetFileVersionBatchDeleteJobProgress returns a plain String whose content is a JSON blob. |
| 15 | + It is therefore invoked as a bare <Method> inside <Actions> with no <Query> wrapper - asking |
| 16 | + for SelectAllProperties on a String fails server-side with "Cannot find stub for type |
| 17 | + System.String". The ProcessQuery response is an array whose only String element is the JSON |
| 18 | + progress payload, which this function parses and returns. (Confirmed against a captured |
| 19 | + Get-SPOSiteFileVersionBatchDeleteJobProgress request.) |
| 20 | +
|
| 21 | + .PARAMETER TenantFilter |
| 22 | + Tenant to query |
| 23 | +
|
| 24 | + .PARAMETER SiteUrl |
| 25 | + Full URL of the SharePoint site to query |
| 26 | +
|
| 27 | + .EXAMPLE |
| 28 | + Get-CIPPSiteVersionCleanupStatus -TenantFilter 'contoso.onmicrosoft.com' -SiteUrl 'https://contoso.sharepoint.com/sites/MySite' |
| 29 | +
|
| 30 | + .FUNCTIONALITY |
| 31 | + Internal |
| 32 | +
|
| 33 | + #> |
| 34 | + [CmdletBinding()] |
| 35 | + param( |
| 36 | + [Parameter(Mandatory = $true)] |
| 37 | + [string]$TenantFilter, |
| 38 | + [Parameter(Mandatory = $true)] |
| 39 | + [string]$SiteUrl |
| 40 | + ) |
| 41 | + |
| 42 | + $SharePointInfo = Get-SharePointAdminLink -Public $false -tenantFilter $TenantFilter |
| 43 | + $AdminUrl = $SharePointInfo.AdminUrl |
| 44 | + $EscapedSiteUrl = [System.Security.SecurityElement]::Escape($SiteUrl) |
| 45 | + |
| 46 | + # CSOM pattern: Tenant Constructor -> GetFileVersionBatchDeleteJobProgress(siteUrl). |
| 47 | + # The method returns a String (JSON), so it is called directly in <Actions> with no <Query>. |
| 48 | + $XML = @" |
| 49 | +<Request AddExpandoFieldTypeSuffix="true" SchemaVersion="15.0.0.0" LibraryVersion="16.0.0.0" ApplicationName="SharePoint Online PowerShell (16.0.24908.0)" xmlns="http://schemas.microsoft.com/sharepoint/clientquery/2009"><Actions><ObjectPath Id="40" ObjectPathId="39" /><Method Name="GetFileVersionBatchDeleteJobProgress" Id="41" ObjectPathId="39"><Parameters><Parameter Type="String">$EscapedSiteUrl</Parameter></Parameters></Method></Actions><ObjectPaths><Constructor Id="39" TypeId="{268004ae-ef6b-4e9b-8425-127220d84719}" /></ObjectPaths></Request> |
| 50 | +"@ |
| 51 | + |
| 52 | + $AdditionalHeaders = @{ |
| 53 | + 'Accept' = 'application/json;odata=verbose' |
| 54 | + } |
| 55 | + |
| 56 | + $Response = New-GraphPostRequest -scope "$AdminUrl/.default" -tenantid $TenantFilter -Uri "$AdminUrl/_vti_bin/client.svc/ProcessQuery" -Type POST -Body $XML -ContentType 'text/xml' -AddedHeaders $AdditionalHeaders |
| 57 | + |
| 58 | + # ProcessQuery returns a JSON array; if it came back as raw text, parse it first. |
| 59 | + if ($Response -is [string]) { |
| 60 | + $Response = $Response | ConvertFrom-Json |
| 61 | + } |
| 62 | + |
| 63 | + # The first array element carries ErrorInfo for the whole request. |
| 64 | + $ErrorInfo = $Response | Where-Object { $_.PSObject.Properties.Name -contains 'ErrorInfo' } | Select-Object -First 1 |
| 65 | + if ($ErrorInfo.ErrorInfo) { |
| 66 | + throw "SharePoint returned an error querying version cleanup status for $SiteUrl : $($ErrorInfo.ErrorInfo.ErrorMessage)" |
| 67 | + } |
| 68 | + |
| 69 | + # GetFileVersionBatchDeleteJobProgress returns its payload as the only String element. |
| 70 | + $ProgressJson = $Response | Where-Object { $_ -is [string] } | Select-Object -First 1 |
| 71 | + |
| 72 | + if ([string]::IsNullOrWhiteSpace($ProgressJson)) { |
| 73 | + return [PSCustomObject]@{ |
| 74 | + SiteUrl = $SiteUrl |
| 75 | + Status = 'NoJob' |
| 76 | + Message = 'No file version batch delete job found for this site.' |
| 77 | + } |
| 78 | + } |
| 79 | + |
| 80 | + $Progress = $ProgressJson | ConvertFrom-Json |
| 81 | + Add-Member -InputObject $Progress -MemberType NoteProperty -Name 'SiteUrl' -Value $SiteUrl -Force |
| 82 | + return $Progress |
| 83 | +} |
0 commit comments