locked
cannot execute cmd command in powershell RRS feed

  • Question

  • Hello,

    I know this question can seem very easy but actually I read that executing cmd commands inside powershell can be done in multiple ways. I tried to execute 2 similar commands.

    This one works fine

    cmd.exe /C "C:\Program Files (x86)\STU-Soft\Business Studio 4.2\BS_WebService\MySQL Server 5.6\bin\mysqldump.exe" -uroot -ptesttest -P($($port_splited[4])) portal> YITPublication.sql 
    

    And this one very similar actually doesn't work

    cmd.exe /C "C:\Program Files (x86)\STU-Soft\Business Studio 4.2\BS_WebService\MySQL Server 5.6\bin\mysql.exe" -uroot -ptesttest -P5556 yitpublication< "C:\BStudio_HTML\MySQL Server 5.6\bin\portal.sql"
    

    saying

    cmd.exe : 'C:\Program' is not recognized as an internal or external command,
    At line:1 char:1
    + cmd.exe /C "C:\Program Files (x86)\STU-Soft\Business Studio 4.2\BS_WebService\My ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: ('C:\Program' is...ternal command,:String) [], RemoteException
        + FullyQualifiedErrorId : NativeCommandError
     
    operable program or batch file.

    Basically, It seems like it doesn't understand first blank space after word "Program" but it didn't so in the first command? There is also blank spaces in the file path.

    OK. I've read that when you have blank spaces inside file path you should envelop it in the `" symbols. so I tried different ways like these:

    cmd.exe /C "`"C:\Program Files (x86)\STU-Soft\Business Studio 4.2\BS_WebService\MySQL Server 5.6\bin\mysql.exe`" -uroot -ptesttest -P5556 yitpublication< `"C:\BStudio_HTML\MySQL Server 5.6\bin\portal.sql`""
    

    gives the same error

    cmd.exe : 'C:\Program' is not recognized as an internal or external command,
    At line:1 char:1
    + cmd.exe /C "C:\Program Files (x86)\STU-Soft\Business Studio 4.2\BS_WebService\My ...
    + ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
        + CategoryInfo          : NotSpecified: ('C:\Program' is...ternal command,:String) [], RemoteException
        + FullyQualifiedErrorId : NativeCommandError
     
    operable program or batch file.
    could you pls help me understand what' wrong with the command and actually what's the difference between the working one and the failed one?


    Thursday, October 1, 2020 8:46 AM

Answers

  • Sorry - here is how to redirect the input in PS.

    $filepath = 'C:\Program Files (x86)\STU-Soft\Business Studio 4.2\BS_WebService\MySQL Server 5.6\bin\mysql.exe'
    $argumentList = '-ptesttest -P5556 yitpublication'
    $infile = 'C:\BStudio_HTML\MySQL Server 5.6\bin\portal.sql'
    Start-Process -FilePath $filepath -ArgumentList $argumentList -RedirectStandardInput $infile


    \_(ツ)_/

    • Marked as answer by AzatM Monday, October 12, 2020 9:25 AM
    Wednesday, October 7, 2020 7:54 AM

All replies

  • Why do you run another command line shell in your Powershell command line shell?  ;-)

    You can run any executable you run in CMD in Powershell as well. To have more control about the execution you can use Start-Process.


    Live long and prosper!

    (79,108,97,102|%{[char]$_})-join''

    • Proposed as answer by Vector BCO Thursday, October 1, 2020 9:48 AM
    Thursday, October 1, 2020 9:35 AM
  • Thanks but I don't think I can run any CMD command in powershell without any manipulations

    If I simply copy this command which I can execute in CMD to powershell It gives me an error

    "C:\Program Files (x86)\STU-Soft\Business Studio 4.2\BS_WebService\MySQL Server 5.6\bin\mysql.exe" -uroot -ptesttest -P5556 yitpublication< "C:\BStudio_HTML\MySQL Server 5.6\bin\portal.sql"

    It doesn't work as I works in CMD

    Thursday, October 1, 2020 9:40 AM
  • Thanks but I don't think I can run any CMD command in powershell without any manipulations

    If I simply copy this command which I can execute in CMD to powershell It gives me an error

    "C:\Program Files (x86)\STU-Soft\Business Studio 4.2\BS_WebService\MySQL Server 5.6\bin\mysql.exe" -uroot -ptesttest -P5556 yitpublication< "C:\BStudio_HTML\MySQL Server 5.6\bin\portal.sql"

    It doesn't work as I works in CMD

    please read previois answer once again

    The opinion expressed by me is not an official position of Microsoft

    Thursday, October 1, 2020 9:49 AM
  • $filepath = 'C:\Program Files (x86)\STU-Soft\Business Studio 4.2\BS_WebService\MySQL Server 5.6\bin\mysql.exe'
    Start-Process -FilePath $filepath -ArgumentList '-ptesttest -P5556 yitpublication< "C:\BStudio_HTML\MySQL Server 5.6\bin\portal.sql"'
    Do things the correct PowerShell way and they will be much easier.

    \_(ツ)_/

    • Proposed as answer by BOfH-666 Friday, October 2, 2020 9:28 AM
    Thursday, October 1, 2020 11:50 PM
  • Unfortunately doesn't work. I see in CMD that it gives hep file for mysql cmd comands. So It didn't interpret the command correctly.
    Tuesday, October 6, 2020 1:22 PM
  • Unfortunately doesn't work. I see in CMD that it gives hep file for mysql cmd comands. So It didn't interpret the command correctly.

    This has nothing to do with PowerShell.  If the SQL executes badly then it is a SQL issue.  You will have to ask in a MySql forum for help with the SQL.


    \_(ツ)_/

    Tuesday, October 6, 2020 5:50 PM
  • That's an issue with the launching this command via powershell. Because when I type this command in CMD it runs fine. So either parameters are passed to the CMD incorrectly or something else. If it would MySQL issue this command didn't run in CMD but in CMD it works fine.
    Wednesday, October 7, 2020 7:04 AM
  • Sorry - here is how to redirect the input in PS.

    $filepath = 'C:\Program Files (x86)\STU-Soft\Business Studio 4.2\BS_WebService\MySQL Server 5.6\bin\mysql.exe'
    $argumentList = '-ptesttest -P5556 yitpublication'
    $infile = 'C:\BStudio_HTML\MySQL Server 5.6\bin\portal.sql'
    Start-Process -FilePath $filepath -ArgumentList $argumentList -RedirectStandardInput $infile


    \_(ツ)_/

    • Marked as answer by AzatM Monday, October 12, 2020 9:25 AM
    Wednesday, October 7, 2020 7:54 AM
  • Thank you. Works fine.
    Monday, October 12, 2020 9:25 AM