Automation through scripts

<< Click to Display Table of Contents >>

Navigation:  Command line >

Automation through scripts

Command line scripts are small programs that can automate workflows by running a series of command line calls. There are two systems available for this under Microsoft Windows.

The CMD (called "Command Prompt" in the Start menu) has existed since MS-DOS times. The scripts created with it have the file extension .bat (batch) and can be found in many existing systems.

PowerShell was developed from scratch. If necessary, it allows significantly more complex and modern scripts and is not compatible with CMD. The scripts created with it have the file extension .ps1 (PowerShell).

SEPA Transfer allows operation in both CMD and PowerShell notation and therefore fits seamlessly into both environments.

General

Programs usually return a return code after being called. This is a number that can be evaluated after the program run. For SEPA transfer, these are  explainedin the chapter Return codes. The following generally applies: If the return code is not 0, an error has occurred.

CMD

The following is a brief introduction to batch programming using a short example script.

The following script could be written in a "My Import.bat" file, for example:

@echo off

chcp 1252

date /T

time /T

"C:\Program Files (x86)\JAM Software\SEPA-Transfer\SEPA-Transfer.exe" -COMMAND import -SEPA "C:\SEPA files\sepa.xml"

echo The return code during import was %ERRORLEVEL%

pause

1.Normally, every script command is output again in text form by the CMD before execution.  You can prevent this outputwith @echo off.

2.Windows uses the character encoding "Codepage 1252" as standard on German systems. If you use umlauts or special characters (!, =, ...), you should  set the character encoding to this code pagewith chcp at the start of the script .

3.The date /Tcommand  outputs the current date, time /T the current time.

4.Installed programs such as SEPA-Transfer can be called up via the full program path. Many programs allow the execution of functionality via parameters, which you note after the program path. For the operation of SEPA-Transfer, see the chapter Command line control.

5.The echo command  outputs the character string following it on the command line. Variables can be used here.  For example, %ERRORLEVEL% outputs the return code of the last executed program.

6.If you execute a batch file by double-clicking on it, for example, a CMD window will open. It will also close again after all commands have been executed. After outputting the echo text, the CMD would close without you having time to read the text. The pause command  returns "Press any key . . ." and waits for a key to be pressed before continuing the process (i.e. also the end of the script).

PowerShell

The following is a brief introduction to PowerShell programming using a short example script.

The following script could be written in a file "My Import.ps1", for example:

chcp 1252

date

$sepatransfer = (Start-Process -FilePath "C:\Program Files (x86)\JAM Software\SEPA-Transfer\SEPA-Transfer.exe" -ArgumentList "-Command Import -SEPA "C:\SEPA Files\sepa.xml"" -PassThru -Wait)

echo The return code for the import was $sepatransfer.ExitCode

1.Windows uses the character encoding "Codepage 1252" by default on German systems. If you use umlauts or special characters (!, =, ...), you should set the character encoding to this code page at the beginning of the script with chcp.

2.The date command outputs the current date and time.

3.Installed programs, such as SEPA-Transfer, can be called up via the full program path. Many programs allow the execution of functionality via parameters, which you note after the program path. For the operation of SEPA-Transfer, see the chapter Command line control. PowerShell generally continues to execute a script even if the program is still running. To wait for a program, Start-Process is  used  with the -Wait option . The process can be assigned to a variable using -PassThru. We can later use this variable to query the return code, among other things.

4.The command echo outputs the character string following it on the command line. Variables can be used here. $sepatransfer.ExitCode, for example, returns the return code of the process that  belongs to the $sepatransfer variable .

Note: To prevent the execution of potentially malicious PowerShell scripts, non-vendor-certified (i.e. also self-created) scripts are disabled by default in Windows .

You can change this as follows:

1.Right-click on the PowerShell icon.

2.Select "Run as administrator".

3.Executethe command Set-ExecutionPolicy Unrestricted.

Writing command line output to a file

In addition to the option of displaying output text (e.g. using echo) on the command line, it is also possible to write the output of a command to a file. This option applies to both CMD and PowerShell. Here, ">" overwrites a file, ">>" appends the output to a given file. If no file exists, it is created in both cases (provided you have the authorization to do so).

 

chcp 1252

date > "C:\Users\Tom Baker\Desktop\Output.txt"

$sepatransfer = (Start-Process -FilePath "C:\Program Files (x86)\JAM Software\SEPA-Transfer\SEPA-Transfer.exe" -ArgumentList "-Command Import -SEPA "C:\SEPA Files\sepa.xml"" -PassThru -Wait)

echo The return code for the import was $sepatransfer.ExitCode >> "C:\Users\Tom Baker\Desktop\Ausgabe.txt"

The example overwrites or creates an "Output.txt" file on the desktop and writes the current time to it. After executing SEPA-Transfer, the return code is appended to the file. The output.txt file could then have the following content:

Fri May 23 13:37:00 Central European Summer Time 2014

The return code during import was 0

Processing multiple command line commands

If a larger number of command line commands are to be processed, it is possible tostart SEPA Transfer with the -WaitForCommands option.This option starts SEPA Transfer. After calling SEPA-Transfer with the parameter -WaitForCommands, the instance of SEPA-Transfer started in this way can accept other commands by calling SEPA-Transfer with additional parameters. These commands are executed immediately if no dialogs are displayed (see note).

 

The -Stop parametercan  beused toend the instance of SEPA Transfer startedwith -WaitForCommands .

The -Kill parameter can be used toend the instance of SEPA Transfer started with-WaitForCommands without processing pending commands.

 

Example:

1.Starting the main instance in the background (command line A)

C:\...\SEPA-Transfer\Bin>SEPATransfer.exe -WaitForCommands -NoGUI

2.Transfer of a parameter (command lineB)

C:\...\SEPA-Transfer\Bin>SEPATransfer.exe -Command Import -SEPA "sepa.xml"

3.Exit the main instance of the commands (command line B)

C:\...\SEPA-Transfer\Bin>SEPATransfer.exe -Stop

 

Note: If this command is used without the global switch "NOGUI", the graphical user interface starts as usual. Command line commands of another instance can only be edited as long as no dialogs are open (e.g. settings, import/export dialog, etc.). As long as a dialog is displayed, the pending commands are collected and only executed after the dialog is closed.

 

Windows Scripting Host

If you want to use Windows Scripting Host (WSH) in SEPA-Transfer, a command for the import could look like this:

Set Shell = CreateObject("WScript.Shell")

Shell.Run """C:\Program Files (x86)\JAM Software\SEPA-Transfer.exe"" -Command Import -File ""C:\Path\zur\EXCEL.xlsx"" -Command Write -SEPA ""C:\Path\zur\converted\SEPA.xml"" -Type Transfer"