从PowerShell输入cmd的重定向 [英] Input redirection for cmd from PowerShell

查看:166
本文介绍了从PowerShell输入cmd的重定向的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从一个文件的输入运行mysql。我发现以下工作:



cmd / c'C:\Program Files\MySQL ... \mysql -u user -pPassword databasename -f <输入文件名.sql'



问题是databasename($ database)和inputfilename.sql($ file)是变量,需要插值。我没有找到任何方法使插值工作。



请注意,invoke-expression和&将不工作,因为PowerShell不实现<

解决方案

我有

em>完全与您的需要相同。以其他SO答案为例,如这一个,这里是我写的:

  $ binPath =< local MySQL bin path> 
$ mySql = Join-Path $ binPathmysql
...
$ dosCommand =$ mySql --host = $ dbHost --user = $ dbUser --password = $ dbPassword $ dbName --verbose<$ scriptPath
cmd.exe / c $ dosCommand


I am trying to to run mysql with input from a file. I have found that the following works:

cmd /c 'C:\Program Files\MySQL...\mysql -u user -pPassword databasename -f < inputfilename.sql'

The problem is that the databasename ($database) and inputfilename.sql ($file) are variables and need to be interpolated. I have not found any way to make the interpolation work.

Note that invoke-expression and & will not work because PowerShell doesn't implement < for input redirection.

Any suggestions?

解决方案

I had exactly the same need as yours. Taking example from other SO answers like this one, here's what I wrote:

$binPath = <local MySQL bin path>
$mySql = Join-Path $binPath "mysql"
...
$dosCommand = "$mySql --host=$dbHost --user=$dbUser --password=$dbPassword $dbName --verbose < ""$scriptPath"""
cmd.exe /c $dosCommand

这篇关于从PowerShell输入cmd的重定向的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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