Scheduled Task (PowerShell) Partially Works

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.

Parents Reply Children
No Data