Collectives™ on Stack Overflow

Find centralized, trusted content and collaborate around the technologies you use most.

Learn more about Collectives

Teams

Q&A for work

Connect and share knowledge within a single location that is structured and easy to search.

Learn more about Teams

i got a mysterious phenomena if i am going to write a file to the share folder on a remote machine.

object[] theProcessToRun2 = { "CMD.EXE /C " + '"' + "echo.exe test 123 4 5 6 7" + '"' + " " + "> " + '"' + @"C:\temp10\Pythontest.py" + '"' };
using (var managementClass = new ManagementClass(serviceScope, new ManagementPath("Win32_Process"), new ObjectGetOptions()))
managementClass.InvokeMethod("Create", theProcessToRun2);

If i write the same code on the cmd line on the remote machine, it writes a file.

ServiceScope and so on is correct ( i tried it with a python script on the remote machine and works fine... so here is not the error).

What did i do wrong, or did not see it correctly ?

I'm pretty sure you need CMD.EXE and /C to be separate arguments from the rest. So your array should be made up of at least 3 strings and no need for the double quotes. I'm not completely sure about how you need to separate the redirect. – juharr Aug 20, 2018 at 10:55 I would guess that if there isn't any exception, then it's not a permission thing. Why not change the theProcessToRun2 to Notepad.exe and see if it starts up. It might just be your CMD.EXE isn't correct. – Neil Aug 20, 2018 at 10:59

The problem is in the string you build for CMD command line. Let' see

{ "CMD.EXE /C " + '"' + "echo.exe test 123 4 5 6 7" + '"' + " " + "> " + '"' + @"C:\temp10\Pythontest.py" + '"' }

This would be the actual string:

CMD.EXE /c "echo.exe test 123 4 5 6 7" > "C:\temp10\Pythontest.py"

CMD has its own rules for processing command line arguments. This is the description from CMD help which can bee seen by typing CMD /? at command prompt

If /C or /K is specified, then the remainder of the command line after the switch is processed as a command line, where the following logic is used to process quote (") characters:

  • If all of the following conditions are met, then quote characters on the command line are preserved:

  • no /S switch
  • exactly two quote characters
  • no special characters between the two quote characters, where special is one of: &<>()@^|
  • there are one or more whitespace characters between the two quote characters
  • the string between the two quote characters is the name of an executable file.
  • Otherwise, old behavior is to see if the first character is a quote character and if so, strip the leading character and remove the last quote character on the command line, preserving any text after the last quote character.

  • Clearly your string is not covered by the case 1 so case 2 will applied to your command line string. That means CMD will remove the first and last quote after /C switch before executing it. So CMD will try to execute this:

    echo.exe test 123 4 5 6 7" > "C:\temp10\Pythontest.py

    Now you see that the redirection operator is between quotes, It is now just a literal > surrounded in quotes so no redirection is performed and this will be printed on console:

    exe test 123 4 5 6 7" > "C:\temp10\Pythontest.py

    Note that echo is an internal command for CMD and windows does have any executable named echo.exe. By using echo.exe you are actually using internal echo command which .exe is it's argument. echo always skips the first character so . is eliminated and exe will printed.

    So this would be the correct string which work as intended:

    CMD.EXE /c echo test 123 4 5 6 7 > "C:\temp10\Pythontest.py"

    object[] theProcessToRun2 = { "CMD.EXE /C " + "echo test 123 4 5 6 7" + " > " + '"' + @"C:\temp10\Pythontest.py" + '"' };
    

    Ok, got some enlightenment from a German forum.

    The reason, why echo is not working in that case is:

    echo tried to output it values to the stdout - which should be linked to a UI element. After the call is on a remote machine, there is no UI element who can be reached. So all the output is going into the Nirvana.

    That's the reason, why it writes a file, but without content.

    Thanks for contributing an answer to Stack Overflow!

    • Please be sure to answer the question. Provide details and share your research!

    But avoid

    • Asking for help, clarification, or responding to other answers.
    • Making statements based on opinion; back them up with references or personal experience.

    To learn more, see our tips on writing great answers.