How to call an executable file with parameters from powershell script

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

I am trying to pass the date and date to exe ... From today, as you can see, it should be today - 1, and today it should be now. The executable path is D: \ DataService and therefore I set the path at the beginning of 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?

+4
source share
6 answers

Do not create a command line. Just use the call statement ( &):

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
+8
source

Invoke-Expression .

Invoke-Expression $_cmd

, :

. :.

& $_cmd

, %.

+3

%$_cmd% PowerShell cmd. % . PowerShell , , , . ,

Invoke-Expression $_cmd

, ReportGen.exe , cmd , . - , , cmd, cmd /c cmd /k . , cmd :

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

/c , cmd . /k , cmd . , /c, PowerShell script.

+2

. , Write-Host. , , - ( - , ). Write-Output - , script . -, Start-Process -ArgumentList.

+2

$DateFrom $DateTo , $DateTo . , , . , , , , , , . , Invoke-Command , :

Invoke-Command -FilePath "D:\DataService\ReportGen.exe" -ArgumentList '-ReportType Data','-DateFrom $DateFrom','$DateTo'
+2

.

$CustomProcess = New-Object System.Diagnostics.ProcessStartInfo
$CustomProcess.FileName = "ReportGen.exe"
$CustomProcess.arguments = "-ReportType Data -DateFrom $DateFrom $DateTo"
[System.Diagnostics.Process]::Start($CustomProcess)
+1

Source: https://habr.com/ru/post/1524320/


All Articles