#PowerShell, #PowerWiseScripting, #ProjectWise, PWPS_DAB

How To: Get File Size Summation

In this post I am going to show how to get a quick file size summation for a datasource. And then put it into a function to offer a quick way to display the result.

All of the ProjectWise related cmdlets are available using the PWPS_DAB module. At the time of this post, I am using version 2.1.12.0. Take a look at the help for each of the cmdlets to become familiar with their functionality, available parameters, etc.

  • Select-PWSQL

Get Filesize Summation

It is very simple to get the overall filesize summation for your active ProjectWise datasource by querying the database. The following will return the file size summation for all documents within the datasource in bytes.

(Select-PWSQL "SELECT SUM (o_size) AS SIZE from dms_doc").Size

FUNCTION

Next, I will incorporate this query into a function. The function will allow you to select how you want the value displayed (ie. bytes, kilobytes, megabytes, gigabytes, or terabytes) as well as rounding.

I apologize for the display. WordPress has made it difficult to manipulate the color of text for individual words.

FUNCTION Get-PWFileSizeSummation{
    [CmdletBinding()]
    param(
 
        [ValidateNotNullOrEmpty()]
        [ValidateSet("bytes", "KB", "GB", "TB")]
        [Parameter(
                HelpMessage = "Specifies which size designation to return file size summation as.",
            Mandatory = $false)]
        [string] $Designation = "bytes",

        [Parameter(
            HelpMessage = "Specifies the number of decimals to round to.",
            Mandatory = $false)]
        [int] $RoundTo
   
    ) # end param...

    BEGIN{
        $CmdletName = $MyInvocation.MyCommand.Name
        $StartTime = Get-Date
        Write-Verbose -Message "[BEGIN] $Start - Entering '$CmdletName' Function..."
    } # end BEGIN...
   
    PROCESS{# bytes (B), kilobytes (KB), megabytes (MB), gigabytes (GB), terabytes (TB), and beyond
   
       
        $Bytes = (Select-PWSQL "SELECT SUM (o_size) AS SIZE from dms_doc").Size

        [double]$Results = $Bytes

        $Divisor = [string]::Empty
        switch ($Designation)
        {
            'KB' { $Divisor = 1KB }
            'GB' { $Divisor = 1GB }
            'TB' { $Divisor = 1TB }
            Default {}
        }

        Write-Verbose -Message "$Results in bytes."

        if( -not ([string]::IsNullOrEmpty($Divisor))){
            $Results = $Bytes / $Divisor
            Write-Verbose -Message "$Results $($Designation.ToUpper())"
        }

        if($RoundTo -gt 0){
            $Results = [Math]::Round($Results, $RoundTo)
            Write-Verbose -Message "Rounded results to $Results $($Designation.ToUpper())"
        }

        Write-Output $Results
   
    } # end PROCESS...

    END {
   
        $EndTime = Get-Date
        Write-Verbose -Message "[END] It took $($EndTime - $StartTime) to complete the process."
        Write-Verbose -Message "[END] $EndTime - Exiting '$CmdletName' Function..."
   
    } # end END...

}

Once you have the function in memory, you can use the Get-PWFileSizeSummation function to get the overall file size.

Get-PWFileSizeSummation -Designation GB -RoundTo 2

Experiment with it and have fun.

Hopefully, you find this useful. Please let me know if you have any questions or comments.  If you like this post, please click the Like button at the bottom of the page.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.