Martien Wijnands
Martien Wijnands

Reputation: 1

Executing exe file with spaces and variables

I have some variables:

$FSDUMP_DIR = 'C:\DUMPDIR\'
$ADS_DIR = 'C:\Program Files\Advantage 10.10\Server\'
$ADS_PASS = 'password'
$DATAADT_DIR = 'C:\SHARED\DATAADT\'
$LOGDIR = 'C:\DUMPDIR\BUDUMPRESULT\'
$LOGFILE = $LOGDIR + 'output.txt'
$ADSBACKUP = 'adsbackup.exe'

I want to execute a file, with arguments assembled from the variables and dump the output in a txt file.

I tried this

$ASSEMBLEFilePath = $ADS_DIR + $ADSBACKUP
$ASSEMBLEArgumentlist = '-p' + $ADS_PASS + ' ' + $DATAADT_DIR + 'florisoft.add ' + $FSDUMP_DIR
$ASSEMBLECommand = $ASSEMBLEFilePath + ' ' + $ASSEMBLEArgumentlist
& $ASSEMBLECommand

But it throws an error. So I tried this:

Invoke-Expression $ASSEMBLECommand

But it throws the same error, so i tried this:

Start-Process -FilePath $ADSBACKUP -WorkingDirectory $ADS_DIR -ArgumentList $ASSEMBLEArgumentlist -Wait

This one works, but I cannot capture the output in a textfile (with >> textfile.txt).

Upvotes: 0

Views: 143

Answers (1)

Ansgar Wiechers
Ansgar Wiechers

Reputation: 200293

For using the call operator (&) program and arguments should not be a single string, and the arguments should be in an array:

$FSDUMP_DIR  = 'C:\DUMPDIR'
$ADS_DIR     = 'C:\Program Files\Advantage 10.10\Server'
$ADS_PASS    = 'password'
$DATAADT_DIR = 'C:\SHARED\DATAADT'
$LOGDIR      = 'C:\DUMPDIR\BUDUMPRESULT'

$LOGFILE     = Join-Path $LOGDIR 'output.txt'
$ADSBACKUP   = Join-Path $ADS_DIR 'adsbackup.exe'

$params = '-p', $ADS_PASS, "${DATAADT_DIR}\florisoft.add", $FSDUMP_DIR

& $ADSBACKUP $params

Upvotes: 2

Related Questions