#PowerShell, #PowerWiseScripting, #ProjectWise, PWPS_DAB

How To: Add a New Folder to All Projects (Updated)

I had a user request a script that would add a sub-folder (PDF) to each of their Rich Projects / Work Areas within the current ProjectWise Datasource.

The following script will add a specified folder to all ProjectWise Rich Projects / Work Areas within the current ProjectWise Datasource.

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.12.2.0. Take a look at the help for each of the cmdlets to become familiar with their functionality, available parameters, etc.

  • Get-PWRichProjects
  • New-PWFolder

Parameters

First thing we need to do is create our parameter and value pairs. The comments will explain the purpose for each parameter.

[CmdletBinding()]
param(
    # Specify New Folder Name.
    [string] $PWFolderName = 'PDFs',
    # Specify New Folder Description.
    [string] $PWFolderDescription = 'Contains PDF files',
    # Specify Environment to be used. Not required.
    [string] $PWEnvironment = 'Complex',
    # Specify storage area to be used.
    [string] $PWStorageArea = 'Storage_01'
)

Begin

In the following begin section, we are only going to display the start time.

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 {   
    $StartTime = Get-Date 
    Write-Verbose -Message "[BEGIN] Start time: $StartTime"
} # end BEGIN

PROCESS

Now, lets look at the PROCESS code block.

Here will get a list of all Rich Projects / Work Areas to add the new sub-folder to.

PROCESS { 
    <# Returns ProjectWise folder objects for all 
        Rich Projects / Work Areas found in the current datasource. #>
    $PWRichProjects = Get-PWRichProjects -Verbose

    ...

} # end PROCESS

Next we will loop through each of the rich projects / work areas returned.

The following is part of the PROCESS block of code.

# Loop thru all projects returned and add new sub-folder. 
foreach ( $PWRichProject in $PWRichProjects ) {
    # Using splatting for readability. 
    $NewFolderInfo = @{ 
        FolderPath = "$($PWRichProject.FullPath)\$PWFolderName" 
        Description = $PWFolderDescription 
        Environment = $PWEnvironment 
        <# You can also set the storage area to that of the parent
            rich project storage area. #> 
        #StorageArea = $PWRichProject.Storage 
        StorageArea = $PWStorageArea 
    } 
    try {
        $pwFolder = New-PWFolder @NewFolderInfo -ErrorAction Stop
    } catch {
        Write-Warning -Message "[PROCESS] Error occurred while attempting to add sub-folder to '$($PWRichPHowTo_Add a New Folder to All Projectsroject.Name)'. $($Error[0].Exception)"
    }
} # end foreach ( $PWRichProject...

END

Lastly, we will proceed to the END block of code. You will see that we have calculated the amount of time it had taken to complete the process.

END {
    $EndTime = Get-Date
    Write-Verbose -Message "[END] It took $([Math]::Round($EndTime.Subtract($StartTime).TotalMinutes, 2)) minutes to complete process."
} # end END

 


The following is a link to the full PowerShell script.

HowTo_Add a New Folder to All Projects

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 )

Google photo

You are commenting using your Google 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