SCRT-HQ / PSGSuite

Powershell module for Google / G Suite API calls wrapped in handy functions. Authentication is established using a service account via P12 key to negate the consent popup and allow for greater handsoff automation capabilities
https://psgsuite.io/
Apache License 2.0
235 stars 67 forks source link

Get Folder Size ! #204

Closed chrispaivacwb closed 5 years ago

chrispaivacwb commented 5 years ago

Is your feature request related to a problem? Please describe. I am in the process of Migration from GSuite to Office365 and I need to know the amount of data I need to manage in this migration. Google Admin Console shows a infomations of usage resources but I am not sure if this info does include Shared Team Drives plus Individual Drive amount.

Describe the solution you'd like I would like to list all users resources usage from personal Drive and Team Drive distinctly.

Describe alternatives you've considered Possibility to list all folders sizes recursively showing the amount of each one in a csv file.

Additional context Add any other context or screenshots about the feature request here.

scrthq commented 5 years ago

Hey @chrispaivacwb - Thanks for opening this up! Here's the sample wrapper function I shared with you in Slack while I work on fully integrating this in PSGSuite!

function Get-GSDriveFolderSize {
    [CmdletBinding()]
    Param (
        [parameter(Mandatory,Position = 0,ValueFromPipelineByPropertyName)]
        [Alias('Id')]
        [String[]]
        $ParentFolderId,
        [parameter()]
        [Switch]
        $Recurse,
        [parameter()]
        [Int]
        $Depth = 0
    )
    Begin {
        $final = @{
            TotalSize = 0
        }
    }
    Process {
        foreach ($id in $ParentFolderId) {
            $files = Get-GSDriveFileList -ParentFolderId $id -IncludeTeamDriveItems -Verbose:$false
            $folderTotal = ($files.Size | Measure-Object -Sum).Sum
            if ($folderTotal){
                Write-Verbose ("Total file size in bytes in folder ID '$id': {0}" -f $folderTotal)
                $final.TotalSize +=  $folderTotal
            }
            if ($Recurse -and ($subfolders = $files | Where-Object {$_.MimeType -eq 'application/vnd.google-apps.folder'})) {
                $newDepth = $Depth + 1
                Write-Verbose "[Depth: $Depth > $newDepth] Recursively searching subfolder Ids: [ $($subfolders.Id -join ", ") ]"
                $subFolderTotal = Get-GSDriveFolderSize -ParentFolderId $subfolders.Id -Recurse -Depth $newDepth
                if ($subFolderTotal) {
                    $final.TotalSize += $subFolderTotal.TotalSize
                }
            }
        }
    }
    End {
        $final['TotalSizeInMB'] = $final['TotalSize'] / 1MB
        $final['TotalSizeInGB'] = $final['TotalSize'] / 1GB
        [PSCustomObject]$final
    }
}
scrthq commented 5 years ago

deployed!