简体   繁体   中英

Powershell variables in command not working

I've tried a ton of different variations of this, but I can't get it to work. I am trying to run mysqldump to export a database (in this case called global).

PS C:\Users\Administrator> &"$mysqlpath\mysqldump.exe -u$mysqluser -p$mysqlpass --databases global | Out-File $env:TEMP\database_backup\global_$timestamp.sql -Encoding UTF8"
& : The term 'C:\Program Files\MySQL\MySQL Server 5.6\bin\mysqldump.exe -ubackup -pbackup_password --databases global | Out-File C:\Users\ADMINI~1\AppData\Local\Temp\2\database_backup\global_2013-12-11T11:47:28.sql -Encoding UTF8' is
not recognized as the name of a cmdlet, function, script file, or operable program.     Check the spelling of the name, or
if a path was included, verify that the path is correct and try again.
At line:1 char:2
+ &"$mysqlpath\mysqldump.exe -u$mysqluser -p$mysqlpass --databases global | Out-Fi ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
    + CategoryInfo          : ObjectNotFound: (C:\Program File... -Encoding UTF8:String) [], CommandNotFoundException
    + FullyQualifiedErrorId : CommandNotFoundException

It looks like it is being exploded into the proper variable names, but I can't get it to run.

UPDATE : So that was the right answer, I had a : in the timestamp. My other problem was solved by putting the variables inside double quotes (")

You are telling Powershell to run an EXE named

&"$mysqlpath\\mysqldump.exe -u$mysqluser -p$mysqlpass --databases global | Out-File $env:TEMP\\database_backup\\global_$timestamp.sql -Encoding UTF8"

So remove the quotes ( " ) and try again. Like so,

& $mysqlpath\\mysqldump.exe -u$mysqluser -p$mysqlpass --databases global | Out-File $env:TEMP\\database_backup\\global_$timestamp.sql -Encoding UTF8

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM