#PowerShell, #PowerWiseScripting, #ProjectWise, PWPS_DAB

HowTo: Use Switch to Set CSBs on Folders

In this post, I will demonstrate using a Switch statement within PowerShell to determine which ProjectWise Folder(s) to associate CSBs (Configuration Settings Blocks) for Managed Workspace to. I will simulate the setting of the Managed Workspace. I will use the Write-Host (with color variations) to present the syntax for setting the WorkSpace for a folder. The syntax is shown in brackets ({}). These are not required when using.

The purpose of this post is not only to demonstrate how to apply CSBs, but more importantly how you can use an advanced Switch statement to select from an array.

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

  • New-PWFolder
  • Convert-PWFolderToRichProject
  • Get-PWFolders
  • Set-PWManagedWorkspaceForFolders
  • Remove-PWFolder
  • Write-Host

Switch Statement

So, what is a Switch statement? I think of a Switch statement as a way to check for multiple conditions in lieu of using multiple if statements. To me a Switch statement is much easier to follow and understand, rather than a bunch of if / if else / else statements. 

Take a look at the help information for a comprehensive overview. One important thing to note, the Switch statement has parameters available for using wildcards or regex, or doing case-sensitive comparisons.

Get-Help about_Switch

Create ProjectWise WorkArea

For this post, I want to create a Work Area and sub-folders to ensure I am working with specific folder names. I will cleanup the Work Area and folders at the end.

In the following code, I provide an array of folder paths. Then loop through the items in the array to create a folder hierarchy in ProjectWise. Finally, I convert the first item in the array to a ProjectWise Work Area. 

# WorkArea folders. 
$arFolders = @(
'Projects\MyNewWorkArea'
'Projects\MyNewWorkArea\Architectural'
'Projects\MyNewWorkArea\Civil'
'Projects\MyNewWorkArea\Electrical'
'Projects\MyNewWorkArea\General'
'Projects\MyNewWorkArea\Geotechnical'
'Projects\MyNewWorkArea\HVAC'
'Projects\MyNewWorkArea\Instrumentation'
'Projects\MyNewWorkArea\Landscape'
'Projects\MyNewWorkArea\Mech-proc'
'Projects\MyNewWorkArea\Plumbing and Fire Protection'
'Projects\MyNewWorkArea\Structural'
'Projects\MyNewWorkArea\Architectural\Model'
'Projects\MyNewWorkArea\Structural\Model'
'Projects\MyNewWorkArea\Civil\Model'
'Projects\MyNewWorkArea\Electrical\P&ID'
'Projects\MyNewWorkArea\Instrumentation\Model'
) # end $arFolders...

# Create new folders to be used in script.
foreach ($f in $arfolders){
$newPWFolder = New-PWFolder -FolderPath $f -StorageArea 'Storage01'
} # end foreach ($f in $arfolders...
# Convert parent folder to Work Area.
Convert-PWFolderToRichProject -InputFolder (Get-PWFolders -FolderPath $arFolders[0] -JustOne)

Use the Switch Statement

First, I will get all of the folders within the folder hierarchy. We will be utilizing the folder name and fullpath in the Switch statement so I am including the -PopulatePaths switch parameter within the Get-PWFolders cmdlet.

# Get all folders pertaining to the desired Work Area.
$pwFolders = Get-PWFolders -FolderPath 'Projects\MyNewWorkArea' -PopulatePaths

Now that we have our folders we can loop through each of the folders. We will use the Switch statement to determine which folders to apply certain CSBs to.

# Here is our foreach loop
foreach($pwFolder in $pwFolders) {

Write-Host "Current folder '$($pwFolder.FullPath)'." -ForegroundColor Cyan

# Capturing name and fullpath to simplify.
$currentName = $pwFolder.Name
$currentFullPath = $pwFolder.FullPath

switch ($currentName) { ... } # end switch ($currentName...

} # end foreach($pwFolder in $pwFolders..

Let’s get into the Switch statement. The following shows the structure of the Switch statement. We will look at each comparison element separately.

# Array of folder names for one of the comparisons.
$arFolderNames = @('Architectural', 'civil', 'electrical', 'general', 'geotechnical', 'hvac', 'instrumentation', 'landscape', 'mech-proc', 'plumbing and fire protection', 'structural')

switch ($currentName) {

# Determines if the current folder name is in the array of folder names.
# Set WorkSet CSBs here.

{ $_ -in $arFolderNames } { ... } # end { $_ -in $arFolderNames...

# Set WorkSet and User for Instrumentation folder ONLY.
'instrumentation' { ... } # end 'instrumentation'...

# Set for Model or P&ID folders ONLY.
{ $_ -in @('model', 'P&ID') } { ... } # end {$_ -in @('model', 'P&ID')...

# Default should ONLY apply to the Work Area folder.
# Set Global configuration ONLY.
default { ... } # end default...

} # end switch ($currentName...

