Bentley Communities
Bentley Communities
  • Site
  • User
  • Site
  • Search
  • User
ProjectWise
  • Product Communities
ProjectWise
ProjectWise PowerShell Extensions Forum Scheduled Task (PowerShell) Partially Works
    • Sign In

    • State Suggested Answer
    • Replies 7 replies
    • Answers 1 answer
    • Subscribers 66 subscribers
    • Views 1330 views
    • Users 0 members are here
    • ProjectWise
    • pwps_dab
    • Get-PWDocumentsBySearch

    Scheduled Task (PowerShell) Partially Works

    Matt Kovach
    Offline Matt Kovach over 2 years ago

    Hello PowerShell Community,

    Anyone experience partially-working Scheduled Task (PowerShell)? The task will:

    • Accept and pass thru Parameters (Ex.: Datasource Name).
    • Sign into the Datasource using the Get-SecureStringFromEncryptedFile method.
    • Write Output to a Text File via Out-File (e.g. Variable Values).

    What it won't do is find and use the specified Saved Search to Get Documents:

    # Get Documents resulting from a Saved Search.
    $Documents = Get-PWDocumentsBySearch -SearchName $SavedSearchName | Export-PWDocuments -OutputFolder $OutputFolderPath -UseDMSFolders

    If I run the PowerShell Script manually (via a Batch File), everything works.

    Here's the entire PowerShell Script Code (in testing phase):

    <#===========================================+
     |  BEGIN SECTION:  Parameter Declarations.  |
     +===========================================#>
    
    Param (
    	[string]$parProjectWiseDatasource,
    	[string]$parWindowsInputFilePath,
    	[string]$parProjectWiseSavedSearchName,
    	[string]$parWindowsOutputFolderPath
    )
    
    <#========================================+
     |  BEGIN SECTION:  Main Body of Script.  |
     +========================================#>
    
    # ProjectWise Datasource parameter was specified during PowerShell Script execution.
    If ($parProjectWiseDatasource)
    	{
    		# Set Datasource Name.
    		$DatasourceName = $parProjectWiseDatasource
    		
    		# Set User Name.
    		$UserName = "*****"
    		
    		# Input File Path parameter was specified during PowerShell Script execution.
    		If ($parWindowsInputFilePath)
    			{
    				# Set Input File Path.
    				$InputFilePath = $parWindowsInputFilePath
    			}
    		
    		Else
    			{
    				# Set Input File Path.
    				$InputFilePath = "C:\Temp\$($UserName).txt"
    			}
    		
    		# Set Password from Encrypted File.
    		$Password = $(Get-SecureStringFromEncryptedFile -FileName $InputFilePath)
    		
    		# Login to the Datasource.
    		$Result = New-PWLogin -DatasourceName $DatasourceName -UserName $UserName -Password $Password -DoNotCreateWorkingDirectory -NonAdminLogin
    		
    	}
    # ProjectWise Datasource parameter was not specified during PowerShell Script execution.
    Else
    	{
    		# Prompt to select and to sign in to a Datasource.
    		$Result = New-PWLogin -BentleyIMS -NonAdminLogin
    		
    		# Get Current Datasource Name.
    		$DatasourceName = Get-PWCurrentDatasource
    	}
    
    # Not signed into a ProjectWise Datasource.
    If ($Result -eq $False)
    	{
    		# Exit PowerShell Script.
    		Exit
    	}
    # Signed into a ProjectWise Datasource.
    Else
    	{
    		# Signed into the "*** (Production)" Datasource.
    		If ($DatasourceName -eq "********************")
    			{
    				# Update Datasource String.
    				$Datasource = "*** (Production)"
    			}
    		# Signed into the "*** (Testing)" Datasource.
    		ElseIf ($DatasourceName -eq "********************")
    			{
    				# Update Datasource String.
    				$Datasource = "*** (Testing)"
    			}
    		
    		# Set Day.
    		$Day = "{0:00}" -f (Get-Date).Day
    		
    		# Get Date.
    		$Date = (Get-Date).Month.ToString() + "-" + $Day.ToString() + "-" + (Get-Date).Year.ToString()
    		
    		# ProjectWise Saved Search Name parameter was specified during PowerShell Script execution.
    		If ($parProjectWiseSavedSearchName)
    			{
    				# Set Saved Search Name.
    				$SavedSearchName = $parProjectWiseSavedSearchName
    			}
    		# ProjectWise Saved Search Name parameter was not specified during PowerShell Script execution.
    		Else
    			{
    				# Set Saved Search Name.
    				$SavedSearchName = "Files Changed in Last 24 Hours"
    			}
    		
    		# Output Folder Path parameter was specified during PowerShell Script execution.
    		If ($parWindowsOutputFolderPath)
    			{
    				# Set Output Folder Path.
    				$OutputFolderPath = $parWindowsOutputFolderPath + "\$($Datasource)\$($Date)"
    			}
    		# Output Folder Path parameter was not specified during PowerShell Script execution.
    		Else
    			{
    				# Set Output Folder Path.
    				$OutputFolderPath = "C:\Projects\ProjectWise\Backups\$($Datasource)\$($Date)"
    			}
    		
    		# Get Documents resulting from a Saved Search.
    		$Documents = Get-PWDocumentsBySearch -SearchName $SavedSearchName | Export-PWDocuments -OutputFolder $OutputFolderPath -UseDMSFolders
    		
    		# Sign out from ProjectWise Datasource.
    		Undo-PWLogin | Out-Null
    	}
    

    Here are some of the Scheduled Task particulars:

    Scheduled Task Action: C:\Windows\SysWOW64\WindowsPowerShell\v1.0\powershell.exe

    Arguments: -NoLogo -NonInteractive -File "C:\~ Data\ProjectWise\PowerShell\Documents\Export_Files_From_Saved_Search.ps1" -parProjectWiseDatasource "******Datasource Name*******" -parWindowsInputFilePath "C:\~ Data\ProjectWise\PowerShell\Documents\PW_Backup.txt" -parProjectWiseSavedSearchName "Files Changed in Last 24 Hours" -parWindowsOutputFolderPath "C:\~ Data\ProjectWise\Backups")

    Thanks in advance.

    • Sign in to reply
    • Cancel
    • Matt Kovach
      0 Offline Matt Kovach Tue, Nov 3 2020 6:39 PM

      Found the error in my ways. The Scheduled Task Action: was set incorrectly. It should read C:\Windows\System32\WindowsPowerShell\v1.0\powershell.exe.

      ProjectWise Explorer CONNECT Edition, Version 10.00.03.453

      ProjectWise Drive, Version 2022.1.328

      OpenRoads Designer CONNECT Edition, Version 10.09.00.91

      OpenRoads Designer CE 202 Release 2, Version 10.10.21.4

      Power GEOPAK V8i (SELECTseries 10), Version 08.11.09.918

      • Cancel
      • Vote Up 0 Vote Down
      • Sign in to reply
      • Verify Answer
      • Reject Answer
      • Cancel
    • Brian Flaherty
      0 Offline Brian Flaherty Tue, Nov 3 2020 7:42 PM

      The Export-PWDocuments has a parameter -Searches which you can pass the name of your saved search. You should not have to get the documents first and then pipe them to the export cmdlet. 

      # Just a recommendation or suggestion.
      #   You can simplify the following:
      # Set Day.
      $Day = "{0:00}" -f (Get-Date).Day
      	
      # Get Date.
      $Date = (Get-Date).Month.ToString() + "-" + $Day.ToString() + "-" + (Get-Date).Year.ToString()
      
      # Replace with:        
      $Date = Get-Date -Format MM-dd-yyyy

      • Cancel
      • Vote Up 0 Vote Down
      • Sign in to reply
      • Verify Answer
      • Cancel
    • Kevin van Haaren
      0 Offline Kevin van Haaren Tue, Nov 3 2020 8:41 PM in reply to Matt Kovach

      Despite the names (how I hate Microsoft for this) System32 is the 64-bit version of powershell. SysWOW64 is the 32-bit version.

       

      • Cancel
      • Vote Up 0 Vote Down
      • Sign in to reply
      • Verify Answer
      • Cancel
    • Matt Kovach
      0 Offline Matt Kovach Wed, Nov 4 2020 7:02 AM in reply to Brian Flaherty

      Thanks for the Export-PWDocuments tip, Brian. I did try the Date with -Format method, but was getting some inconsistent results during my testing. Will give it another look though.

      ProjectWise Explorer CONNECT Edition, Version 10.00.03.453

      ProjectWise Drive, Version 2022.1.328

      OpenRoads Designer CONNECT Edition, Version 10.09.00.91

      OpenRoads Designer CE 202 Release 2, Version 10.10.21.4

      Power GEOPAK V8i (SELECTseries 10), Version 08.11.09.918

      • Cancel
      • Vote Up 0 Vote Down
      • Sign in to reply
      • Verify Answer
      • Cancel
    • Kevin van Haaren
      0 Offline Kevin van Haaren Wed, Nov 4 2020 9:42 AM in reply to Matt Kovach

      Another thing to watch out for on Scheduled Tasks is line length limits. I'd try and keep the Argument section to 200 characters or less. In your code the parameter names are pretty long which makes the command line longer. I've used some tricks when I've run into this before:

      1. Use the shortened form of the parameter name or create aliases for the parameters to shorten them. The parameter on the command line only needs to be enough characters for powershell to tell which parameter it is.

      2. Use a batch file to launch the powershell. This isn't as useful as you can still run into line length limits of windows but might work.

      3. Use a configuration file. This is my current method for most of my scheduled tasks. I put all my parameters in a configuration file and then just point to the config file on the command line

       

      • Cancel
      • Vote Up 0 Vote Down
      • Sign in to reply
      • Verify Answer
      • Cancel
    >

    Communities
    • Home
    • Getting Started
    • Community Central
    • Products
    • Support
    • Secure File Upload
    • Feedback
    Support and Services
    • Home
    • Product Support
    • Downloads
    • Subscription Services Portal
    Training and Learning
    • Home
    • About Bentley Institute
    • My Learning History
    • Reference Books
    Social Media
    •    LinkedIn
    •    Facebook
    •    Twitter
    •    YouTube
    •    RSS Feed
    •    Email

    © 2023 Bentley Systems, Incorporated  |  Contact Us  |  Privacy |  Terms of Use  |  Cookies