#PowerShell, #PowerWiseScripting, #ProjectWise, PWPS_DAB

HowTo: Monitor Folder to Import Documents Into ProjectWise

In this post, I will demonstrate how to setup a FileSystemWatcher to monitor a specified local folder for new files. When a new file(s) is created in the folder on the local system, it will then be imported into a corresponding ProjectWise folder. We will only be monitoring created events, however, you can also configure it to monitor changed, deleted and renamed events.

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

  • New-PWLogin
  • New-PWDocument
  • Undo-PWLogin
  • Register-ObjectEvent
  • Get-EventSubscriber
  • Unregister-Event

Configure the FileSystemWatcher

Here will create a new instance of the FileSystemWatcher.

The FileSystemWatcher will monitor the specified folder (path) for any new documents being added. You can use the .Filter property to limit the file types processed.

# Initializes a new instance of the FileSystemWatcher class.
$FileSystemWatcher = New-Object System.IO.FileSystemWatcher

# Sets the path of the directory to watch.
$FileSystemWatcher.Path = 'd:\temp\MonitoredFolder'

# When set to true, all subfolders within the path will be monitored.
$FileSystemWatcher.IncludeSubdirectories = $false

# Enables the component.
$FileSystemWatcher.EnableRaisingEvents = $true

# Used to filter the files monitored.
$FileSystemWatcher.Filter = "*.*"

<# Default is 8KB. Setting to maximum buffer size to ensure
        all files will be processed. #>
$FileSystemWatcher.InternalBufferSize = 64KB

# Sets the type(s) of changes to watch for.
$FileSystemWatcher.NotifyFilter = [System.IO.NotifyFilters]::CreationTime

Register the Event

Now we need to register the new event. We also need to specify the ProjectWise datasource and folder to import documents into.

# ProjectWise datasource to log into.
$pwDatasource = 'bmf-ws2016-pwdi:ProjectWise'
# ProjectWise folder to import documents into.
$PWFolder = 'Projects\MonitoredFolder'

# Register event
$Splat_Register = @{
    InputObject = $FileSystemWatcher
    SourceIdentifier = 'MonitorDocsToPW'
    EventName = 'Created'
}
Register-ObjectEvent @Splat_Register -Action {

    $File = $Event.SourceEventArgs.FullPath
    $EventType = $Event.SourceEventArgs.ChangeType
    $Time = $Event.TimeGenerated
    $Object = "'$File' was $EventType at $Time."
    
    Write-Host $Object -ForegroundColor Green

    try { 
        # Log into ProjectWise datasource.
        try { 
            New-PWLogin -DatasourceName $pwDatasource -BentleyIMS -ErrorAction Stop -WarningAction Stop
        } catch {
            throw "Failed to login to ProjectWise datasource '$pwDatasource'."
        }

        # Create new document within the current ProjectWise session.
        try { 
            $Splat_NewDoc = @{
                FolderPath = $PWFolder
                FilePath = "$($Event.SourceEventArgs.FullPath)"
                Description = 'Imported from monitored folder.'
            }
            $newDoc = New-PWDocument @Splat_NewDoc -ErrorAction Stop
        } catch {
            throw 'Failed to create new ProjectWise document.'
        }

        # Log out of ProjectWise datasource.
        Undo-PWLogin
    } catch {
        Write-Host $Error[0] -ForegroundColor Magenta
    }
}

Add Files To Monitored Folder

Now that we’ve registered to the FileSystemWatcher, we can add files to the monitored folder. It does not matter how you add the documents. You can drag-and-drop documents into the folder. You can right mouse-click and create a new file from the menu, or you can save a file from within an application.

The following show the local folder being monitored and the corresponding ProjectWise folder prior to adding and files.

windowsExplorerProjectWiseExplorer

The following shows the local folder and ProjectWise folder after dragging and dropping files into the monitored folder and creating one document from the menu.

windowsExplorerallProjectWiseExplorerAll The following shows the output to the PowerShell console when a document is processed.

dragndrop

Unregister the Event

You will want to unregister the event to discontinue processing files.

# Unregister event
Get-EventSubscriber -SourceIdentifier MonitorDocsToPW | Unregister-Event

Summary

This is a simple way to import documents into your ProjectWise datasource. You can also, include events for when files are renamed, updated, or deleted.


Experiment with it and have fun. And please, let me know if there is a topic you would like to see a post for. Or share a solution you have developed.

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. And thank you for checking out my blog.

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.