问题
I successfully added my mysql path to System Variables in Windows variable Path. If I run cmd and then mysql.exe. It will execute the program without problems.
I want to run mysql.exe from PowerShell
$mysql = "mysql.exe"
$params = "-C","-B","-h$server","-P$port","-u$user","-p$password",$db,"-e$query"
& $mysql @params
I tried ./mysql.exe and mysql.exe but it's not working.
I get the error: The term 'mysql.exe' is not recognized as the name of a cmdlet, function, script file, or operable program.
Thanks in Advance for your Help.
回答1:
Remember that PowerShell is a shell that can run commands: You put the command at the beginning of the line, and you put the arguments afterward. If any of the arguments come from variables, just put them there. This is all you need to do:
mysql.exe -C -B "-h$server" "-P$port" "-u$user" "-p$password" $db "-e$query"
For parameters that have their argument "pasted" directly to the parameter (like -P<port>), quote the parameter so PowerShell knows to do the variable expansion within the string.
Of course in this example we are assuming that mysql.exe is in the Path. If it's not and you need to specify the full path and filename to mysql.exe, just put the full path and filename of mysql.exe:
& "C:\Program Files\MySQL\bin\mysql.exe" ...
(That path is made up.) The quotes (") are required since the path contains white space, and the & tells PowerShell to execute the quoted string as a command (rather than simply interpreting it as a string).
来源:https://stackoverflow.com/questions/46937146/run-mysql-exe-as-systemvariable-using-powershellscript