Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Added Get-PnPFolderStorageMetric #2646

Merged
merged 10 commits into from
Jan 12, 2023
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).
- Added `-EnableAzureADB2BIntegration` and `-SyncAadB2BManagementPolicy` parameters to `Set-PnPTenant` [#2631](https://github.com/pnp/powershell/pull/2631)
- Added `-ShowInFiltersPane` to `Set-PnPField` which allows fields to be shown or hidden in the filters pane [#2623](https://github.com/pnp/powershell/pull/2632)
- Added `-KeyColumn` to `Add-PnPDataRowsToSiteTemplate` which allows for overwriting existing list items in a site template [#2616](https://github.com/pnp/powershell/pull/2616)
- Added `Get-PnPFolderStorageMetric` which allows storage usage of a specific folder to be retrieved [#2646](https://github.com/pnp/powershell/pull/2646)
- Added `IsTeamsConnected`, `IsTeamsChannelConnected` and `TeamChannelType` to be returned when `Get-PnPTenantSite` cmdlet is executed. [#2656](https://github.com/pnp/powershell/pull/2656)
- Added `-EnvironmentVariable` parameter to `Connect-PnPOnline` to connect using Azure environment variables. [#2681](https://github.com/pnp/powershell/pull/2681)
- Added `ResponseHeadersVariable` parameter to the `Invoke-PnPSPRestMethod` which if specified will store the response headers values in the PowerShell variable name that is specified. [#2711](https://github.com/pnp/powershell/pull/2711)
Expand Down Expand Up @@ -51,6 +52,7 @@ The format is based on [Keep a Changelog](http://keepachangelog.com/en/1.0.0/).

### Contributors

- Robin Meure [robinmeure]
- Rohit Varghese [rohitvarghese96]
- Arleta Wanat [PowershellScripts]
- Erwin van Hunen [erwinvanhunen]
Expand Down
111 changes: 111 additions & 0 deletions documentation/Get-PnPFolderStorageMetric.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,111 @@
---
Module Name: PnP.PowerShell
schema: 2.0.0
applicable: SharePoint Online
online version: https://pnp.github.io/powershell/cmdlets/Get-PnPFolderStorageMetric.html
external help file: PnP.PowerShell.dll-Help.xml
title: Get-PnPFolderStorageMetric
---

# Get-PnPFolderStorageMetric

## SYNOPSIS
Allows retrieval of storage metrics for a folder in SharePoint Online

## SYNTAX

### Folder via site relative URL (Default)
```powershell
Get-PnPFolderStorageMetric [-FolderSiteRelativeUrl <String>] [-Connection <PnPConnection>]
```

### Folder via pipebind
```powershell
Get-PnPFolderStorageMetric -List <ListPipeBind> [-Connection <PnPConnection>]
```

### Folder via list
```powershell
Get-PnPFolderStorageMetric -Identity <FolderPipeBind> [-Connection <PnPConnection>]
```

## DESCRIPTION
Allows retrieval of storage metrics for a folder in SharePoint Online. It will reveal the true storage usage, similar to what will be shown through storman.aspx, the date and time the folder was last modified and the amount of files inside the folder.

Please note that there is a delay of typically just a few minutes between making changes to files on a site and this cmdlet returning updated values.

## EXAMPLES

### EXAMPLE 1
```powershell
Get-PnPFolderStorageMetric
```
Retrieves the storage metrics of the current site/web

### EXAMPLE 2
```powershell
Get-PnPFolderStorageMetric -List "Documents"
```
Retrieves the storage metrics of the specified document library

### EXAMPLE 3
```powershell
Get-PnPFolderStorageMetric -FolderSiteRelativeUrl "Shared Documents"
```
Retrieves the storage metrics of the folder using the server-relative Url

### EXAMPLE 4
```powershell
$folder = Get-PnPFolder -Url "Shared Documents"
Get-PnPFolderStorageMetric -Identity $folder
```

Retrieves the storage metrics of the folder using the identity parameter

## PARAMETERS

### -FolderSiteRelativeUrl
The path to the folder to query for its storage consumption, relative to the SharePoint Online site to which the connection has been made, i.e. "Shared Documents\Subfolder"

```yaml
Type: String
Parameter Sets: Folder via site relative URL

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```

### -List
Id, name or instance of a list to query for its storage consumption

```yaml
Type: ListPipeBind
Parameter Sets: Folder via list

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```

### -Identity
Id, name or instance of a folder to query for its storage consumption

```yaml
Type: FolderPipeBind
Parameter Sets: Folder via pipebind

Required: False
Position: Named
Default value: None
Accept pipeline input: False
Accept wildcard characters: False
```

## RELATED LINKS

[Microsoft 365 Patterns and Practices](https://aka.ms/m365pnp)
103 changes: 103 additions & 0 deletions src/Commands/Files/GetFolderStorageMetric.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
using System;
using System.Management.Automation;
using Microsoft.SharePoint.Client;
using PnP.Core.Model.SharePoint;
using PnP.Framework.Utilities;
using PnP.PowerShell.Commands.Base.PipeBinds;

namespace PnP.PowerShell.Commands.Files
{
[Cmdlet(VerbsCommon.Get, "PnPFolderStorageMetric", DefaultParameterSetName = ParameterSet_BYSITRERELATIVEURL)]
[OutputType(typeof(Model.SharePoint.FolderStorageMetric))]
public class GetFolderStorageMetric : PnPWebCmdlet
{
private const string ParameterSet_BYSITRERELATIVEURL = "Folder via site relative URL";
private const string ParameterSet_BYLIST = "Folder via list";
private const string ParameterSet_BYFOLDER = "Folder via pipebind";

[Parameter(Mandatory = false, ValueFromPipeline = true, ParameterSetName = ParameterSet_BYSITRERELATIVEURL)]
public string FolderSiteRelativeUrl;

[Parameter(Mandatory = true, ValueFromPipeline = true, ParameterSetName = ParameterSet_BYLIST)]
[ValidateNotNullOrEmpty]
public ListPipeBind List;

[Parameter(Mandatory = true, ValueFromPipeline = true, Position = 0, ParameterSetName = ParameterSet_BYFOLDER)]
[ValidateNotNullOrEmpty]
public FolderPipeBind Identity;

protected override void ExecuteCmdlet()
{
Folder targetFolder = null;
switch (ParameterSetName)
{
case ParameterSet_BYFOLDER:
targetFolder = Identity.GetFolder(CurrentWeb);
break;
case ParameterSet_BYLIST:
var list = List.GetList(CurrentWeb);
if (list != null)
{
targetFolder = list.RootFolder;
}
break;

case ParameterSet_BYSITRERELATIVEURL:
string serverRelativeUrl = null;
if (!string.IsNullOrEmpty(FolderSiteRelativeUrl))
{
if(FolderSiteRelativeUrl == "Microsoft.SharePoint.Client.Folder")
{
throw new PSArgumentException($"Please pass in a Folder instance using the -{nameof(Identity)} parameter instead of piping it to this cmdlet");
}
else
{
var webUrl = CurrentWeb.EnsureProperty(w => w.ServerRelativeUrl);
serverRelativeUrl = UrlUtility.Combine(webUrl, FolderSiteRelativeUrl);
}
}

targetFolder = (string.IsNullOrEmpty(FolderSiteRelativeUrl)) ? CurrentWeb.RootFolder : CurrentWeb.GetFolderByServerRelativePath(ResourcePath.FromDecodedUrl(serverRelativeUrl));
break;

default:
throw new NotImplementedException($"Parameter set {ParameterSetName} not implemented");
}

if (targetFolder != null)
{
try
{
ClientContext.Load(targetFolder, t => t.ServerRelativeUrl);
ClientContext.ExecuteQueryRetry();
}
catch(Microsoft.SharePoint.Client.ServerException e)
{
if (e.ServerErrorTypeName == "System.IO.FileNotFoundException")
{
throw new PSArgumentException("The provided list or folder does not exist");
}
else
{
throw;
}
}

IFolder folderWithStorageMetrics = PnPContext.Web.GetFolderByServerRelativeUrlAsync(targetFolder.ServerRelativeUrl, f => f.StorageMetrics).GetAwaiter().GetResult();
var storageMetrics = folderWithStorageMetrics.StorageMetrics;

WriteObject(new Model.SharePoint.FolderStorageMetric
{
LastModified = storageMetrics.LastModified,
TotalFileCount = storageMetrics.TotalFileCount,
TotalFileStreamSize = storageMetrics.TotalFileStreamSize,
TotalSize = storageMetrics.TotalSize
});
}
else
{
throw new PSArgumentException($"Can't find the specified folder.");
}
}
}
}
33 changes: 33 additions & 0 deletions src/Commands/Model/SharePoint/FolderStorageMetric.cs
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@

using System;

namespace PnP.PowerShell.Commands.Model.SharePoint
{
/// <summary>
/// Contains storage metrics of a folder
/// </summary>
public class FolderStorageMetric
{
/// <summary>
/// Gets the last modified date and time of the storage resource
/// </summary>
public DateTime LastModified { get; set; }

/// <summary>
/// Gets the total count of files in the storage resource
/// </summary>
public long TotalFileCount { get; set; }

/// <summary>
/// Gets the total stream size of the storage resource
/// </summary>
public long TotalFileStreamSize { get; set; }

/// <summary>
/// Gets the total size of the storage resource
/// </summary>
public long TotalSize { get; set; }
}
}