Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to call an executable with parameters from powershell script

I am seeking help on how to call cmd with specific parameters from a powershell script. So far what I have written is below, but it gives me an error message saying $_cmd is not recognized.

I am trying to pass the from date and to date to a an exe... The from date as you can see needs to be today - 1 and the to date should be now. The path of the executable is D:\DataService and that's why I am setting the path early in the script.

    Write-Host "Get data from service"

$path ="D:\DataService"

Push-Location $path
$Date = Get-Date
$DateFrom = $Date.ToString("yyyy-MM-dd HH:mm:ss")
$DateTo = $Date.AddDays(-1).ToString("yyyy-MM-dd")
$_cmd = "ReportGen.exe -ReportType Data -DateFrom $DateFrom $DateTo"

%$_cmd%

Any suggestions please?

like image 987
Tarun Arora Avatar asked Jan 29 '14 16:01

Tarun Arora


People also ask

How do I run an exe file from a PowerShell parameter?

You can run exe files in powershell different ways. For instance if you want to run unrar.exe and extract a . rar file you can simply write in powershell this: $extract_path = "C:\Program Files\Containing folder"; $rar_to_extract = "C:\Path_to_arch\file.

How do you prompt a parameter in PowerShell?

A: You can prompt for user input with PowerShell by using the Read-Host cmdlet. The Read-Host cmdlet reads a line of input from the PowerShell console. The –Prompt parameter enables you to display a string of text. PowerShell will append a colon to the end of the string.

What is param () in PowerShell?

Parameters can be created for scripts and functions and are always enclosed in a param block defined with the param keyword, followed by opening and closing parentheses. param() Inside of that param block contains one or more parameters defined by -- at their most basic -- a single variable as shown below.


2 Answers

Don't make a command string. Simply use the call operator (&):

Write-Host 'Get data from service'

$path = 'D:\DataService'

Push-Location $path

$Date     = Get-Date
$DateFrom = $Date.ToString('yyyy-MM-dd HH:mm:ss')
$DateTo   = $Date.AddDays(-1).ToString('yyyy-MM-dd')

& ReportGen.exe -ReportType Data -DateFrom $DateFrom $DateTo
like image 158
Ansgar Wiechers Avatar answered Oct 12 '22 08:10

Ansgar Wiechers


%$_cmd% looks like a mixture of PowerShell and cmd syntax. The %'s have no special significance. PowerShell interprets that as the literal name of a command, which of course is not recognized. To execute the contents of the string, use

Invoke-Expression $_cmd

However, that will only work if ReportGen.exe is in the path, and cmd doesn't even get involved, because you're not calling it anywhere. If for some reason, as you say, you specifically want to execute that command with cmd, you should add cmd /c or cmd /k at the beginning. However, you don't even need to assign to a string, you can just call cmd directly:

cmd /c ReportGen.exe -ReportType Data -DateFrom $DateFrom $DateTo

/c means that cmd will exit after executing the command. /k means the cmd prompt will remain open after the command is executed. You probably want /c so that you'll return to the PowerShell prompt after executing the script.

like image 28
Adi Inbar Avatar answered Oct 12 '22 09:10

Adi Inbar