#PowerShell, #PowerWiseScripting, #ProjectWise, PWPS_DAB

HowTo: Use Copy-PWFoldersBetweenDatasources

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, I will be demonstrating how to use the Copy-PWFoldersBetweenDatasources. This is a powerful tool which can be used to quickly migrate or copy data from one datasource to another.

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

  • New-PWLogin
  • Get-PWSessions
  • Get-PWCurrentDatasource
  • Set-PWSession
  • Get-PWFolders
  • Show-PWFolderBrowserDialog
  • Copy-PWFoldersBetweenDatasources
  • Read-Host

Login Into Multiple ProjectWise Sessions

We will need to log into both the source and target ProjectWise datasources simultaneously for this cmdlet to work. We can use the Sessions cmdlets to switch between the individual sessions.

Ensure the source datasource is the current datasource prior to proceeding.

#region LOGIN TO MULTIPLE PROJECTWISE SESSIONS

# Open sessions of ProjectWise for each datasource.
#   Source: BMF-WS2016-PWDI:ProjectWise
#   Target: BMF-WS2016-PWDI:PowerShell
$SourceDatasource = 'BMF-WS2016-PWDI:ProjectWise'
$TargetDatasource = 'BMF-WS2016-PWDI:PowerShell'

# Using a loop to log into each datasource. Prompting for the user name and password for each.
for($x = 0; $x -lt 2; $x++){
    if($x -eq 0){
        $ds = $SourceDatasource
    } else {
        $ds = $TargetDatasource
    }
    $Splat_Login = @{
        DatasourceName = $ds
        UserName = (Read-Host -Prompt 'Enter user name')
        Password = (Read-Host -Prompt 'Enter password' -AsSecureString)
    }
    New-PWLogin @Splat_Login
}

# Use to show all active ProjectWise sessions. 
Get-PWSessions
# Use to see the current ProjectWise datasource.
Get-PWCurrentDatasource
# Use to set the current datasource.
Set-PWSession -Datasource $SourceDatasource

#endregion LOGIN TO MULTIPLE PROJECTWISE SESSIONS

The following shows the two ProjectWise datasource sessions.

datasources

Get Source Work Area(s) / Folder(s)

The following shows the source Work Areas we are going to copy into the highlighted target folder within the Target datasource.

workareas

Next, we will be selecting our work area(s) and or folders to be copied over to the target datasource. You can pass an array of folder objects. However, keep in mind that the contents of each folder object provided, will be copied into the same target folder.  This may or may not be a desired result.

#region GET SOURCE PROJECTWISE WORK AREA(S) / FOLDER(S)

<# Be sure to ONLY pass the parent folder of the Work Area
    or folder hierarchy you are copying over. #>
# Use the -JustOne switch parameter when using the Get-PWFolders.
$WorkArea = Get-PWFolders -FolderID 177 -JustOne
# Or use the Show-PWFolderBrowserDialog to select your Work Area or folder.
$WorkArea2 = Show-PWFolderBrowserDialog
# Add both Work Areas into an array.
$WorkAreas = @($WorkArea, $WorkArea2)

#endregion GET SOURCE PROJECTWISE WORK AREA(S) / FOLDER(S)

The following shows two Work Areas being selected. ONLY the parent folder has been selected which is very important.

sourcefolders

Create and Populate Variables

Now we will create and populate the required variables. Each variable corresponds to a parameter from the Copy-PWFoldersBetweenDatasources cmdlet.

#region CREATE AND POPULATE VARIABLES

<# The path and name of the folder within the Target datasource
    to copy the source folders into. The target folder will be created
    if it does not exist within the Target datasource. #>
$TargetFolderPath = 'Projects'
# The storage area within the Target datasource to be used.
$TargetStorage = 'Storage01'

<# Include switch parameters. Use these switch parameters to 
    include the specific item type. #>
$IncludeDocuments  = $true
$IncludeAuditTrail = $true

<# Ignore switch parameters. Use these switch parameters
    to prevent the copy from failing when the ProjectTypes, 
    Environment, Workflows, and/or Views do not exist
    in the target datasource. #>
$IgnoreMissingEnvironments = $true
$IgnoreMissingWorkflows    = $true
$IgnoreMissingProjectTypes = $true
$IgnoreMissingViews        = $true

#endregion CREATE AND POPULATE VARIABLES

Run Copy-PWFoldersBetweenDatasources Cmdlet

Finally, we have everything we need to run the cmdlet. We will loop through the $WorkAreas array and copy the contents of each into a separate target folder.

#region RUN COPY CMDLET

foreach($WA in $WorkAreas) { 

    $Splat_Copy = @{
        <# The InputFolders contains the Work Area(s) / folder(s) from the Source datasource
            to be copied over. These must be a ProjectWise folder object(s). #>
        InputFolders = $WA
        # Target Datasource parameters.
        TargetDatasource = $TargetDatasource
        TargetParentPath = $TargetFolderPath
        TargetStorage    = $TargetStorage
        # Include Options:
        IncludeDocuments  = $IncludeDocuments
        IncludeAuditTrail = $IncludeAuditTrail
        # Ignore Options:
        IgnoreMissingProjectTypes = $IgnoreMissingProjectTypes
        IgnoreMissingEnvironments = $IgnoreMissingEnvironments
        IgnoreMissingWorkflows    = $IgnoreMissingWorkflows
        IgnoreMissingViews        = $IgnoreMissingViews
    }
    $results = Copy-PWFoldersBetweenDatasources @Splat_Copy -Verbose

} # end foreach($WorkArea in $WorkAreas)...


#endregion RUN COPY CMDLET

The following shows the contents of the $Splat_Copy variable prior to running the command.

splat_copy

The following shows the results. You can see that we have two new folders under the Projects folder. Also, you can see that document versions were copied over and referencing was maintained. This very cool!!! No need to run a Scan for References.

results

Summary

Just to recap, we copied the contents of two source folders from one datasource, to the target folder of a second datasource. All folders and documents were copied over, including document versions. Also, document referencing was maintained and document audit trail records were copied over for each document.  This is a powerful tool.


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.

4 thoughts on “HowTo: Use Copy-PWFoldersBetweenDatasources”

  1. Hello,

    My target datasource project has the folder structure from our template with no documents. It also has a different security schema than that of the source location. This copy process works great to copy the source documents as well as any missing folders. However, the new folders that get copied include the source security schema. The folders that “match” are skipped and the destination security maintain as expected. What can I do to maintain the destination security on any new sub folders that are created?

    Many thanks.

    Like

    1. Hey Luis,
      First, thank you for checking out my blog. I hope you find it useful. As for the script, I think you will need to update the access control after adding the new folders to ensure the access control is correct.
      Cheers,
      Brian

      Like

Leave a comment

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