Question
"When executing a PowerShell script in the File Processor script output, how do I call and execute an external PowerShell script? Optionally how can I pass parameters to the external PowerShell script?"
General
In article will demonstrate how you can call an external PowerShell script from inside a PowerShell script in the File Processor Script Editor. It will use a very specific example for demonstration purposes.
Configuration
Let us assume that you have configured a File Processor channel which has an Email input which processes only attachments.
- Go to the Output and select Output Type:
Script
. - Click the Edit-button, behind the Script label to open the Script Editor.
- Set the Script language: to
PowerShell
. - For the script use the code below. It will take the email attachment name and pass it to the external script as a parameter.
function ProcessData($lg, $data, $parameters) {
# add PowerShell code here
# might be needed
set-executionpolicy -executionpolicy unrestricted -scope CurrentUser
if ($data) {
# get the data content
[Winking.FileProcessor.Infrastructure.ServiceInterfaces.InputOutput.IDataContent[]]$emailcontents = ([Winking.FileProcessor.Infrastructure.ServiceInterfaces.InputOutput.IData]$data).DataContents
# we only process attachments, not email+attachment, so index=0 is our attachment (else index=0 is our email body)
$attachmentname = $emailcontents[0].ContentName
# call our script (note that this path doesn't contain any spaces in the path, else you have to escape accordingly)
$scriptpath = "C:\FileProcessor\scripts\externalscript.ps1"
# call the script (note that we have put attachmentname variable in single quotes, because attachment name can contain special characters, like an ampersand &
Invoke-Expression "$scriptpath '$attachmentname'"
}
# ok
$boolResult = $true; # boolean with result to return
return $boolResult;
}
- In the script above, we have configured a
$scriptpath
variable with valueC:\FileProcessor\scripts\
.
Create and go to this directory:C:\FileProcessor\scripts\
.
In this directory, create a file calledexternalscript.ps1
. - Open the
externalscript.ps1
file with Notepad, add the script below and close the file again:
Param(
[Parameter(Mandatory=$true, Position=0)]
[Alias("att")]
$attach
)
function DoStuff() {
if ($attach) {
$attach | Add-Content -path "C:\FileProcessor\scripts\mydata.txt"
}
}
# call the function
DoStuff
If you now execute your File Processor channel it will take an attachment from an unread email, execute our PowerShell script, which will call our external PowerShell script. This external script accepts a parameter, in this case the name of the attachment, and writes it to a file called mydata.txt
.
Extra Info
In case of error(s):
File C:\FileProcessor\scripts\externalscript.ps1 cannot be loaded because running scripts is disabled on this system.
Access to the registry key 'HKEY_LOCAL_MACHINE\SOFTWARE\Microsoft\PowerShell\1\ShellIds\Microsoft.PowerShell' is denied. To change the execution policy for the default (LocalMachine) scope, start Windows PowerShell with the "Run as administrator" option.
Make sure you configure the PowerShell execution policy on your computer.
The line of code in the script set-executionpolicy -executionpolicy unrestricted -scope CurrentUser
allows you to execute scripts from any source (local or internet) for the current user.
See: https:/go.microsoft.com/fwlink/?LinkID=135170