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 new ProjectWise (PW) Work Area from a PW Template. This is a pretty straight forward process. I will be including some validation and a little error handling.
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.8.0. Take a look at the help for each of the cmdlets to become familiar with their functionality, available parameters, etc.
- Show-PWFolderBrowserDialog
- Get-PWProjectTemplates
- New-PWRichProjectFromTemplate
Populate Variables
I have a few variables to be created and populated.
# Name and description of new Work Area to be created.
$NewProjectName = "MyTestProject"
$NewProjectDescription = "My Test Project (Created via PowerShell)"
# Name of template to be used. This will be validated prior to continuing.
$TemplateName = "XXXXXX - Template"
<# Enter the name of the storage area to be used.
Otherwise, use the target folder's storage area. #>
$StorageArea = [string]::Empty
Select Target Folder
First thing we need to do is select the folder to create the new Work Area in. To do this I will be using the Show-PWFolderBrowserDialog cmdlet. You can also use the Get-PWFolders to do the same.
<# Get ProjectWise folder object where
the new Work Area (Rich Project) will be created. #>
$TargetFolder = Show-PWFolderBrowserDialog
The following shows the Select Folder for PowerShell dialog to select the target folder.
Validate Template
Next, we will verify that the provided template is valid. If it is, we will continue with creating the Work Area, if it is not valid, we will exit the process.
The following shows the available Templates.
# Verify the template provided is valid. If not, can the process.
if($Template = Get-PWProjectTemplates | Where-Object Name -EQ $TemplateName){
Write-Host "Validated '$TemplateName' template." -ForegroundColor Green
... PROCESS CODE (see below).
} else {
Write-Warning -Message "Invalid template name ('$TemplateName') provided."
}
Process Code
The following is the actual code for creating the new Work Area. I like to use Splatting for easier readability.
The following shows the Target folder prior to creating new Work Area.
$Splat_New = @{
InputFolder = $TargetFolder
NewProjectName = $NewProjectName
TemplateName = $TemplateName
}
<# Update the folder description for the new Work Area
if one is provided. Otherwise, it will be skipped. #>
if($NewProjectDescription.Length > 0){
$Splat_New.NewProjectDescription = $NewProjectDescription
}
<# If a StorageArea value is not provided,
use the storage area from the target folder. #>
if($StorageArea.Length > 0){
$Splat_New.StorageArea = $StorageArea
} else {
$Splat_New.StorageArea = $TargetFolder.Storage
}
try{
$PWFolder_New = New-PWRichProjectFromTemplate @Splat_New -Verbose
} catch {
Write-Warning -Message "$($Error[0].Exception.Message)"
}
The following shows the Work Area was successfully created.
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.
Thanks for this Brian. For some reason when I do this all my folders lose their workflows. The workflows are present in the job, just not mapped correctly? Any thoughts on what I have done wrong!
LikeLike
Thanks for checking out my blog. I hope you find it useful.
I don’t think you doing anything wrong. This appears to be a short-coming with the cmdlet. I would suggest posting this to the Bentley Communities ‘https://communities.bentley.com/products/projectwise/f/projectwise-powershell-extensions-forum’ site and request copying the workflows be added to the ‘New-PWRichProjectFromTemplate’ cmdlet.
LikeLike
Thanks for the reply Brian. I will do that. Anything you can suggest to get around this?
Would I still have this issue with the New-PWRichProjectsFromSpreadsheet option. I can’t seem to find the format needed for this spreadsheet so haven’t investigated this method really!
I have used ‘Get-PWFolders -Slow | Export-csv -Path’ which shows the what workflows are assigned to what folders but can’t see a way to apply that back to my mew schemes folder tree!
LikeLike
Something like the following should work for you.
# Get full folder hierarchy for the Template Work Area used.
$Template_Hierarchy = Get-PWFolders -FolderID $Template.ProjectID
# Get full folder hierarchy for the NEW Work Area created.
$Target_Hierarchy = Get-PWFolders -FolderID $PWFolder_New.ProjectID
# Loop through all folders within the template which has a workflow assigned,
# and update the corresponding folder within the new work area.
foreach($f in $Template_Hierarchy | Where-Object WorkFlowID -GT 0){
$temp = $Target_Hierarchy | Where-Object Name -EQ $f.Name
Set-PWWorkflowByFolderPath -FolderID $temp.ProjectID -WorkflowName $f.Workflow -Verbose
}
LikeLike