如何使用powershell脚本中的参数调用可执行文件 [英] How to call an executable with parameters from powershell script

查看:36
本文介绍了如何使用powershell脚本中的参数调用可执行文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在寻求有关如何使用 powershell 脚本中的特定参数调用 cmd 的帮助.到目前为止,我写的内容如下,但它给了我一条错误消息,说 $_cmd 无法识别.

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.

我正在尝试将起始日期和截止日期传递给一个 exe...你可以看到起始日期必须是今天 - 1,而截止日期应该是现在.可执行文件的路径是 D:\DataService,这就是我在脚本的早期设置路径的原因.

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%

请问有什么建议吗?

推荐答案

不要创建命令字符串.只需使用调用运算符 (&):

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

这篇关于如何使用powershell脚本中的参数调用可执行文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