Archiving files at source agent using PowerShell script in IBM MQ Managed File Transfer Ant Scripts - ibm-messaging/mq-mft GitHub Wiki
IBM MQ Managed File Transfer (MFT) provides a number of tasks which can be used to build a file transfer solution using Apache Ant. Some of the tasks provided by MFT are:
-
fte:filecopy- Transfer file without removing the source file(s)
-
fte:filemove - Transfer file and remove source file(s)
-
fte:ping - Test the agent availability.
-
fte:call - execute an external program or a script.
Complete list of tasks can be found here: http://www.ibm.com/support/knowledgecenter/SSFKSJ_9.0.0/com.ibm.wmqfte.doc/ant_tasks.htm
Ant scripts are basically XML documents having one or more targets to perform an action. Each target in the script contain task elements to run to perform a job. IBM MQ Managed File Transfer provides tasks as mentioned above which you can use to integrate file transfer function into Apache Ant.
MFT provides a task, fte:call to execute an external script or a program. The agent runs a script or program and returns the outcome of the execution. The commands to call must be accessible to the agent.
This article describes steps required to run a PowerShell script to archive files as part of a file transfer request as postsrc task. As PowerShell scripts can be run only on Windows platform, this article is applicable to Windows only.
MFT Agent uses the CreateProcess Windows API to run an executable. As CreateProcess API can run only executables, it can not execute a PowerShell script directly. An alternative method is required to run the PowerShell script by an MFT Agent. A simple batch file containing the PowerShell script can be used for this purpose.
Here are the steps for setting up transfer and then archiving files.
The first steps is to create a batch file containing the PowerShell command and associated script.
Powershell -executionpolicy bypass -File archive.ps1 %1 %2 %3
The above command runs the PowerShell script that takes three parameters where:
%1 - Parameter 1: Name of the directory containing files which are to be archived.
%2 - Parameter 2: Name of the directory where archived file will be created.
%3 - Parameter 3: Name of the archive file.
The complete contents of command file are here
###Step 2: - .NET Framework version pre-requisite
The PowerShell script to archive files uses System.IO.Compression.FileSystem
.NET assembly to archive the files. This assembly is available from .NET Framework version 4.5. Hence the machine where the source agent runs must have the Microsoft .NET Framework version 4.5 or higher installed. This article assumes .NET Framework version 4.5 is installed on the machine.
###Step 3: Enable PowerShell to use .NET Framework 4.5
PowerShell by default uses Microsoft .NET Framework 2.0. As the System.IO.Compression.FileSystem
is available from .NET Framework v4.5, the PowerShell command must be enabled to use .NET Framework 4.5. This can be done by modifying/creating a application configuration (app.config) file, PowerShell.exe.config
and adding 'supportedRuntime' attribute. The
PowerShell.exe.config` file must placed in the same folder as PowerShell executable, typically in C:\Windows\SysWOW64\WindowsPowerShell\v1.0 directory. Full contents of the file are provided are here.
###Step 4: Create PowerShell script to archive files. Next step is to create a PowerShell script that archives the source files. The script provided by this article takes three parameters as described above in step #1. Full contents of the script are here.
###Step 5: Create MFT Ant script. Next step is to create a MFT Script to initiate the file transfer and then archive the files at source agent. The full contents of the MFT Ant script are here.
###Step 6: Submit transfer request
Next step is to run the Ant script using fteAnt
command.
fteAnt -f archive.xml