#PowerShell, #PowerWiseScripting, #ProjectWise, PWPS_DAB

HowTo: Confirm Disk Space Prior To Migration

Be sure to check out my Scripting4Crypto initiative. It’s a fun way to get into using cryptocurrencies all while getting your PowerShell needs met.

In this post, we will be creating a PowerShell module script file (.psm1) which contains a function to verify if there is enough space on a local drive to proceed with a Work Area / Folder migration process.  We will provide a Work Area object, get a file size summation, compare that to the available disk space on the provided drive. The return will be a Boolean; returns a true if there is adequate space, otherwise returns false.

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

  • Get-PWFolders
  • Add-PWSavedSearch
  • Get-PWSavedSearches
  • Get-PWDocumentsBySearch
  • Remove-PWSavedSearch
  • Get-CimInstance

FUNCTION Definition

The following is the function wrapper which will contain the remainder of the code.

FUNCTION Confirm-AdequateDiskSpace {
<#
.SYNOPSIS
Verifies adequate disk space is available

.DESCRIPTION
Verifies adequate disk space is available to continue with the current process.

.PARAMETER WorkAreaObject
ProjectWise Work Area object to get document size summation for.

.PARAMETER LocalDrive
Local drive letter to verify disk space. Must be drive letter and colon (d:).

.EXAMPLE
PS C:\> Confirm-AdequateDiskSpace -WorkAreaObject $WorkAreaObject -LocalDrive 'd:'

.NOTES
This cmdlet is intended to be used with ProjectWise Work Area objects to ensure there is enough disk space available on the local drive. This will prevent processes from failing due to running out of resources.
#> [CmdletBinding(PositionalBinding = $true)]
[OutputType([boolean])] param (...) # end param... BEGIN {...} # end BEGIN... PROCESS {...} # end PROCESS... END{...} # end END... } # end FUNCTION Confirm-AdequateDiskSpace... Export-ModuleMember -Function Confirm-AdequateDiskSpace

Parameters

 

First thing we need to do is create our parameter and value pairs. The help messages will explain the purpose for each parameter. You will see that I have incorporated “ValidateScript” for the WorkAreaObject parameter and “ValidatePattern” for the LocalDrive parameter. This way I do not have do any additional checks to determine if the provided ProjectWise WorkArea exists, or the provided LocalDrive value is in the correct format.

[CmdletBinding(PositionalBinding = $true)]
[OutputType([boolean])] param ( [ValidateNotNullOrEmpty()] [ValidateScript({ Get-PWFolders -FolderPath $_.FullPath -JustOne })] [Parameter( HelpMessage = "ProjectWise Work Area object to report on.", Mandatory = $true, ValueFromPipeline = $true,
Position = 0)] [psobject]$WorkAreaObject, [ValidateNotNullOrEmpty()]
[ValidatePattern('^.:$')] [Parameter( HelpMessage = "Local drive to verify space for.", Mandatory = $true, Position = 1)] [string] $LocalDrive ) # end param...

Begin

I want to take a second and point out a couple of conventions that I use in my scripts. First, you may notice that I put a comment at the end of many of the blocks of code. Doing this makes it easier for me to determine where a block of code starts and ends. Comes in handy when troubleshooting a script.  I also add text ([BEGIN], [PROCESS], [END]) in the output messages (Write-Verbose, Write-Warning, Write-Error) corresponding to the section of the script I am in. Again, this makes it obvious where something occurs within the script.

BEGIN {  
    $CmdletName = $MyInvocation.MyCommand.Name
    $StartTime = Get-Date
    Write-Verbose "[BEGIN] $StartTime - Entering '$CmdletName' Function..."
} # end BEGIN...

PROCESS

Now, we will proceed to the PROCESS code block. Here we will create a temporary saved search for the Work Area being provided. We will then retrieve all of the document objects from the Work Area utilizing the temporary saved search. Once we have that information we can get the file size summation for the document objects returned and remove the temporary saved search. Next we will get the available free space for the drive provided and compare it to the file size summation. We are doubling the file size summation to ensure there is adequate space available. If there is not enough free space, the script will return a false. Otherwise, true will be returned.

PROCESS {
    
    try {

        #region CREATE TEMPORARY SAVED SEARCH

# Name of temporary saved search to be created.
$SearchName = 'AllDocs_PSTemp'
$Splat_SavedSearch = @{ OwnerProject = $WorkAreaObject
SearchName = $SearchName SearchFolder = $WorkAreaObject.FullPath
SearchSubFolders = $true
ReplaceExisting = $true } Add-PWSavedSearch @Splat_SavedSearch

$test = Get-PWSavedSearches -InputFolder $OwnerProject
if ( -not ($test | Where-Object Name -eq $SearchName)) {
Write-Warning -Message 'Failed to add temporary saved search.'
throw
}
#endregion CREATE TEMPORARY SAVED SEARCH #region GET PROJECTWISE WORKAREA DOCUMENT SIZE
# Get summation of documents filesize based on temporary saved search.
$pwDocsData = Get-PWDocumentsBySearch -SearchName $SavedSearch |
Measure-Object FileSize -Sum
[long]$WorkAreaFileSize = $pwDocsData.Sum

Remove-PWSavedSearch -InputFolder $WorkAreaObject -SearchName $SearchName

#endregion GET PROJECTWISE WORKAREA DOCUMENT SIZE
# Get available disk space for local drive provided.
[long]$diskFreeSpace = Get-CimInstance -Class CIM_LogicalDisk |
Where-Object DeviceID -eq $LocalDrive |
Select-Object -ExpandProperty Freespace

<# Double WorkAreaFileSize and subtract from diskFreeSpace
to determine if there is enough diskspace to complete process. #>
if ($diskFreeSpace - ($WorkAreaFileSize * 2) -lt 0) {
Write-Warning -Message "Not enough disk space to run process. Required: $([math]::Round(($WorkAreaFileSize * 2)/1GB, 2)) GB Available: $([math]::Round($diskFreeSpace/1GB, 2)) GB"
Write-Output $false
} else {
Write-Output $true
} } catch { Write-Warning -Message "[PROCESS] $($Error[0])" } # end try/catch... } # end PROCESS...

END

Lastly, we will proceed to the END block of code. We will return some information to the console via the Verbose stream. If aren’t displaying verbose information, you will not see this information.

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

The following is a link to the full PowerShell script. 

FUNCTION – Confirm-AdequateDiskSpace

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 )

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.