user6808274
user6808274

Reputation: 33

Powershell - how to pass variables within an argument list

I have a script that generates PDF reports using a command line application.

Here's the script in batch form (dates are variables):

QsrCrExport.exe -f"C:\SMTP\Reports\Speed of Service Summary" -o"C:\SMTP\Daily_Archive\SpeedofService.pdf" -a"Date:%one%,%one%" -epdf

I'm trying to implement the above into Powershell using start-process with arguments but cannot seem to get the arguments to correctly work

$crexport = "C:\Program Files (x86)\ReportViewer\Bin\QsrCrExport.exe";
$arguments = '-f"C:\ProgramData\ReportViewer\Reports\Speed of Service Summary" -o"C:\SMTP\Generated\SpeedofService.pdf" -a"Date:$reportdate1,$reportdate2" -epdf'
Start-Process $crexport $arguments -Wait;

I know this won't work wrapped in single-quotes as the variables wont be replaced with the value, however if I remove all the double quotes within the arguments and wrap the whole line in double quotes it still won't work.

I'm pretty new to powershell so apologies if this is something really straight forward.

Upvotes: 1

Views: 9154

Answers (2)

Michael Noe
Michael Noe

Reputation: 82

The problem I see is that you have variables inside single quotes. I've switched the quotes so that everything inside the double quotes becomes expressed as an object instead of a dedicated string. Also by using the -f operator on a string inside the single quotes you can pass variables into the string, even inside single quotes.

$crexport = "C:\Program Files (x86)\ReportViewer\Bin\QsrCrExport.exe"
$arguments = ("-f 'C:\ProgramData\ReportViewer\Reports\Speed of Service Summary' -o 'C:\SMTP\Generated\SpeedofService.pdf' -a 'Date:{0},{1} -epdf'" -f $reportdate1, $reportdate2)
Start-Process $crexport $arguments -Wait

Upvotes: 0

Asnivor
Asnivor

Reputation: 266

You could use an argumentlist array:

Start-Process -FilePath '"C:\Program Files (x86)\ReportViewer\Bin\QsrCrExport.exe"' -ArgumentList @('-f"C:\ProgramData\ReportViewer\Reports\Speed of Service Summary"', '-o"C:\SMTP\Generated\SpeedofService.pdf"', '-a"Date:$reportdate1,$reportdate2"', '-epdf') -wait

That should work.

Upvotes: 1

Related Questions