In the first comparison, the Switch statement will determine if the current folder name exists within the $arFolderNames variable. If it does, the code block will be processed. Two lines of text will be written to the console. One displays the current folder name, and the second gives the syntax to apply the CSB to the current folder object (within brackets).

We are taking advantage of the Break statement as we no longer want the current folder to be processed. If the current folder does not exist in the array, it will proceed to the next comparison within the Switch statement.

# Determines if the current folder name is in the array of folder names.
# Set WorkSet CSBs here.
{ $_ -in $arFolderNames } {
Write-Host "$currentName" -ForegroundColor DarkMagenta
Write-Host " - {Set-PWManagedWorkspaceForFolders -InputFolders $pwFolder -WorkspaceType WorkSet -ConfigurationBlockName 'MyNewWorkArea'}" -ForegroundColor Yellow
break
} # end { $_ -in $arFolderNames...

In this comparison block the only accepted value is instrumentation. Again, we will display the folder name and each of the CSBs to be applied. In this one there are two CSBs being applied.

# Set WorkSet and User for Instrumentation folder ONLY.
'instrumentation' {
Write-Host "$currentName" -ForegroundColor Green
Write-Host " - {Set-PWManagedWorkspaceForFolders -InputFolders $pwFolder -WorkspaceType WorkSet -ConfigurationBlockName 'MyNewWorkArea'}" -ForegroundColor Yellow
Write-Host " - {Set-PWManagedWorkspaceForFolders -InputFolders $pwFolder -WorkspaceType User -ConfigurationBlockName 'Instrumentation'}" -ForegroundColor Yellow
break
} # end 'instrumentation'...

In this next section, we will be comparing to an array again. If the name of the current folder exists in the array,  we will continue to process.  
Here we also compare the fullpath to determine if it contains certain folder names. If it does, the appropriate CSBs will be applied.

# Set for Model or P&ID folders ONLY.
{ $_ -in @('model', 'P&ID') } {
Write-Host "$currentName" -ForegroundColor Green

# Set OBD (Open Building Designer) CSB for Architectural folders.
if($currentFullPath.Contains('architectural')) {
Write-Host " - Architectural Model" -ForegroundColor DarkGreen
Write-Host " - {Set-PWManagedWorkspaceForFolders -InputFolders $pwFolder -WorkspaceType Predefined -ConfigurationBlockName 'OBD'}" -ForegroundColor Yellow
}

# Set ORD (Open Roads Designer) CSB for Civil folders.
if($currentFullPath.Contains('civil')) {
Write-Host " - Civil Model" -ForegroundColor DarkGreen
Write-Host " - {Set-PWManagedWorkspaceForFolders -InputFolders $pwFolder -WorkspaceType Predefined -ConfigurationBlockName 'ORD'}" -ForegroundColor Yellow
}

if($currentFullPath.Contains('electrical')) {
Write-Host " - Electrical Model" -ForegroundColor DarkGreen
}

# Set MicroStation CSB for all folders.
Write-Host " - {Set-PWManagedWorkspaceForFolders -InputFolders $pwFolder -WorkspaceType Predefined -ConfigurationBlockName 'MicroStation'}" -ForegroundColor Yellow
break
} # end {$_ -in @('model', 'P&ID')...

Finally, we have the default code block of the Switch statement. Here we set the global values for the Work Area folder. Nothing else should reach this point. 

# Default should ONLY apply to the Work Area folder.
# Set Global configuration ONLY.
default {
Write-Host "Global" -ForegroundColor Magenta
Write-Host " - Set Global Workspace CSBs Here." -ForegroundColor Magenta
Write-Host " - {Set-PWManagedWorkspaceForFolders -InputFolders $pwFolder -WorkspaceType Global -ConfigurationBlockName 'Global Variables'}" -ForegroundColor Yellow
Write-Host " - {Set-PWManagedWorkspaceForFolders -InputFolders $pwFolder -WorkspaceType Site -ConfigurationBlockName 'MyCompany'}" -ForegroundColor Yellow
break
} # end default...

The following is an example of the output.

output

Remove ProjectWise WorkArea

The following shows how to remove the folders we added at the beginning.

# Cleanup Work Area created for script.
Remove-PWFolder -InputFolder (Get-PWFolders -FolderPath $arFolders[0] -JustOne) -RemoveFolders -ProceedWithDelete -Verbose

Summary

I wanted to demonstrate how to use the Switch statement in a slightly more advanced way. Comparing a value to an array can be very powerful and useful.  Hopefully all of this made sense and comes in handy.


The following is a link to the full PowerShell script.

HowTo – Use Switch Case to Set CSBs on Folders

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

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